Hi!<div><br><div class="gmail_quote">On Mon, Jul 18, 2011 at 18:13, Greg Clayton <span dir="ltr"><<a href="mailto:gclayton@apple.com">gclayton@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
On Jul 18, 2011, at 1:32 PM, Filipe Cabecinhas wrote:<br>
<br>
> Hi,<br>
><br>
> I'm trying to create an LLDB command that sets an internal breakpoint for a function, and then executes some code, but I'm having come difficulties...<br>
><br>
> I've seen the expression command, which does something close to what I want to do after the breakpoint, but I have some doubts. I want the code to be able to return from the function where it's called, but the "target->EvaluateExpression" doesn't let the code return from it (while I would like to execute code with something like "if (condition) return NULL; more code…"). Is there a way to compile arbitrary code (with return statements) and execute it?<br>

<br>
</div>Not currently.<br>
<div class="im"><br>
><br>
> Is there a way to create something like an anonymous function (with certain parameters), and have it compiled and linked, while looking up global variables?<br>
<br>
</div>Current expressions can do the lookups, but as you already know they don't live beyong the first invocation.<br>
<div class="im"><br>
> ClangUtilityFunction doesn't look up any variables, and I can't seem to find a way to look up global variables without a Frame object.<br>
<br>
</div>For globals you shouldn't need the frame. If the globals are in your symbol table and are external you might be able to use dlsym().<br>
<div class="im"><br>
> Is there a way to know a function (or method)'s address from its prototype?<br>
<br>
</div>A normal fuction that was compiled into your code or an expression function?<br></blockquote><div><br></div><div>For my first try (a command like "expr" but that would re-define functions) I wantes to find out the location of some function/method, given the prototype (e.g: "ProcessGDBRemote::StartDebugserverProcess(char const*)"). I would suppose we could mangle the name and try to find the symbol. I haven't seen any way to do that in lldb, but I suppose it's possible to do. Maybe I'm looking at it wrong.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">> My final purpose is to be able to redefine functions on-the-fly (with caveats for inlined functions, etc). The only way I saw that could work was creating a (similar) function and making the other function a trampoline (either using breakpoints, or writing a jmp expression at its address)… Did I miss another easier way?<br>

<br>
</div>We do want the ability to just compile up something in an LLDB command but we don't have that yet. You currently can do this via python if you really want to by making a source file, invoking the compiler on it, and then making a dylib. You can then use the "process load" command to load the shared library:<br>

<br>
(lldb) process load foo.so<br>
<br>
So if you have your python code do the global variable lookups and create the source code, you could hack something together.<br>
<br>
When/if you are ready to try and take over the function, you can look for any "Trampoline" symbols. For a simple a.out program on darwin we see:<br>
<br>
(lldb) file ~/Documents/src/args/a.out<br>
Current executable set to '~/Documents/src/args/a.out' (i386).<br>
(lldb) image dump symtab a.out<br>
Symtab, file = /Volumes/work/gclayton/Documents/src/args/a.out, num_symbols = 18:<br>
               Debug symbol<br>
               |Synthetic symbol<br>
               ||Externally Visible<br>
               |||<br>
Index   UserID DSX Type         File Address/Value Load Address       Size               Flags      Name<br>
------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------<br>
....<br>
[   10]     16     Trampoline   0x0000000000001e76                    0x0000000000000006 0x00010100 __stack_chk_fail<br>
...<br>
[   12]     18     Trampoline   0x0000000000001e7c                    0x0000000000000006 0x00010100 exit<br>
[   13]     19     Trampoline   0x0000000000001e82                    0x0000000000000006 0x00010100 getcwd<br>
[   14]     20     Trampoline   0x0000000000001e88                    0x0000000000000006 0x00010100 perror<br>
[   15]     21     Trampoline   0x0000000000001e8e                    0x0000000000000006 0x00010100 printf<br>
[   16]     22     Trampoline   0x0000000000001e94                    0x0000000000000006 0x00010100 puts<br>
<br>
On MacOSX, you could then easily patch the trampoline code to call your own function for say "printf" by modifying the function address in the PLT entry.<br>


<br>
</blockquote></div><br></div><div>That would be a good solution, at least to substitute functions that are accessed with the PLT. But are the trampolines reified (I don't think so)? Or should I just write to the process' PLT directly, after loading the function?</div>
<div><br></div><div>What about replacing other functions? Let's say that I want to replace a random function (that I can't replace by changing the PLT). If I have information about which functions call it, I can replace the definition of the function by a jump and, if necessary, get the new versions of the functions that call the replaced function (doing the same to them, for a maximum of X iterations, for example). Though I would suppose clang won't give us that information (at least for now).</div>
<div><br></div><div>Thanks for the help,</div><div><br></div><div>  Filipe</div>