When I run a program with gdb and have a commands list, the commands aren't invoked the first time the breakpoint is hit. I setup a little test program to demonstrate.
test.c:
#include <stdio.h>
void incrementI(){
static int i = 0;
i++;
printf("i: %d\n", i);
sleep(10);
}
int main() {
int i = 0;
while(1){
incrementI();
}
return 0;
}
commands list: testgdbBreakpoint
set breakpoint pending on
b incrementI
commands
backtrace
continue
end
shell script which runs gdb: testgdbinvoke.sh
#!/bin/sh
gdb -x testGDBBreakpoint -ex=r --args testGDB
The output I see when running testgdbinvoke.sh is:
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from testGDB...done.
Breakpoint 1 at 0x10478
Starting program: /data/config/testGDB
Breakpoint 1, 0x00010478 in incrementI ()
(gdb) c
Continuing.
i: 1
Breakpoint 1, 0x00010478 in incrementI ()
#0 0x00010478 in incrementI ()
#1 0x000104d8 in main ()
i: 2
Breakpoint 1, 0x00010478 in incrementI ()
#0 0x00010478 in incrementI ()
#1 0x000104d8 in main ()
i: 3
The first time the break point is hit, I don't see a backtrace, instead I have to manually continue.
Are there specific options I have to set in the commands list in order to have them run everytime the breakpoint is hit?
Reproduced using GDB-10.0.
There appears to be some kind of unexpected interaction between using multiple -x or -ex arguments. This doesn't work:
gdb -x testgdbBreakpoint -ex run ./a.out
gdb -ex 'source testgdbBreakpoint' -ex run ./a.out
But adding run at the end of testgdbBreakpoint works as expected:
gdb -q -x 'testgdbBreakpoint' ./a.out
Breakpoint 1 at 0x401136: file test.c, line 4.
Breakpoint 1, incrementI () at test.c:4
4 i++;
#0 incrementI () at test.c:4
#1 0x0000000000401189 in main () at test.c:11
i: 1
Breakpoint 1, incrementI () at test.c:4
4 i++;
#0 incrementI () at test.c:4
#1 0x0000000000401189 in main () at test.c:11
i: 2
^C
gdb/mi complains about undefined command when I use -display-insert
relevent page in gdb manual
The -display-insert Command
Synopsis
-display-insert expression
Display expression every time the program stops.
GDB Command
The corresponding GDB command is `display'.
My operation:
gdb test -i mi
b main
r
-display-insert $rsp
Output:
[yuan#Blanche test]$ gdb test -i mi
=thread-group-added,id="i1"
~"GNU gdb (GDB) 8.3\n"
~"Copyright (C) 2019 Free Software Foundation, Inc.\n"
~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law."
~"\nType \"show copying\" and \"show warranty\" for details.\n"
~"This GDB was configured as \"x86_64-pc-linux-gnu\".\n"
~"Type \"show configuration\" for configuration details.\n"
~"For bug reporting instructions, please see:\n"
~"<http://www.gnu.org/software/gdb/bugs/>.\n"
~"Find the GDB manual and other documentation resources online at:\n <http://www.gnu.org/software/gdb/documentation/>."
~"\n\n"
~"For help, type \"help\".\n"
~"Type \"apropos word\" to search for commands related to \"word\"...\n"
~"Reading symbols from test...\n"
(gdb)
b main
&"b main\n"
~"Breakpoint 1 at 0x1165: file test.c, line 10.\n"
=breakpoint-created,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000000000001165",func="main",file="test.c",fullname="/home/yuan/test/test.c",line="10",thread-groups=["i1"],times="0",original-location="main"}
^done
(gdb)
r
&"r\n"
~"Starting program: /home/yuan/test/test \n"
=thread-group-started,id="i1",pid="14434"
=thread-created,id="1",group-id="i1"
=breakpoint-modified,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000555555555165",func="main",file="test.c",fullname="/home/yuan/test/test.c",line="10",thread-groups=["i1"],times="0",original-location="main"}
=library-loaded,id="/lib64/ld-linux-x86-64.so.2",target-name="/lib64/ld-linux-x86-64.so.2",host-name="/lib64/ld-linux-x86-64.so.2",symbols-loaded="0",thread-group="i1",ranges=[{from="0x00007ffff7fd4100",to="0x00007ffff7ff2c64"}]
^running
*running,thread-id="all"
(gdb)
=library-loaded,id="/usr/lib/libc.so.6",target-name="/usr/lib/libc.so.6",host-name="/usr/lib/libc.so.6",symbols-loaded="0",thread-group="i1",ranges=[{from="0x00007ffff7dde630",to="0x00007ffff7f2689f"}]
=breakpoint-modified,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000555555555165",func="main",file="test.c",fullname="/home/yuan/test/test.c",line="10",thread-groups=["i1"],times="1",original-location="main"}
~"\n"
~"Breakpoint 1, main () at test.c:10\n"
~"10\t int x = 1;\n"
*stopped,reason="breakpoint-hit",disp="keep",bkptno="1",frame={addr="0x0000555555555165",func="main",args=[],file="test.c",fullname="/home/yuan/test/test.c",line="10",arch="i386:x86-64"},thread-id="1",stopped-threads="all",core="2"
(gdb)
-display-insert $rsp
^error,msg="Undefined MI command: display-insert",code="undefined-command"
(gdb)
Note the error
^error,msg="Undefined MI command: display-insert",code="undefined-command"
I expect -display-insert to work just like display in gdb, which is what the manual claimed.
That's the old GDB manual, the current manual can be found here:
https://sourceware.org/gdb/current/onlinedocs/gdb/
The -display-insert command was removed from the manual in 2006 - I don't believe this command was ever actually implemented.... or maybe it was and was removed from GDB before 2006.
You need to investigate using GDB/MI Variable Objects instead of display commands when using the MI, see this part of the manual: https://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI-Variable-Objects.html#GDB_002fMI-Variable-Objects
I'm using clion to write a console application. If I simply run the program, I can see the results of my cout calls. But if I debug it, no output occurs under the Debug Console tab other than my exe's name and Process finished with exit code 0. Is there an extra step to get console output to show up under debug in clion?
Or is this not even clion specific and is a general thing people who have been using gdb already know about?
According to the JetBrains'post on clion debugger, you can see the debug output by click the "Console" tab, which is next to "Debugger" tab:
GDB manipulates the process of running a program.
An example of GDB session:
% cat hello.c
#include<stdio.h>
main() {
int count;
for (count=0;count<10;count++)
printf("Hello from CETS!\n");
}
% gcc -g hello.c
% gdb ./a.out
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.13 (sparc-sun-solaris2.3),
Copyright 1994 Free Software Foundation, Inc...
(gdb) b main
Breakpoint 1 at 0x10784: file hello.c, line 6.
(gdb) r
Starting program: /home1/b/bozo/./a.out
Breakpoint 1, main () at hello.c:6
6 for (count=0;count<10;count++)
(gdb) s
7 printf("Hello from CETS!\n");
(gdb) p count
$1 = 0
(gdb) disp count
1: count = 0
(gdb) set count=8
(gdb) s
Hello from CETS!
6 for (count=0;count<10;count++)
1: count = 8
(gdb)
7 printf("Hello from CETS!\n");
1: count = 9
(gdb) c
Continuing.
Hello from CETS!
Program exited with code 01.
(gdb) q
%
Content that might be helpful for you:
http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.html
http://www.ifp.illinois.edu/~nakazato/tips/xgcc.html#GDB
http://www.seas.upenn.edu/cets/answers/gcc.html
Short question: Is there any support of D2 in gdb (I have gdb 7.2) ?
Long story: I compiled next little application ...
// file main.d
int glVar = 0xAAAAAAAA;
void main()
{
glVar = 0xBBBBBBBB;
}
... using command "dmd -gc -debug main.d";
then I load it to gdb and trying to debug it:
vnm#vnm:~/proj/d_gdb_test$ gdb main
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/vnm/proj/d_gbb_test/main...done.
(gdb) b main
Breakpoint 1 at 0x804b667
(gdb) r
Starting program: /home/vnm/proj/d_gbb_test/main
[Thread debugging using libthread_db enabled]
Breakpoint 1, 0x0804b667 in main ()
(gdb) info line
No line number information available.
(gdb) info variables glVar
All variables matching regular expression "glVar":
File main.d:
int _D4main5glVari;
Why gdb can't show line information and why it shows symbols in mangled form ? Is this software issues or I'm doing something wrong ?
gdb has support for D starting with version 7.2. So, you can debug D programs with gdb 7.2.
Now, that doesn't mean that support is perfect - far from it in fact. For instance, I'm not sure that you can get it to print strings properly. And it's not at all surprising if it doesn't demangle D symbols. So, it works, but it's far from perfect.
I have problem with gdb, it wasn't stop in one function. Can you explain why and how to stop program after calling strcpy?
grzes#trampek:~/poligon$ gdb ./char_array2
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/grzes/poligon/char_array2...done.
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6 printf("ssss");
7 strcpy(str_a, "Hello, world!\n");
8 printf(str_a);
9 }
10
(gdb) break main
Breakpoint 1 at 0x8048465: file char_array2.c, line 4.
(gdb) run
Starting program: /home/grzes/poligon/char_array2
Breakpoint 1, main () at char_array2.c:4
4 int main() {
(gdb) break strcpy
Breakpoint 2 at 0x1a1205
(gdb) cont
Continuing.
ssssHello, world!
Program exited with code 016.
(gdb) q
grzes#trampek:~/poligon$
You may be using gcc's __builtin_strcpy. Compile with gcc -fno-builtin and see if that helps.
For more info see: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/C-Dialect-Options.html#C-Dialect-Options