现在的位置: 首页 > 综合 > 正文

gcc编译&&gdb调试

2014年08月24日 ⁄ 综合 ⁄ 共 1889字 ⁄ 字号 评论关闭

环境:ubuntu12.04

目标:初步学习gdb调试

代码程序:

网上的调试代码程序在我的系统环境下会编译报错

错误在  printf("result1: %ld\n", result );
这行代码在网上的例子上是:printf("result1: %d\n", result );
因为result1是long int 所以输出格式应该为%ld

gcc编译

gcc -g a.c -o a

注意gcc默认是在home/user下,要是程序在其他路径下面的时候要带是绝对路径

运行:

./a

chris@ubuntu:~$ gdb a

GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/chris/a...done.
(gdb) l
6        {
7        sum+=i;
8        }    
9      return sum;
10    }
11    
12    main()
13    {
14    int i;
15    long result = 0;
(gdb)
16    for(i=1; i<=100; i++)
17        {
18        result += i;
19        }
20    printf("result1: %ld\n", result );
21    printf("result2: %d\n", func(250));
22    }
(gdb) break 14
Breakpoint 1 at 0x804841a: file a.c, line 14.
(gdb) break func
Breakpoint 2 at 0x80483ea: file a.c, line 4.
(gdb) info b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x0804841a in main at a.c:14
2       breakpoint     keep y   0x080483ea in func at a.c:4
(gdb) run
Starting program: /home/chris/a

Breakpoint 1, main () at a.c:15
15    long result = 0;
(gdb) next
16    for(i=1; i<=100; i++)
(gdb) n
18        result += i;
(gdb) continue
Continuing.
result1: 5050

Breakpoint 2, func (n=250) at a.c:4
4        int sum=0,i;
(gdb) n
5        for(i=0;i<n;i++)
(gdb) print i
$1 = -1207961320
(gdb) n
7        sum+=i;
(gdb) p sum
$2 = 0
(gdb) p i
$3 = 0
(gdb) n
5        for(i=0;i<n;i++)
(gdb) p i
$4 = 0
(gdb) n
7        sum+=i;
(gdb) p sum
$5 = 0
(gdb) bt
#0  func (n=250) at a.c:7
#1  0x08048461 in main () at a.c:21
(gdb) finish
Run till exit from #0  func (n=250) at a.c:7
0x08048461 in main () at a.c:21
21    printf("result2: %d\n", func(250));
Value returned is $6 = 31125
(gdb) continue
Continuing.
result2: 31125
[Inferior 1 (process 2256) exited with code 017]
(gdb)

抱歉!评论已关闭.