简体   繁体   English

gdb运行程序问题

[英]gdb run program questions

program need to be debugged: ~/bin/tryIt (generated: gcc -g -Wall -o try try.c) gdb version: GNU gdb (GDB) 8.3需要调试的程序:~/bin/tryIt(生成:gcc -g -Wall -o try try.c) gdb 版本:GNU gdb (GDB) 8.3

Questions for running the program tryIt after setting a same break point for all the trials: 1. there are different bash run the tryIT, sometimes it's OS (MAC OS latest version) bash: ~/bin/tryIt在为所有试验设置相同的断点后运行程序 tryIt 的问题: 1. 有不同的 bash 运行 tryIT,有时它是 OS(MAC OS 最新版本) bash: ~/bin/tryIt

And sometimes it's GDB bash: ~/Library/Caches/gdb/bin/bash.有时它是 GDB bash:~/Library/Caches/gdb/bin/bash。

Q: Why does GDB use different bash to handle the program? Q:为什么GDB使用不同的bash来处理程序? How to control/choose the one to run?如何控制/选择一个运行?

  1. Sometimes the running program did stop at the break point and show the gdb prompt.有时正在运行的程序确实在断点处停止并显示 gdb 提示符。 seems it's still running, but sometimes it stops at the break point.似乎它仍在运行,但有时它会在断点处停止。
    And seems this happens randomly (merely 10% stop) and not matched to the bash running the program.并且这似乎是随机发生的(仅 10% 停止)并且与运行程序的 bash 不匹配。

Q: What's the reason it cannot stop at the break point Q:断点停不下来是什么原因

  1. When the program cannot stop at the breakpoint (showed line like:[New Thread 0x1703 of process 14771]), Ctrl+C doesn't work to stop the program, and also the kill -9 doesn't work to kill the running program (no matter which bash invoke it), has to kill the gdb and do everything again.当程序无法在断点处停止时(显示为:[New Thread 0x1703 of process 14771]),Ctrl+C 不能停止程序,kill -9 也不能杀死正在运行的程序(无论哪个 bash 调用它),都必须杀死 gdb 并再次执行所有操作。

Searched online for all the solutions for this (include stackoverflow.com), none of them worked.在网上搜索了所有解决方案(包括 stackoverflow.com),都没有奏效。 While not try one solution which is change gdb's source code.虽然不尝试一种更改 gdb 源代码的解决方案。

Source code of tryIt.c tryIt.c 的源代码


#include <stdio.h>
#include <stdlib.h>
typedef struct node NODE;

struct node
{
  int x;
  char *name;
  NODE *next; 
};
NODE *getNext(NODE *stNode)
{
  return stNode->next;
}
int main()
{
  NODE n1, n2, n3;
  n1.x = 5;
  n1.name = "n1";
  n1.next = &n2;

  n2.x = 10;
  n2.name = "n2";
  n2.next = &n3;

  n3.x = 20;
  n3.name = "n3";
  n3.next = NULL; 

  struct node *nx = getNext(&n1);
  printf("Current Node:%s, its value:%d\n", nx->name, nx->x);

  NODE *ptr = &n1;
  while (ptr != NULL)
  {
    printf("Current Node:%s, its value:%d\n", ptr->name, ptr->x);
    ptr = ptr->next;
  }

  return 0;
}


I find out how to answer Q1 by myself.我自己找出了如何回答 Q1。

edit ~/.gdbinit file.编辑 ~/.gdbinit 文件。 If it didn't exist, create it.如果它不存在,请创建它。 Add one line: set startup-with-shell disable添加一行:set startup-with-shell disable

Then re-run gdb, it will always show the process ~/bin/tryIt然后重新运行gdb,它会一直显示进程~/bin/tryIt

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

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