# Getting a useful crash dump when an exception is thrown

**URL:** <https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165>\
**Category:** ROOT\
**Created:** [March 3, 2018, 6:15pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165 "2018-03-03T18:15:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwatts](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gwatts](https://root-forum.cern.ch/u/gwatts)\
**Post date:** [March 3, 2018, 6:15pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/1 "2018-03-03T18:15:29Z")

</div>

When I cause an exception in my code, I’d like to get a useful stack dump. Specifically, the line where the exception was thrown. For example, I use the vector::at method to access my vector arrays to make sure I don’t go past the end. In the middle of a very very long script I get an exception because I violate this. But I get no useful information other than the exception is thrown from root.

I’ve attached [crash.cxx](https://root-forum.cern.ch/uploads/default/original/2X/7/71527ed862392d1d8d27ac8cb91dfe898b40cd77.cxx) (196 Bytes) as an example of code that generates this kind of exception. The code is very simple:

```auto
int crash()
{
  vector<int> dude;
  dude.push_back(10);
  cout << "hi" << endl;
  cout << dude.at(10) << endl;
  return dude.size();
}

```

I then run root and get the error:

```auto
gwatts@FourByFour:~/root-binaries/testcrash$ root -b
   ------------------------------------------------------------
  | Welcome to ROOT 6.10/02 http://root.cern.ch |
  | (c) 1995-2017, The ROOT Team |
  | Built for linuxx8664gcc |
  | From tag v6-10-02, 6 July 2017 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] .L crash.cxx++
Info in <TUnixSystem::ACLiC>: creating shared library /home/gwatts/root-binaries/testcrash/./crash_cxx.so
root [1] crash()
hi
Error in <TRint::HandleTermInput()>: std::out_of_range caught: vector::_M_range_check: __n (which is 10) >= this->size() (which is 1)
root [2]

```

When I get that error - I’d like to know what line number that came from - or _something_ else useful about the site of the error. A stack dump would be amazing, since this is all complied code.

I’m running on Ubuntu, 6.10/02. Many thanks in advance!!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 3, 2018, 6:22pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/2 "2018-03-03T18:22:02Z")

</div>

> [@Segmentation Fault depending on linux distribution](https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6):
>
> Try something like (note: some [Valgrind](http://valgrind.org/) functionality requires that the source code, that you want to analyse, is compiled with debug symbols): valgrind --tool=memcheck --leak-check=full [--show-reachable=yes] [--track-origins=yes] [--num-callers=50] [--vgdb=full] --suppressions=`root-config --etcdir`/valgrind-root.supp `root-config --bindir`/root.exe -l -q 'YourMacro.cxx[++g][(Any, Parameters, You, Need)]' valgrind --tool=exp-sgcheck [--num-callers=50] [--vgdb=full] --suppressions=`root-config…

> [@AddressSanitizer memory error detector](https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046):
>
> The default [valgrind’s “memcheck” memory error detector](http://valgrind.org/docs/manual/mc-manual.html) will not find various {stack,global}-buffer overflow bugs. [Valgrind](http://valgrind.org/) does provide a dedicated complementary [“exp-sgcheck” experimental stack and global array overrun detector](http://valgrind.org/docs/manual/sg-manual.html) but, for the time being, [it does not work, as soon as ROOT related libraries are used](https://root-forum.cern.ch/t/valgrind-tool-exp-sgcheck/21044) [and it seems that it is possible to use it](https://root-forum.cern.ch/t/valgrind-tool-exp-sgcheck/21044/2). On the other hand, gcc 4.8 and newer compilers provide their own [AddressSanitizer (a.k.a. ASan) fast memory error detector](https://github.com/google/sanitizers/wiki/AddressSanitizer) (note: you may n…

---

<div class="post-metadata">

**Author:** ![gwatts](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gwatts](https://root-forum.cern.ch/u/gwatts)\
**Post date:** [March 3, 2018, 9:08pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/3 "2018-03-03T21:08:24Z")

</div>

I run this automatically on 100’s of files - so I don’t want to run valgrind or the debugger on everything. Normally, when there is an exception, there is a way to get a crash dump. Is that not the case here? Am I really stuck with valgrind or the debugger?

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [March 3, 2018, 11:04pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/4 "2018-03-03T23:04:27Z")

</div>

Running under gdb will be faster than valgrind, but if that’s also too inconvenient b/c of the number of files, you can try the following code: [trace\_except2.cxx](https://root-forum.cern.ch/uploads/default/original/2X/0/0c03a87f797347a312d1ad1395567cf8d549de91.cxx) (4.1 KB)

Build it into a shared library, then preload it. Run it, and use addr2line to get the line from the address + file name that is printed.

---

<div class="post-metadata">

**Author:** ![gwatts](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gwatts](https://root-forum.cern.ch/u/gwatts)\
**Post date:** [March 4, 2018, 9:04am UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/5 "2018-03-04T09:04:50Z")

</div>

> [@wlav](#):
>
> nto a shared library, then preload it. Run it, and use addr2line to get the line from the addr

Thanks. Ok, the problem is I have about 100 different processes running. And this is a generic problem - so I was hoping for a generic solution. Something I could look at in a log file. For example, a crash dump from root when the exception is thrown. My ATLAS code gives a crash dump… It seems like ROOT must be doing some extra work to suppress this sort of information.

For this I’ll use one of these techniques to solve my current problem. Hopefully there is something that can be done to solve the generic problem.

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [March 4, 2018, 3:36pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/6 "2018-03-04T15:36:22Z")

</div>

> [@gwatts](#):
>
> It seems like ROOT must be doing some extra work to suppress this sort of information.

No conspiracy, just how C++ exceptions work: the exception in your case is uncaught until it reaches the ROOT interpreter. At that point, the stack has been unwound and no further information about it is available.

All the tools proposed print the trace at the point of throwing. An alternative, and more general in the sense that you want it I think, is to not print but store the stack trace at point of throwing. Then only print it if the exception reaches the interpreter uncaught. Of course, code that uses lots of exceptions will be heavily penalized that way.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 4, 2018, 3:38pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/7 "2018-03-04T15:38:14Z")

</div>

@axel Maybe you could incorporate Wim’s solution into CLING?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 6, 2018, 10:31am UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/8 "2018-03-06T10:31:49Z")

</div>

Yes that’s a known issue; e.g. C# (?) passes the stack trace with exceptions.

Forced pre-loading of ABI-specific exception handling code is fairly evil… but maybe I can inject this only if the users sets a certain flag. I have created [https://sft.its.cern.ch/jira/browse/ROOT-9296](https://sft.its.cern.ch/jira/browse/ROOT-9296) to keep track of this. Thanks for the code, Wim!

---

<div class="post-metadata">

**Author:** ![gwatts](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/aca169/32.png) [@gwatts](https://root-forum.cern.ch/u/gwatts)\
**Post date:** [March 6, 2018, 1:01pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/9 "2018-03-06T13:01:23Z")

</div>

Thanks!!

C# does add the stack trace to the exception, indeed. But the C# behavior hadn’t entered my mind at all (I swear! I swear! ;-)) Most C++ code I run on Linux generates a stack trace on an uncaught exception. I guess the deal here is that the exception is actually caught by ROOT, and thus isn’t really a “crash” - and so the stack trace is lost (the catching was what I meant by ROOT doing extra work).

I have since fixed the source fo the bug, but it took some time to isolate this crash from my framework. And, indeed, once I’d isolated it, had I seen the line of code that triggered the exception I probably would have had the bug in a few minutes.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 6, 2018, 1:24pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/10 "2018-03-06T13:24:13Z")

</div>

Can you point me to a C++ program that does it? ATHENA I guess, thanks to Wim’s code - but otherwise they emit a mere

```nohighlight
terminate called after throwing an instance of '...'
Aborted

```

But I totally understand how useful it is; we’ll see how to add it!

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [March 6, 2018, 1:53pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/11 "2018-03-06T13:53:16Z")

</div>

actually, IIRC (and if this hasn’t changed since I left Atlas), the stack trace happy Atlassians used to get was provided by good ol’ Seal from LCG:

- [http://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Control/CxxUtils/CxxUtils/exctrace.h](http://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Control/CxxUtils/CxxUtils/exctrace.h)
- [http://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Control/CxxUtils/src/exctrace/exctrace\_collector.cxx](http://acode-browser1.usatlas.bnl.gov/lxr/source/athena/Control/CxxUtils/src/exctrace/exctrace_collector.cxx)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 20, 2018, 1:53pm UTC](https://root-forum.cern.ch/t/getting-a-useful-crash-dump-when-an-exception-is-thrown/28165/12 "2018-03-20T13:53:20Z")

</div>

Closed on March 20, 2018, 1:53pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
