简体   繁体   中英

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

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

And sometimes it's GDB bash: ~/Library/Caches/gdb/bin/bash.

Q: Why does GDB use different bash to handle the program? How to control/choose the one to run?

  1. Sometimes the running program did stop at the break point and show the gdb prompt. 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.

Q: What's the reason it cannot stop at the break point

  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.

Searched online for all the solutions for this (include stackoverflow.com), none of them worked. While not try one solution which is change gdb's source code.

Source code of 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.

edit ~/.gdbinit file. If it didn't exist, create it. Add one line: set startup-with-shell disable

Then re-run gdb, it will always show the process ~/bin/tryIt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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