News for hackers. Stuff that matters.
Re:How Does a C Debugger Work? (2014) by userbinator on Sunday October 18, 2020 @12:53AM (#24815630)
It writes an invalid instruction at this location. What ever this instruction, it just has to be invalid.

On x86 at least, it is a valid instruction. INT3, or CC in hex. There are also the debug registers which implement breakpoints without modifying any code, although it's limited to a maximum of 4 at once.

Characterising gdb as a "C debugger" is quite appropriate --- try to debug the Asm directly with it is an excruciating experience.

Re:How Does a C Debugger Work? (2014) by wallnuss on Sunday October 18, 2020 @01:20AM (#24815696)
Having recently had the pleasure of having to debug JIT-compiled code with an ABI mismatch, I can't overstate how useful `rr` (https://github.com/mozilla/rr) can be to debug assembly. The ability to `rsi` e.g. reverse step instruction is very powerful.

One tool that I started exploring is https://pernos.co/, the ability to do dataflow analysis is super cool. Let's you easily answer the question "How did this value get into this register".

Re:How Does a C Debugger Work? (2014) by jlokier on Sunday October 18, 2020 @11:38AM (#24818616)
`rr` is indeed very excellent.

It's really striking that it can't work on ARM though, due to an ARM architectural issue which x86 doesn't have.

Re:How Does a C Debugger Work? (2014) by pm215 on Sunday October 18, 2020 @08:18AM (#24817275)
Yeah, it's useful to have a special-purpose 'software breakpoint' instruction. Arm actually didn't have one until v5 of the architecture, so some older systems or those wanting to maintain compatibility will still use an arbitrary undefined-instruction pattern. The advantage of an architecturally defined instruction rather than picking something invalid at random is (a) software can rely on future CPUs not deciding to use it for some actual feature (b) software can easily distinguish it from a random attempt to execute an illegal instruction and (c) it acts as a "strong convention" that pushes all software (OSes, debuggers, etc) towards using the same mechanism for setting a software breakpoint, which improves interoperability. In the Unix world the distinction is usually surfaced to the debugger as SIGTRAP vs SIGILL.
Re:How Does a C Debugger Work? (2014) by weinzierl on Sunday October 18, 2020 @09:55AM (#24817900)
(d) it facilitates code obfuscation because you don't have to guess which instruction you have to use to confuse the debugger
Re:How Does a C Debugger Work? (2014) by saagarjha on Sunday October 18, 2020 @01:25AM (#24815710)
Really? I use GDB all the time to debug assembly, it's quite good at it. Perhaps there's something much better that I'm not aware of?
Re:How Does a C Debugger Work? (2014) by userbinator on Sunday October 18, 2020 @02:29AM (#24815914)
Having come from a background of WinDbg (Windows) and DEBUG (DOS) before it, GDB feels very "unergonomic" in comparison. The general verbosity (why do you need an asterisk in front of an address when writing a breakpoint --- as the title of this site so prominently reminds?), lack of a regular hexdump command (16-byte hex+ASCII format) and the rather perplexing behaviour of the "disassemble" command (https://stackoverflow.com/questions/1237489/how-to-disassemb...) are what comes to mind immediately.

WinDbg also lets you set the default base to 16, a feature whose usefulness is greatly appreciated when working with Asm: https://docs.microsoft.com/en-us/windows-hardware/drivers/de... GDB... makes an attempt: https://sourceware.org/bugzilla/show_bug.cgi?id=23390

Re:How Does a C Debugger Work? (2014) by PaulDavisThe1st on Sunday October 18, 2020 @02:45AM (#24815966)
gdb) watch my_ptr

what does this mean? watch when the value at the address given by my_ptr changes, or watch when the value of my_ptr changes (i.e. it is modified to point to a different location) ?

gdb) watch * my_ptr

Ah ... now it's clear what is meant.

gdb) watch 0xfeedface

hmm, now 0xfeedface is not a variable but literally an address. But wait, is it? What does this mean? Watch the value at memory location 0xfeedface? But that's totally inconsistent with the semantics of "watch my_ptr". So,

gdb) watch * 0xfeedface

and once again, no ambiguity over what is going on and consistent syntax.

As for you other complaints, I've been programming for about 33 years in C and C++, and I don't recall ever needing to use a hexdump inside the debugger or the disassemble command. Which is not to say that they're not important for some work, but they are also not important for all work.

Re:How Does a C Debugger Work? (2014) by mormegil on Sunday October 18, 2020 @03:24AM (#24816106)
Wasn't that _exactly_ the OP's point? That GDB is a fine C debugger but its logic and UX choices do not make much sense for debugging assembly code (e.g. there are no asterisks in assembly addresses, so no consistency there, etc.).
Re:How Does a C Debugger Work? (2014) by tom_ on Sunday October 18, 2020 @10:49AM (#24818220)
Names of locations in assembly language are typically constants, similar to global arrays in C. There's no my_ptr, as such; there's an address, and my_ptr is its name. When you watch my_ptr, you want to monitor what's at that address - ``watch *my_ptr'', in gdb terms.

Not the end of the world, just annoying.

Re:How Does a C Debugger Work? (2014) by PaulDavisThe1st on Sunday October 18, 2020 @11:59AM (#24818793)
It seems that people read my comment as sarcastic. It wasn't. I was trying to make the point you're making.
Re:How Does a C Debugger Work? (2014) by mhh__ on Sunday October 18, 2020 @03:20AM (#24816094)
I think GDB is an example of a program which should've been designed around a kind of inverse Greenspun's tenth rule principle i.e. vague semantics and weird syntax so just build a lisp from the start to control it.
Re:How Does a C Debugger Work? (2014) by secondcoming on Sunday October 18, 2020 @10:36AM (#24818142)
> Ah ... now it's clear what is meant.

Is it? I can't tell if your post has a sarcastic tone or not!

Re:How Does a C Debugger Work? (2014) by PaulDavisThe1st on Sunday October 18, 2020 @11:59AM (#24818797)
not sarcastic.
Re:How Does a C Debugger Work? (2014) by trott on Sunday October 18, 2020 @03:37AM (#24816160)
If you can use DDD (which is an interface to GDB), it shows the disassembly.
Re:How Does a C Debugger Work? (2014) by wazari972 on Sunday October 18, 2020 @06:12AM (#24816735)
Author here.

I will rewrite this sentence, "invalid instruction" is too limited. It should be something like "an instruction that traps, and which isn't already used for another purpose". Syscalls trap but they are already used; and INT3/CC are valid instructions.

Re:How Does a C Debugger Work? (2014) by drfuchs on Sunday October 18, 2020 @07:37AM (#24817113)
Your description still has problems on architectures like x86 with instructions of different lengths. In particular, you pretty much have to use a single-byte instruction to do the trap; a multi-byte one runs the risk of clobbering other code that could well be jumped to. That’s why INT3 exists as a single-byte instruction to begin with!
Re:How Does a C Debugger Work? (2014) by dima55 on Sunday October 18, 2020 @01:03AM (#24815658)
What issues did you have debugging assembly? I've done it somewhat, and it SEEMS to work ok.