Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop a debugger

I am looking for a way to do things such as attach to a process, set breakpoints, view memory, and other things that gdb/lldb can do. I cannot, however, find a way to do these things.

This question is similar to this one, but for MacOS instead of Windows. Any help is appreciated!

Note: I want to make a debugger, not use one.

Another thing is that i dont want this debugger to be super complicated, all i need is just reading/writing memory, breakpoint handling, and viewing the GPR

like image 966
Camden Weaver Avatar asked Aug 22 '18 14:08

Camden Weaver


2 Answers

LLDB has an API that can be consumed from C++ or Python. Maybe this is what you’re looking for.

Unfortunately the documentation is fairly threadbare, and there don’t seem to be usage examples. This will therefore entail some reading of the source and a lot of trial and error.

like image 79
Konrad Rudolph Avatar answered Oct 02 '22 01:10

Konrad Rudolph


If you really want to make your own debugger, another way to start would be to figure out how to cons up and parse the gdb-remote protocol packets (e.g. https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html). That's the protocol gdb uses when remote debugging and lldb uses for everything but Windows debugging. On MacOS, lldb spawns a debugserver instance which does the actual debugging and controls it with gdb-remote protocol packets. On Linux, it uses the lldb-server tool that's part of the Linux lldb distribution for the same purpose.

The gdb-remote protocol has primitives for most of the operations you want to perform, launch a process, attach to a process, set breakpoints, read memory & registers and isolates you from a lot of the low-level details of controlling processes.

You can help yourself out by observing how lldb uses this protocol by running an lldb debug session with:

(lldb) log enable gdb-remote packets

But you might also have a look at the SB API's in lldb. The documentation is not as advanced as it should be but there are a bunch of examples in the examples/python directory of the lldb sources to get you started, and in general the API's are pretty straightforward and self-explanatory.

like image 24
Jim Ingham Avatar answered Oct 02 '22 01:10

Jim Ingham