4 ms·
I'd really like a way for a program to have an API for interacting with GDB: with that it could tell "I'm running under a debugger". There would be a file desc
by kazinator 9y ago
I'd really like a way for a program to have an API for interacting with GDB: with that it could tell "I'm running under a debugger". There would be a file descriptor which talks to the debugger, if the debugger is present. Output sent to it goes to the debugger and perhaps it could obtain input from the debugger also. Maybe the API could provide some introspection: the program could ask what breakpoints have been set and things like that.
- wazari972 9y agothis is not particularly hard to do with the Python interface: you define structures that are known by the process and the Python module + global variables, then you set an internal (silent) breakpoint on a given 'event' function. In the program you fill the structure with some queries, and in the Python breakpoint callback, you process it as you need. This is the way multi-thread debugging work (used to at least), shared library detection as well...
- kazinator 9y agoOkay, good ideas there: so the program has some standard dispatch function that can be called to talk to the debugger. Normally that does nothing. The debugger can recognize this function and stick a breakpoint in it to alter the behavior. The function can do something like put a request into a mailbox buffer, and then check for a reply in the inbox buffer. When there is no debugger, there is never any reply. When the debugger stops the show with the breakpoint, it processes the request and prepares a reply, flipping some "reply present" flag; when the program is resumed then, it gets the reply. It would be good not have any of that damned Python monkey business involved in this.
- wazari972 9y ago> It would be good not have any of that damned Python monkey business involved in this. please, GDB internals are open source, you can do all of that in plain C :D That's what I did initially, to give GDB the ability to distinguish user-level threads (see libthread_db). Having a high-level language / interface to GDB is a great again of time !
- modeless 9y agoHaving the program behave differently under a debugger is a great way to introduce bugs that disappear when the debugger is present. Any features of this type would have to be completely optional.
- dustfinger 9y agoYes, and introducing bugs is not the only issue at stake. Imagine malware that could react differently when a debugger is attached.
- kazinator 9y agoThe debugger could have a mode of "stealth attachment" under which that feature is not activated; the connection with that special interface is not made, and so the program is not informed that it's under a debugger, and those debug streams don't connect to anything.
- stouset 9y agoMalware already does this. Non-malware programs already do this to complicate analysis.
- kazinator 9y agoIf those non-malware programs are fixed to use a proper API instead of side channel hacks, then it simplifies analysis.
- saagarjha 9y ago> If those non-malware programs are fixed to use a proper API instead of side channel hacks, then it simplifies analysis. I think you’re missing the point as to why programs would do this. Usually it’s to protect some sort of DRM scheme through obscurity.
- deleted 9y ago[deleted]
- monocasa 9y agoI'm a little unclear as to what your end goal is.
- kazinator 9y agoOne use case would be to have some debug_printf() in the program that shows up in the debugger, no matter whether the debugger is remote or local. It doesn't go to some console, or file, but always to the debugger. The Microsoft environment has something like this: OutputDebugString. Very useful. https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3... Also IsDebuggerPresent: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680345(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms6... and other things. The debugging experience on Windows 20+ years ago was was ahead of debugging on GNU/Linux today, I'm afraid, so this stuff is above criticism from the "M$ SuX" angle.
- monocasa 9y agoYou don't need OutputDebugString on because you can always tap the write syscall on stdout from gdb. And I don't really know what IsDebuggerPresent gets you. You're really focusing on features, rather than use cases.
- kazinator 9y agoWell, stdout isn't a debug channel; you might want to debug a program that actually uses stdout for its output, not for debug chatter. Never mind that though, we can take some other descriptor, like 3, and bind it to, say /dev/null. What are the gdb commands to have whatever is going to file descriptor 3 be shown inside gdb? I really do not care how it's implemented. However, "tightly integrated" is better than "flimsy hack"; "consumes breakpoints" is better than "doesn't consume breakpoints".
- flukus 9y ago> Well, stdout isn't a debug channel; you might want to debug a program that actually uses stdout for its output, not for debug chatter. What's your use case for this that makes stderr unsuitable?
- jburgess777 9y agoSomething like this is already supported by the GDB "remote" protocol: https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.ht... In your program you would probably need to link in some code to implement the protocol, like the GDB remote stub: https://sourceware.org/gdb/onlinedocs/gdb/Remote-Stub.html https://sourceware.org/gdb/onlinedocs/gdb/Remote-Stub.html Qemu and Valgrind both implement their own gdb stubs. For ARM targets, there is a similar feature named "semihosting": http://www.keil.com/support/man/docs/armcc/armcc_pge1358787046598.htm http://www.keil.com/support/man/docs/armcc/armcc_pge13587870...
- kazinator 9y agoSo that's a different approach: integrate a remote debugging GDB server into the program (could be a shared lib that is dlopen-ed, by the way). Then that server is always used, even in a local session. The server is deeply integrated into the program, taking advantage of the protocol to communicate between application code and the debugging client. It sounds promising. A particularly interesting area is events: https://sourceware.org/gdb/onlinedocs/gdb/Notification-Packets.html#Notification-Packets https://sourceware.org/gdb/onlinedocs/gdb/Notification-Packe... The program could generate custom notifications.