News for hackers. Stuff that matters.
Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @01:30PM (#16276208)
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.
Re:GDB 8.1 Released by modeless on Wednesday January 31, 2018 @02:08PM (#16276575)
Having 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.
Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @02:29PM (#16276770)
Any optional features are a good way to have an application behave differently when the option is on, versus off. :)

Programs behave differently under a debugger anyway; "bugs disappearing under a debugger" already happens now.

It's just a question of the risk/reward.

"This debugger feature increases the scope of situations when a bug disappears under the debugger": that's a risk.

"This debugger feature gives me a better overall debugging experience most of the time": benefit

Re:GDB 8.1 Released by dustfinger on Wednesday January 31, 2018 @02:19PM (#16276684)
Yes, and introducing bugs is not the only issue at stake.

Imagine malware that could react differently when a debugger is attached.

Re:GDB 8.1 Released by jcelerier on Wednesday January 31, 2018 @04:13PM (#16277592)
> Imagine malware that could react differently when a debugger is attached.

that has been the case for a long time: simply timing some instructions is generally enough to tell you that you're running under a debugger. It's also helpful for copy protection.

Re:GDB 8.1 Released by ChrisLomont on Wednesday January 31, 2018 @02:31PM (#16276797)
Malware already does this on many platforms via many methods, and has done so for perhaps decades.

Here's [1] the tip of the iceberg on this issue.

[1] antukh.com/blog/2015/01/19/malware-techniques-cheat-sheet/

Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @02:30PM (#16276785)
The 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.
Re:GDB 8.1 Released by dustfinger on Wednesday January 31, 2018 @02:32PM (#16276803)
Well, to be fair malware already exists that can detect when a system debugger is attached so maybe my concern is invalid. I guess the greater concern is the introduction of bugs after all.

Edit - Other commenters beet me to this point :-P

Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @02:40PM (#16276865)
The program is what it is and it has bugs. There is a risk that bugs are irreproducible under a debugger (no matter how transparent the debugger). Oh well; you have to debug those in some other way. That risk shouldn't paralyze us from innovating debuggers.

There are bugs that won't reproduce when you debug via JTAG; so what.

Simply pausing on a breakpoint can make a bug irreproducible.

Programs can infer that they are being debugged via timings, and various side channels.

Re:GDB 8.1 Released by saagarjha on Wednesday January 31, 2018 @03:26PM (#16277239)
The issue is that by introducing this feature, you're increasing the number of programs that behave differently when a debugger is attached. Just because this can be an issue today doesn't mean that we should make it even more of an issue tomorrow.
Re:GDB 8.1 Released by stouset on Wednesday January 31, 2018 @02:31PM (#16276793)
Malware already does this. Non-malware programs already do this to complicate analysis.
Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @02:42PM (#16276884)
If those non-malware programs are fixed to use a proper API instead of side channel hacks, then it simplifies analysis.
Re:GDB 8.1 Released by saagarjha on Wednesday January 31, 2018 @02:59PM (#16277016)
> 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.

Re:GDB 8.1 Released by jburgess777 on Wednesday January 31, 2018 @05:02PM (#16277953)
Something like this is already supported by the GDB "remote" protocol:

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

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_pge13587870...

Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @05:46PM (#16278251)
So 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-Packe...

The program could generate custom notifications.

Re:GDB 8.1 Released by wazari972 on Wednesday January 31, 2018 @01:42PM (#16276334)
this 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...
Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @02:36PM (#16276831)
Okay, 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.

Re:GDB 8.1 Released by wazari972 on Wednesday January 31, 2018 @03:07PM (#16277080)
> 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 !

Re:GDB 8.1 Released by monocasa on Wednesday January 31, 2018 @03:05PM (#16277058)
I'm a little unclear as to what your end goal is.
Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @05:51PM (#16278293)
One 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/aa3...

Also IsDebuggerPresent: 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.

Re:GDB 8.1 Released by monocasa on Wednesday January 31, 2018 @06:18PM (#16278457)
You 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.

Re:GDB 8.1 Released by kazinator on Wednesday January 31, 2018 @09:40PM (#16279321)
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.

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".

Re:GDB 8.1 Released by flukus on Wednesday January 31, 2018 @11:49PM (#16279699)
> 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?

Re:GDB 8.1 Released by kazinator on Thursday February 01, 2018 @01:13PM (#16284164)
stderr also isn't for debug output.

If, say, a compiler puts out "hello.c: syntax error" on its standard error stream, that is not a debugging diagnostic regarding that compiler's internals; it's about "hello.c".

stderr is not an instrumentation tool which goes away when we're not debugging the program.

Re:GDB 8.1 Released by monocasa on Thursday February 01, 2018 @01:32PM (#16284390)
Why? It's not like it'd be printing debugging level messages on release builds in either case.