David Blaikie via llvm-dev
2022-Jan-10 02:22 UTC
[llvm-dev] How to pass the current function name at runtime without additional Global variables?
On Sun, Jan 9, 2022 at 12:42 AM Alberto Barbaro via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi all, > 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. > > 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. >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. Any suggestions?>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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20220109/f0c2fb10/attachment.html>
Wang, Phoebe via llvm-dev
2022-Jan-10 02:58 UTC
[llvm-dev] How to pass the current function name at runtime without additional Global variables?
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: extern int bar(char *, int); int foo(int a) { char *name = __get_name(bar); // This is what we are inserting return bar(name, a); } Ps: Just realize the calling functions are external. Not sure how to do with SOs, static link library should be workable. Thanks Phoebe From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of David Blaikie via llvm-dev Sent: Monday, January 10, 2022 10:22 AM To: Alberto Barbaro <barbaro.alberto at gmail.com> Cc: llvm-dev <llvm-dev at lists.llvm.org> Subject: Re: [llvm-dev] How to pass the current function name at runtime without additional Global variables? On Sun, Jan 9, 2022 at 12:42 AM Alberto Barbaro via llvm-dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>> wrote: Hi all, 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. 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. 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. Any suggestions? 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20220110/7d7c9f02/attachment-0001.html>