<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
{font-family:"\@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.EmailStyle19
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="color:#1F497D">Some wild thoughts. Even release built binaries contain the information of function name (unless you stripe them), we always see them in a crash backtrace. So we may be able to get the function name by the function
address with the aid of debug API. Or write one ourselves. Then we can insert the call to this function before the external call in a pass. For example:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#FFFFFE"><span style="font-size:10.5pt;font-family:Consolas;color:blue">extern</span><span style="font-size:10.5pt;font-family:Consolas;color:black">
</span><span style="font-size:10.5pt;font-family:Consolas;color:blue">int</span><span style="font-size:10.5pt;font-family:Consolas;color:black"> bar(</span><span style="font-size:10.5pt;font-family:Consolas;color:blue">char</span><span style="font-size:10.5pt;font-family:Consolas;color:black">
*, </span><span style="font-size:10.5pt;font-family:Consolas;color:blue">int</span><span style="font-size:10.5pt;font-family:Consolas;color:black">);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#FFFFFE"><span style="font-size:10.5pt;font-family:Consolas;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#FFFFFE"><span style="font-size:10.5pt;font-family:Consolas;color:blue">int</span><span style="font-size:10.5pt;font-family:Consolas;color:black"> foo(</span><span style="font-size:10.5pt;font-family:Consolas;color:blue">int</span><span style="font-size:10.5pt;font-family:Consolas;color:black">
a) {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#FFFFFE"><span style="font-size:10.5pt;font-family:Consolas;color:black">
</span><span style="font-size:10.5pt;font-family:Consolas;color:blue">char</span><span style="font-size:10.5pt;font-family:Consolas;color:black"> *name = __get_name(bar);
</span><span style="font-size:10.5pt;font-family:Consolas;color:green">// This is what we are inserting</span><span style="font-size:10.5pt;font-family:Consolas;color:black"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#FFFFFE"><span style="font-size:10.5pt;font-family:Consolas;color:black">
</span><span style="font-size:10.5pt;font-family:Consolas;color:blue">return</span><span style="font-size:10.5pt;font-family:Consolas;color:black"> bar(name, a);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:14.25pt;background:#FFFFFE"><span style="font-size:10.5pt;font-family:Consolas;color:black">}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Ps: Just realize the calling functions are external. Not sure how to do with SOs, static link library should be workable.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Thanks<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D">Phoebe<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> <b>On Behalf Of
</b>David Blaikie via llvm-dev<br>
<b>Sent:</b> Monday, January 10, 2022 10:22 AM<br>
<b>To:</b> Alberto Barbaro <barbaro.alberto@gmail.com><br>
<b>Cc:</b> llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> Re: [llvm-dev] How to pass the current function name at runtime without additional Global variables?<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sun, Jan 9, 2022 at 12:42 AM Alberto Barbaro via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">Hi all,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I'm working on a little LLVM pass that should add a call to an external function ( C++ project ) and pass as a parameter the name of the function that is about to be called. I have a problem finding a nice way to pass the current function
name. In fact, so far, Unfortunately the only solution that I have found for passing the name is to create a global for each function, store the name there and pass it as a parameter. I don't really like this solution because the size of the final binary
increases and the overall approach feels not optimal.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Assuming that I have a variable `I` which is the CallInst instruction, is it possible to pass the function name at runtime to my external library in a nice way? I thought about passing the address of the instruction and casting it back
to the CallInst instruction but I don't know how to do it.<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">That doesn't quite line up to me - the "Instruction"/CallInst only exists at compile-time, not runtime. So if you embedded the pointer value into the program, casting that pointer back to a CallInst at runtime in the program would only
give you a corrupt pointer/garbage.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">Any suggestions?<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><br>
Not much to it that I can think of - one way or another you'd have to have the string embedded in the program somewhere, or in a side-table (eg: your compiler could produce a little csv file or something that lets you lookup some ID number to the string value
- then you just encode/pass around the ID in the program, and in some post-processing step you can lookup the string in the csv file given the ID). You could avoid a global variable by generating instructions in the caller that could write the string value
into a buffer, then pass that buffer by-value to the function you're calling - but that's a ton of extra instructions compared to passing a pointer to a global/constant buffer.<o:p></o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>