What's the best way to debug a segfault in my SPARC assembly?
I'm writing a sparc compiler. One of my test cases works fine when run from the command line normally开发者_高级运维, but segfaults when I redirect the output to a file.
I've tried using GDB but it's difficult with assembly. How can I figure out something as simple as which line of assembly is causing the segfault?
Unfortunately GDB is really the best way to debug machine-level problems in UnixSPARCs, which is kind of sad.
The basic thing to understand is that a segfault is a kind of exception, and exceptions cause the program to break into the debugger, if one is attached. They do this so that you can examine the state of the processor registers and memory at the moment of the crash.
You should look at the instruction counter register (%ip). This will contain the address of the last instruction to execute, which is the instruction that triggered the segfault. It will be a load or store operation, so you can look at which register contains its source/destination memory address, and figure out why that address is bad (usually it's NULL, or some garbage number that isn't a valid address).
The other thing you can do is compile your process to emit a core dump when it fails, which will write out a snapshot of the program's state at the moment it died as a big file on disk. Unfortunately, the program used to read core dumps is... gdb.
精彩评论