1

In my code I have some runtime assert macro (let's call it runtime_assert). This should be in multi threaded application.

When condition passed evaluated to false, runtime_assert terminates program by dumping stack trace , followed by calling _exit().

As you probably know, dumping stack trace isn't a trivial task (How to get a stack trace for C++ using gcc with line number information?).

The idea is to invoke gdb with pid of the process by calling system().

  1. Is it good idea in general? Or it's better to use process only tools to get backtrace? (e.g. gcc
    backtrace()/backtrace_symbols())
  2. When ptrace() is invoked, will it somehow effect other threads?
  3. If system is out of resources (e.g. memory/disk space) may gdb fork fail?
  4. How to print stack trace of current thread only? (I can get address of current method from gcc backtrace())
Community
  • 1
  • 1
dimba
  • 26,717
  • 34
  • 141
  • 196

2 Answers2

2
  1. Don't think this is a good idea - by the time system() is done and GDB attaches other threads are way beyond the trouble point. I actually don't even think bothering with stack trace is worth it in this case (it might help when you give your app to some "less-sophisticated" users and any info you get out of them is useful). Here I'd just abort() and have a nice core file to peruse.
  2. I don't know if this is specified anywhere - my suspicion it's OS and thread library dependent (somebody please correct me if I'm wrong here).
  3. Yes, it might fail (same for the the core file though).
  4. I believe this could be done via some gettid() and GDB init-file and user-defined command trickery, but again, don't think I would bother.
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • The offending thread is thread that called runtime_assert. stack traces of other threads aren't important in my case. – dimba Jan 25 '11 at 18:07
  • 1
    Hmm, yes, but I'd probably want a whole picture at that particular point. You might like to look at `gcore` instead. – Nikolai Fetissov Jan 25 '11 at 18:13
0

I remember a similar discussion about this some time ago on SO. After searching the forum I found that it was you who asked, and it's the same thread you point out in this question.

Have you seen nobar's answer? It seems it's exactly what you are looking for. The trick is to use execlp to invoke gdb instead of system:

How to get a stack trace for C++ using gcc with line number information?

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426