2

Is it possible to trigger an GDB break from C++ in an Android NDK program which still allows the program to resume afterwards?

Meaning, I hit the assert causing GDB to halt the program, and I want to be able to press the "Play" button in Eclipse to resume the program, continuing beyond the assert.

Right now I am using:

__asm__ ("bkpt 0");

This triggers the program to halt, and brings me to the line of code that triggered it, but does not allow me to resume afterwards.

GDB output the following at the time that the program is halted.

(gdb) 
82 info signal SIGBUS
&"info signal SIGBUS\n"
~"Signal        Stop\tPrint\tPass to program\tDescription\n"
~"SIGBUS        Yes\tYes\tYes\t\tBus error\n"
82^done
(gdb) 

If I press "resume" at this point I get the following output in the LogCat:

Fatal signal 11 (SIGSEGV) at 0xfffffffd (code=1)

Perhaps my quesiton is how to throw a non-fatal break?

Goose
  • 1,307
  • 2
  • 14
  • 28

1 Answers1

3

The standard Linux way of detecting if your process is being debugged is:

if (ptrace(PTRACE_TRACEME, 0, NULL, 0) == -1)
    //Yes, we're running under GDB

With that in mind, do a conditional hard breakpoint (bkpt 0) that only fires when under debugger.

Not sure if Java-only debugging in Android would affect ptrace. Give it a try.

EDIT: call raise(SIGABRT) to break. Then in GDB, type signal 0 to continue. Other signals, like SIGINT and SIGTRAP, might work too.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • To clarify, I am _not_ looking for a way to skip the break completely when the debugger isn't attached. I want to be able hit the break, pausing into the debugger (this part works already), and then have the ability to hit "Continue" to resume running the program. Right now if I try to resume after hitting the break, I get an error (adding the error to the original question): Fatal signal 11 (SIGSEGV) at 0xfffffffd (code=1). – Goose Oct 24 '12 at 17:16
  • 1
    Then see [here](http://stackoverflow.com/questions/4326414/set-breakpoint-in-c-or-c-code-programmatically-for-gdb-on-linux). – Seva Alekseyev Oct 24 '12 at 17:36
  • That's it! I've edited your answer to include that link and will mark it as answered. Thank you! – Goose Oct 24 '12 at 19:00
  • Edited in the gist of their answer. – Seva Alekseyev Oct 24 '12 at 20:12
  • 2
    I don't think SIGABRT works in this case; that causes the application to abort. SIGINT did the trick for me. – Goose Oct 24 '12 at 21:58