ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I "try" everything at the top level in main, catch exceptions and use report_error to display the source file and line number. I'd like to print the whole stack in this way, but it seems like I would have to catch exceptions in EVERY function, tag the source file/line number onto a list and rethrow the exception. That seems like a real pain.
Type run to run it, and when the exception occurs and execution stops, type bt for a backtrace (it will print the stack, line numbers, functions, etc.)
Another way:
As long as your program is using throw or abort() to exit, the system will dump your program's core if the limit on the size of the core dump is reasonable.
Run
Code:
ulimit -c
to see the current upper limit in core dump size and
Code:
ulimit -c 100000000
to set it to unlimited.
When your program exits, you'll see something like
Code:
Aborted (core dumped)
A file will be created in the current directory with a name like "core". Run gdb on your program with the -c COREFILENAME option, and you can work on what is essentially a snapshot of your program running just before it aborted. Typing bt will give you a stack trace just as before.
Arg... I just wrote a reply that got munched by some browser malfunction.
To reiterate my lost post:
Thanks for the suggestions. I realized I can get a stack trace with gdb, but if you have an intermittent bug it can be tricky to replicate in gdb, and sometimes compiling with debugging options affects things so I was hoping to prepare a backtrace manually, in the code.
It certainly seems like Java is a winner in this respect... but is there really no nifty was to do it in C++?
Actually, there is. Just add "abort()" to your C++ exception handler(s). Voila - instant stack trace.
There are actually very compelling arguments in favor of leaving "abort()" in production code: if your program encounters a truly irrecoverable error, you *want* to try to capture as much information about the current state of the program as possible. Including the stack traceback. And "abort()" is the way to do it!
PS:
It's not, of course, the *only* solution. But (in many, many cases) it's a perfectly acceptable solution!
Arg... I just wrote a reply that got munched by some browser malfunction.
To reiterate my lost post:
Thanks for the suggestions. I realized I can get a stack trace with gdb, but if you have an intermittent bug it can be tricky to replicate in gdb, and sometimes compiling with debugging options affects things so I was hoping to prepare a backtrace manually, in the code.
It certainly seems like Java is a winner in this respect... but is there really no nifty was to do it in C++?
You don't have to be in gdb to use abort() to generate a core file. Instead of throwing the exception, call abort().
Another way of generating the core is to call the macro assert(). Assert takes a bool condition. If the condition is false it prints a message with the bool condition in it an calls abort(). The assert() processing can be disabled in production code by defining NDEBUG on the compiler command line.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.