views:

58

answers:

4

I want to get address of instruction that causes external program to SIGSEGV. I tried using ptrace for this, but I'm getting EIP from kernel space (probably default signal handler?). How GDB is able to get the correct EIP?

Is there a way to make GDB provide this information using some API?

edit: I don't have sources of the program, only binary executable. I need automation, so I can't simply use "run", "info registers" in GDB. I want to implement "info registers" in my own mini-debugger :)

A: 

Compile your program with -g, run gdb <your_app>, type run and the error will occur. After that use info registers and look in the rip register.

You can use objectdump -D <your_app> to get some more information about the code at that position.

tur1ng
I don't have sources, only binary.
karramba
A: 

You can enable core dumps with ulimit -c unlimited before running your external program.

Then you can examine the core dump file after a crash using gdb /path/to/program corefile

Because it is binary and not compiled with debugging options you will have to view details at the register and machine code level.

Zan Lynx
I need automation. Best solution for me would be to capture register values in my program.
karramba
A: 

Try making a core dump, then analyse it with gdb. If you meant you wanted to make gdb run all your commands at one touch of a key by 'automate', gdb ca do that too. Type your commands into a file and look into the help user-defined section of manuals, gdb can handle canned commands.

vpit3833
+1  A: 

You can attach to a process using ptrace. I found an article at Linux Gazette.

It looks like you will want PTRACE_GETREGS for the registers. You will want to look at some example code like strace to see how it manages signal handling and such. It looks to me from reading the documentation that the traced child will stop at every signal and the tracing parent must wait() for the signal from the child then command it to continue using PTRACE_CONT.

Zan Lynx