0

I have a small C++ program my-program. When I run my-program directly

./my-program arg1

everything is ok. I don't get any errors. Besides I have my-script.sh:

#!/bin/sh
my-program $1

When I run this script directly:

./my-script.sh arg1

I don't get any errors.

The problem appears when my-script.sh is run from other linux processes. In this case I sometimes(!) get Segmentation fault error. What I did: I added the -g switch to c++ compiler and edited my-script.sh, so it became:

#!/bin/sh
gdb -batch -x gdb-script --args my-program $1

In gdb-script I wrote:

run

However, I still can't find the line which causes Segmentation fault error. How can I make dgb print the stacktrace to some file after error? Or maybe there are ways to get the place in program which causes this error?

2
  • 1
    Add bt (or backtrace or where) to your "gdb-script" file? - if you have many threads, you may want to do more to print all stacks for all threads - I'm not sure what that command is, but I know it can be done. Commented Feb 14, 2017 at 8:25
  • Try running under valgrind. Commented Feb 14, 2017 at 12:12

2 Answers 2

0

The problem appears when my-script.sh is run from other linux processes.

Your script contains several mistakes:

  1. It will only work if the directory in which my-program resides is on your $PATH.

  2. It does not pass arguments to my-program correctly. In particular, only the the first argument is passed, and if that argument contains spaces, it will be split into words and will become multiple arguments by the time my-program is invoked.

To fix this, do something like:

#!/bin/sh
exec $(dirname "$0")/my-program "$@"

How can I make dgb print the stacktrace to some file after error?

Append where command to the gdb-script.

Sign up to request clarification or add additional context in comments.

Comments

0

You can check if you have some limit for core dumps set in your system:

ulimit -c

If it is '0', you won't get any core dumps generated. Set:

ulimit -c unlimited

And then run the program as long as you get 'Segmentation fault'. Then you should have core dump file generated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.