Imagine malware that could react differently when a debugger is attached.
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.
Here's [1] the tip of the iceberg on this issue.
[1] antukh.com/blog/2015/01/19/malware-techniques-cheat-sheet/
Edit - Other commenters beet me to this point :-P
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.
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.
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
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...
It sounds promising. A particularly interesting area is events:
https://sourceware.org/gdb/onlinedocs/gdb/Notification-Packe...
The program could generate custom notifications.
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.
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 !
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.
And I don't really know what IsDebuggerPresent gets you.
You're really focusing on features, rather than use cases.
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".
What's your use case for this that makes stderr unsuitable?
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.