使用-g选项进行编译
gcc -g 1.c
gcc -v 查看版本
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
show version 查看gdb版本
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
#include<stdio.h>
int foo(int);
void bar(int);
int main(void)
{
int a;
char *s ="hello, world";
printf("%s\n",&s[7]);
a=5;
foo(a);
return 0;
}
int foo(int n){
int b;
b=n;
b*=2;
bar(b);
return b;
}
void bar(int m){
printf("Hi, I'am bar!\n");
}
D:\Codes\test>gdb a.exe
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
Reading symbols from a.exe...done.
(gdb) break main
Breakpoint 1 at 0x40155d: file 1.c, line 7.
(gdb) run
Starting program: D:\Codes\test\a.exe
[New Thread 8612.0x1cb0]
[New Thread 8612.0x15ac]
Thread 1 hit Breakpoint 1, main () at 1.c:7
warning: Source file is more recent than executable.
7 char *s ="hello, world";
(gdb) print s
$1 = 0x10 <error: Cannot access memory at address 0x10>
(gdb) print a
$2 = 0
(gdb) n
8 printf("%s\n",&s[7]);
(gdb) print s
$3 = 0x404000 "hello, world"
(gdb) print s+13
$4 = 0x40400d "Hi, I'am bar!"
(gdb) n
world
9 a=5;
(gdb) print a
$5 = 0
(gdb) n
10 foo(a);
(gdb) print a
$6 = 5
(gdb) step
foo (n=5) at 1.c:15
15 b=n;
(gdb) print b
$7 = 0
(gdb) n
16 b*=2;
(gdb) n
17 bar(b);
(gdb) step
bar (m=10) at 1.c:21
21 printf("Hi, I'am bar!\n");
(gdb) print m
$10 = 10
(gdb) n
Hi, I'am bar!
22 }
(gdb) continue
Continuing.
[Thread 8612.0x15ac exited with code 0]
[Inferior 1 (process 8612) exited normally]
(gdb)
网友评论