<div dir="ltr"><div>I recently started using lldb to write a basic instrumentation tool for tracking the values of variables at various code-points in a program. I've been working with lldb for less than two weeks, so I am pretty new. Though, I have used and written llvm passes in the past, so I'm familiar with the clang/llvm/lldb ecosystem.<br></div><div><br></div><div>I have a very early prototype of the tool up and running, using the C++ API. The user can specify either an executable to run or an already-running PID to attach to. The user also supplies a file+line_number at which a breakpoint (with a callback) is placed. For testing/prototyping purposes, the breakpoint callback just increments a counter and then immediately returns false. Eventually, more interesting things will happen in this callback.</div><div><br></div><div>I've noticed that just the action of hitting a breakpoint and invoking the callback is very expensive. I did some instruction-count collection by running this lldb tool on a simple test program, and placing the breakpoint+callback at different points in the program, causing it to get triggered different amounts of times. I used `perf stat -e instructions ...` to gather instruction exec counts for each run. After doing a little math, it appears that I'm incurring 1.0 - 1.1 million instruction execs per breakpoint.</div><div><br></div><div>This amount of slowdown is prohibitively expensive for my needs, because I want to place callbacks in hot portions of the "inferior" program.</div><div><br></div><div>Is there a way to make this faster? Is it possible to create "lighter-weight" breakpoints? I really like the lldb API (though the documentation is lacking in some places), but if this performance hit can't be mitigated, it may be unusable for me.</div><div><br></div><div>For reference, this is the callback function:</div><div><br></div><div>``` </div><div>static int cb_count = 0;</div><div>bool SimpleCallback (</div><div> void *baton, </div><div> lldb::SBProcess &process,</div><div> lldb::SBThread &thread,</div><div> lldb::SBBreakpointLocation &location) {</div><div> //TODO: Eventually do more interesting things...</div><div> cb_count++; </div><div> return false;</div><div>}</div><div>```</div><div><br></div><div>And here is how I set it up to be called back:</div><div><br></div><div>```</div><div>lldb::SBBreakpoint bp1 = debugger_data->target.BreakpointCreateByLocation(file_name, line_no);</div><div>if (!bp1.IsValid()) std::cerr << "invalid breakpoint";</div><div>bp1.SetCallback(SimpleCallback, 0);</div><div>```</div><div><br></div><div>-Benjamin</div>
</div>