简体   繁体   English

C ++:在Linux Shell脚本中运行gdb

[英]C++: running gdb in linux shell script

I have a small C++ program my-program . 我有一个小的C ++程序my-program When I run my-program directly 当我直接运行my-program

./my-program arg1

everything is ok. 一切都好。 I don't get any errors. 我没有任何错误。 Besides I have my-script.sh : 另外我有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. 从其他Linux进程运行my-script.sh时出现问题。 In this case I sometimes(!) get Segmentation fault error. 在这种情况下,我有时会收到Segmentation fault错误。 What I did: I added the -g switch to c++ compiler and edited my-script.sh , so it became: 我所做的:我将-g开关添加到c ++编译器并编辑了my-script.sh ,因此它变为:

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

In gdb-script I wrote: gdb-script我写道:

run

However, I still can't find the line which causes Segmentation fault error. 但是,我仍然找不到导致Segmentation fault错误的行。 How can I make dgb print the stacktrace to some file after error? 错误后如何使dgb将stacktrace打印到某些文件? Or maybe there are ways to get the place in program which causes this error? 也许有办法在程序中引起该错误的地方?

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. 如果它为“ 0”,则不会生成任何核心转储。 Set: 组:

ulimit -c unlimited

And then run the program as long as you get 'Segmentation fault'. 然后,只要出现“分段错误”,就运行该程序。 Then you should have core dump file generated. 然后,您应该已生成核心转储文件。

The problem appears when my-script.sh is run from other linux processes. 从其他Linux进程运行my-script.sh时出现问题。

Your script contains several mistakes: 您的脚本包含几个错误:

  1. It will only work if the directory in which my-program resides is on your $PATH . 仅当my-program所在的目录位于$PATH上时,它才有效。

  2. It does not pass arguments to my-program correctly. 它没有将参数正确传递给my-program 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. 特别是,仅传递第一个参数,并且如果该参数包含空格,则将在调用my-program时将其拆分为多个单词并将成为多个参数。

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? 错误后如何使dgb将stacktrace打印到某些文件?

Append where command to the gdb-script . where命令附加到gdb-script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM