gdb linux環境でC言語のデバッグ

 [root@linux1 test]# gdb gdbsample
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-20.el8
Copyright (C) 2018 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 "x86_64-redhat-linux-gnu".
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 gdbsample...done.
(gdb) break main
Breakpoint 1 at 0x40060e: file ./gdbsample.c, line 11.
(gdb) run
Starting program: /work/sakag/routine/modify_original/test/gdbsample
Breakpoint 1, main () at ./gdbsample.c:11
11              char buf[] = {"a1       b2      c3"};
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-236.el8.7.x86_64 libgcc-8.5.0-20.el8.x86_64 libstdc++-8.5.0-20.el8.x86_64
(gdb) l
6       #include        <string.h>
7
8       #define M_SEPARATE      '\t'
9
10      int main() {
11              char buf[] = {"a1       b2      c3"};
12              char *p = buf;
13              char *tmp = NULL;
14
15              printf("buf=%s\n",buf);
(gdb) s
12              char *p = buf;
(gdb) s
13              char *tmp = NULL;
(gdb) print p
$1 = 0x7fffffffe317 "a1\tb2\tc3"
(gdb) s
15              printf("buf=%s\n",buf);
(gdb) s
buf=a1  b2      c3
16              while(*p != NULL) {
(gdb) break 16
Breakpoint 2 at 0x400648: file ./gdbsample.c, line 16.
(gdb) s
17                      tmp = strchr(buf, M_SEPARATE);
(gdb) s
18                      if (tmp != NULL) {
(gdb) p tmp
$2 = 0x7fffffffe319 "\tb2\tc3"
(gdb) c
Continuing.
Breakpoint 2, main () at ./gdbsample.c:16
16              while(*p != NULL) {
(gdb) l
11              char buf[] = {"a1       b2      c3"};
12              char *p = buf;
13              char *tmp = NULL;
14
15              printf("buf=%s\n",buf);
16              while(*p != NULL) {
17                      tmp = strchr(buf, M_SEPARATE);
18                      if (tmp != NULL) {
19                              *tmp = ',';
20                      }
(gdb) clear 16
Deleted breakpoint 2
(gdb) b 17
Breakpoint 3 at 0x400653: file ./gdbsample.c, line 17.
(gdb) s
Breakpoint 3, main () at ./gdbsample.c:17
17                      tmp = strchr(buf, M_SEPARATE);
(gdb) p buf
$3 = "a1,b2\tc3"
(gdb) c
Continuing.
Breakpoint 3, main () at ./gdbsample.c:17
17                      tmp = strchr(buf, M_SEPARATE);
(gdb) c
Continuing.
Breakpoint 3, main () at ./gdbsample.c:17
17                      tmp = strchr(buf, M_SEPARATE);
(gdb) s
18                      if (tmp != NULL) {
(gdb) p tmp
$4 = 0x0
(gdb) p buf
$5 = "a1,b2,c3"
(gdb) c
Continuing.
Breakpoint 3, main () at ./gdbsample.c:17
17                      tmp = strchr(buf, M_SEPARATE);
(gdb) clear
Deleted breakpoint 3
(gdb) c
Continuing.
buf changed=a1,b2,c3
[Inferior 1 (process 11940) exited normally]
(gdb) quit
[root@lnux1 test]#

command:
->省略形
break main ->b main
run ->r
break 120 ->b 120
list ->l
continue ->c
step ->s
clear(clear breakpoint) ->cl
quit(quit) ->q
info break(info breakpoints) -> i b
info local: ローカル変数の一覧を表示
watch global_var : グローバル変数が更新されたときに通知する
finish:現在の関数を最後まで実行
until: loopを抜ける
record:recordを始める
reverse-step:stepを1つ戻す
reverse-continue:continueを1つ戻す
reverse-finish:finishを1つ戻す
record stop : recordを止める

# cat gdbsample.mk

SRCDIR     = .
OBJDIR     = .
LIBDIR     = .

TARGETDIR  = .

TARGETLIB  = $(LIBDIR)/gdbsample
TARGETOBJ  = $(OBJDIR)/gdbsample.o
TARGETSRC  = $(SRCDIR)/gdbsample.c

CC = g++
CCOPTIONS = -c -g -O0 -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small
### gdb needs -g -O0 options
### gdb needs -g3 -O0 options for macro debugging
LIBCC = $(CC)
LDOPTIONS = -m64

.SUFFIXES: .c .o

# -------------------------------
# rules
# -------------------------------
all: $(TARGETLIB)

$(TARGETLIB): $(TARGETOBJ)
        $(LIBCC) $(LDOPTIONS) $(TARGETOBJ) -o $(TARGETLIB)

$(TARGETOBJ): $(TARGETSRC)
        $(CC) $(CCOPTIONS) $(TARGETSRC) -o $(TARGETOBJ)

clean:
        @rm -f $(TARGETOBJ) $(TARGETLIB)

install:
        @cp -p $(TARGETLIB) $(TARGETDIR)

# -------------------------------
# end of makefile
# -------------------------------
[root@linux1 test]#

# cat gdbsample.c
// test program for gdb to debug codes
// gdbsample.c
//
#include        <stdio.h>
#include        <stdlib.h>
#include        <string.h>

#define M_SEPARATE      '\t'

int main() {
        char buf[] = {"a1       b2      c3"};
        char *p = buf;
        char *tmp = NULL;

        printf("buf=%s\n",buf);
        while(*p != NULL) {
                tmp = strchr(buf, M_SEPARATE);
                if (tmp != NULL) {
                        *tmp = ',';
                }
                p++;
        }
        printf("buf changed=%s\n",buf);
        return 0;
}

MACROのdebugには、-g3 optionでコンパイルする。

#define M_SEPARATE              '\t'

(gdb) s
17                      tmp = strchr(buf, M_SEPARATE);
(gdb) print M_SEPARATE
$1 = 9 '\t'
(gdb) s
18                      if (tmp != NULL) {
(gdb) print tmp
$2 = 0x7fffffffe329 "\tb2\tc3"
(gdb)

コメント

人気の投稿