gdb调试高级用法

Linux下进程崩溃时定位源代码位置

 gcc编译器的默认选项设置:

gcc -dumpspecs

gdb 调试打印完整字符串

 

如何在调试内核时,同时可以调试应用程序的做法:

(cskygdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
default_idle () at arch/csky/kernel/process.c:83
83		__asm__ __volatile__(
(cskygdb) hb *0x00065e34           ==》对应应用反汇编处的地址
Hardware assisted breakpoint 1 at 0x65e34
(cskygdb) c
Continuing.

Program received signal SIGTRAP, Trace/breakpoint trap.
0x00065e36 in ?? ()
(cskygdb) disa
disable      disassemble  
(cskygdb) disa
disable      disassemble  
(cskygdb) disassemble 
No function contains program counter for selected frame.
(cskygdb) disassemble $pc,$pc+30    ==》确认地址是否跟反汇编处的地址一样
Dump of assembler code from 0x65e36 to 0x65e54:
=> 0x00065e36:	st	r15, (r0, 24)
   0x00065e38:	st	r8, (r0, 28)
   0x00065e3a:	mov	r8, r0
   0x00065e3c:	st	r2, (r8, 8)
   0x00065e3e:	st	r3, (r8, 12)
   0x00065e40:	st	r4, (r8, 16)
   0x00065e42:	bmaski	r7, 0
   0x00065e44:	st	r7, (r8, 0)
   0x00065e46:	ld	r7, (r8, 12)
   0x00065e48:	bmaski	r6, 8
   0x00065e4a:	andn	r7, r7, r6
   0x00065e4c:	cmpnei	r7, 0
   0x00065e4e:	bt	0x65e5c
   0x00065e50:	ld	r2, (r8, 8)
   0x00065e52:	ld	r3, (r8, 12)
End of assembler dump.
(cskygdb) add-symbol-file /home/yingc/nfs/dvb/out.elf 0x00080e0  ==》0x00080e0是text段的地址
add symbol table from file "/home/yingc/nfs/dvb/out.elf" at
	.text_addr = 0x80e0
(y or n) y
Reading symbols from /home/yingc/nfs/dvb/out.elf...done.
(cskygdb) si
\0x00065e38	23	{
(cskygdb) l
18	
19	#include "module/app_ioctl.h"
20	//#include "module/app_ca_manager.h"
21	
22	status_t app_ioctl(uint32_t id, uint32_t cmd, void * params)
23	{
24	    status_t ret = GXCORE_ERROR;
25	    switch (cmd & (~0xff))
26	    {
27	        case APP_FRONTEND_BASE:
(cskygdb) n




\yingc@yingc:~/work/goxceed-dvbs-hd/test/solution/output$ csky-elf-readelf -S out.elf
There are 28 section headers, starting at offset 0xa84da0:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .init             PROGBITS        000080b4 0000b4 000022 00  AX  0   0  4
  [ 2] .text             PROGBITS        000080e0 0000e0 2f779a 00  AX  0   0 16
  [ 3] .fini             PROGBITS        002ff87c 2f787c 000016 00  AX  0   0  4
  [ 4] .rodata           PROGBITS        002ff8a0 2f78a0 071b60 00   A  0   0 16
  [ 5] .eh_frame         PROGBITS        00371400 369400 0013a8 00   A  0   0  4
  [ 6] .tdata            PROGBITS        003737a8 36a7a8 000004 00 WAT  0   0  4
  [ 7] .tbss             NOBITS          003737ac 36a7ac 000008 00 WAT  0   0  4
  [ 8] .ctors            PROGBITS        003737ac 36a7ac 000008 00  WA  0   0  4
  [ 9] .dtors            PROGBITS        003737b4 36a7b4 000008 00  WA  0   0  4
  [10] .jcr              PROGBITS        003737bc 36a7bc 000004 00  WA  0   0  4
  [11] .got              PROGBITS        003737c0 36a7c0 0000c4 04  WA  0   0  4
  [12] .data             PROGBITS        00373888 36a888 04a8f8 00  WA  0   0  8
  [13] .bss              NOBITS          003be180 3b5180 05a4cc 00  WA  0   0  8
  [14] .comment          PROGBITS        00000000 3b5180 000082 01  MS  0   0  1
  [15] .debug_aranges    PROGBITS        00000000 3b5202 008558 00      0   0  1
  [16] .debug_pubnames   PROGBITS        00000000 3bd75a 01cd87 00      0   0  1
  [17] .debug_info       PROGBITS        00000000 3da4e1 3b54ec 00      0   0  1
  [18] .debug_abbrev     PROGBITS        00000000 78f9cd 03ddac 00      0   0  1
  [19] .debug_line       PROGBITS        00000000 7cd779 19ac68 00      0   0  1
  [20] .debug_frame      PROGBITS        00000000 9683e4 043dc0 00      0   0  4
  [21] .debug_str        PROGBITS        00000000 9ac1a4 06262a 01  MS  0   0  1
  [22] .debug_loc        PROGBITS        00000000 a0e7ce 00e472 00      0   0  1
  [23] .debug_pubtypes   PROGBITS        00000000 a1cc40 061f78 00      0   0  1
  [24] .debug_ranges     PROGBITS        00000000 a7ebb8 0060e8 00      0   0  1
  [25] .shstrtab         STRTAB          00000000 a84ca0 0000fe 00      0   0  1
  [26] .symtab           SYMTAB          00000000 a85200 034710 10     27 7115  4
  [27] .strtab           STRTAB          00000000 ab9910 03845b 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)




259129 00065e34 <app_ioctl>:
259130
259131 #include "module/app_ioctl.h"
259132 //#include "module/app_ca_manager.h"
259133
259134 status_t app_ioctl(uint32_t id, uint32_t cmd, void * params)
259135 {
259136    65e34:▸  25f0      ▸ subi▸   r0, r0, 32
259137    65e36:▸  9f60      ▸ st▸ r15, (r0, 24)
259138    65e38:▸  9870      ▸ st▸ r8, (r0, 28)
259139    65e3a:▸  1208      ▸ mov▸r8, r0
259140    65e3c:▸  9228      ▸ st▸ r2, (r8, 8)
259141    65e3e:▸  9338      ▸ st▸ r3, (r8, 12)
259142    65e40:▸  9448      ▸ st▸ r4, (r8, 16)
259143     status_t ret = GXCORE_ERROR;
259144    65e42:▸  2c07      ▸ bmaski▸ r7, 0
259145    65e44:▸  9708      ▸ st▸ r7, (r8, 0)
259146     switch (cmd & (~0xff))
259147    65e46:▸  8738      ▸ ld▸ r7, (r8, 12)
259148    65e48:▸  2c86      ▸ bmaski▸ r6, 8
259149    65e4a:▸  1f67      ▸ andn▸   r7, r7, r6
259150    65e4c:▸  2a07      ▸ cmpnei▸ r7, 0
259151    65e4e:▸  e006      ▸ bt▸ 0x65e5c
259152     {
259153         case APP_FRONTEND_BASE:
259154             ret = nim_ioctl(id, cmd, params);


注意:hb 的个数有限制,即使disable的话,也可能会不响应下一个

Unable to handle kernel paging request at virtual address 00000000, epc == 90164fc4
。。。。。。
[   54.940000] CURRENT PROCESS:
[   54.940000] 
[   54.950000] COMM=out.elf PID=922
[   54.950000] TEXT=00008000-0029e630 DATA=0029f630-0031d180 BSS=0031d180-0041e000
[   54.960000] USER-STACK=7faa2e80  KERNEL-STACK=904ef560
[   54.960000] 
[   54.960000] PC: 0x90164fc4
[   54.970000] PSR: 0x8f150141
[   54.970000] r1:  0x00000030    syscallr2: 0x0000e190    r2: 0x0000e190    r3: 0x00000000
[   54.980000] r4:  0x9025908c    r5:  0xe470e190    r6:  0xe40e0020    r7:  0x9023b08c
[   54.990000] r8:  0x00000000    r9:  0x92cb4000    r10: 0x00001101    r11: 0x00000000
[   54.990000] r12: 0x902e6ef4    r13: 0x00043800    r14: 0x9023b08c    r15: 0x90162154
[   55.000000] 
[   55.000000] CODE:
[   55.010000] 90164fa4: 97361c47 75737e87 1ce41254 f00e12e7 
[   55.010000] 90164fb4: 3d06c607 1e26c217 3d05c527 1e25c237 
[   55.020000] 90164fc4: 12359503 96052035 20732077 e7f00f47 
[   55.020000] 90164fd4: 12236002 77637f7c 747b8607 75661c46 
[   55.030000] 
[   55.030000] KERNEL STACK:
[   55.030000] 92cb5dc0: 00000000 92cb5e54 000000a0 00000000 
[   55.040000] 92cb5dd0: 0000011c 0000003b 902dd310 90029f84 
Unable to handle kernel paging request at virtual address 00000000, epc == c07055f0

[root@gx3201 /]# lsmod
ohci_hcd 15264 0 - Live 0xc0074000
ehci_hcd 25520 0 - Live 0xc006c000
av 1121948 2 - Live 0xc06e2000

(cskygdb) p /x 0xc07055f0-0xc06e2000
$2 = 0x235f0

av.ko 反汇编:

105593 RetCode VPU_Init(PhysicalAddress workBuf,VirtualAddress Virtual_addr)
105594    235d6:   7547        lrw r5, 0x1E000
105595    235d8:   1254        mov r4, r5
105596    235da:   1ce4        addu    r4, r4, r14
105597     disFlagRegAddr[0] = BIT_FRM_DIS_FLG_0;
105598     disFlagRegAddr[1] = BIT_FRM_DIS_FLG_1;
105599     disFlagRegAddr[2] = BIT_FRM_DIS_FLG_2;
105600     disFlagRegAddr[3] = BIT_FRM_DIS_FLG_3;
105601 
105602     for (i = 0; i < sizeof(bit_code) / sizeof(bit_code[0]); i += 4)
105603    235dc:   12e7        mov r7, r14
105604    235de:   f00e        br  0x235fc
105605     {
105606         dataH = (bit_code[i+0] << 16) | bit_code[i+1];
105607    235e0:   c607        ldh r6, (r7, 0)
105608    235e2:   3d06        lsli    r6, r6, 16
105609    235e4:   c217        ldh r2, (r7, 2)
105610    235e6:   1e26        or  r6, r6, r2
105611         dataL = (bit_code[i+2] << 16) | bit_code[i+3];
105612    235e8:   c527        ldh r5, (r7, 4)
105613    235ea:   3d05        lsli    r5, r5, 16
105614    235ec:   c237        ldh r2, (r7, 6)
105615    235ee:   1e25        or  r5, r5, r2
105616         VpuWriteMem(vir_code_buf+i*2, dataL);
105617    235f0:   9503        st  r5, (r3, 0)
105618 int VPU_IsBusy()
105619 {
105620     return VpuReadReg(BIT_BUSY_FLAG) != 0;
105621 }
105622 

  79     for (i = 0; i < sizeof(bit_code) / sizeof(bit_code[0]); i += 4)
  80     {
  81         dataH = (bit_code[i+0] << 16) | bit_code[i+1];
  82         dataL = (bit_code[i+2] << 16) | bit_code[i+3];
  83         VpuWriteMem(vir_code_buf+i*2, dataL);
  84         VpuWriteMem(vir_code_buf+i*2+4, dataH);
  85     }

 

gdb dump内存中的数据及查看代码段是否被冲:

(cskygdb) dump memory 111.yuv 0x94f54cc8 0x94f54cc8 + 0x450*200
(cskygdb) p 0x450
$7 = 1104


display -size 1104x200 111.yuv



Loading section .text, size 0x64b628 lma 0x90000000
    section progress: 100.0%, total progress: 100.0%
Loading section .devtab, size 0xa28 lma 0x9064b628
    section progress: 100.0%, total progress: 100.0%
Start address 0x90000458, load size 6602832
Transfer rate: 853 KB/sec, 4093 bytes/write.
0x90000458 in __reset_vector ()
(cskygdb) 46edfc
Undefined command: "46edfc".  Try "help".
(cskygdb) dump binary memory text.ok 0x90000000 0x90000000+0x46edfc
(cskygdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x900e99ce in idle_thread_main (data=0)
    at /home/yingc/work/gitcode/v1.9-dev/gxcoreapi/ecos3.0/packages/kernel/v3_0/src/common/thread.cxx:1244
1244            HAL_IDLE_THREAD_ACTION(idle_thread_loops[CYG_KERNEL_CPU_THIS()]);
(cskygdb) dump binary memory text1.ok 0x90000000 0x90000000+0x46edfc
(cskygdb) c



 21 SECTIONS
 22 {
 23
 24     .text : {
 25             _start_text = .;
 26             . = ALIGN (0x4);
 27         . = ALIGN (0x4); *(.rel.*) *(.rela.*)
 28         . = ALIGN (0x4); _stext = .; _etext = .; _ftext = . ; *(.text*) *(.stub)
 29
 30         . = ALIGN (0x4);
 31                                 _sdata = . ;
 32                                 __data_start = . ;
 33                                 data_start = . ;
 34                                 *(.got_plt)
 35                                 *(.got)
 36                                 FILL(0);
 37                                 . = ALIGN(0x20) ;
 38                                 LONG(-1)
 39                 . = ALIGN(0x20) ;
 40                 *(.rodata)
 41                 *(.rodata*)
 42                 *(.rodata1)
 43                 *(.rodata.*)
 44                 *(.rodata.str1.4)
 45                 *(.gnu.linkonce.r*)
 46                 *(.data)
 47                 *(.data*)


16434 9046ec14 T yaffs_ECCCalculateOther
16435 9046ecbc T yaffs_ECCCorrectOther
16436 9046edfc T __data_start
16437 9046edfc T _sdata
16438 9046edfc T data_start
16439 90473fc4 t __FUNCTION__.11312
16440 90473fd8 t __FUNCTION__.11303
16441 90473fec t __FUNCTION__.11293

ecos3.0/packages/hal/gx/ckmmu/ck610m/var/v3_0/src/ckmmu_ck610m.ld

 gdb调试内核.gdbinit文件的写法:

  1 tar jtag jtag://127.0.1.1:1025
  2 handle SIG32 nostop noprint
  3 handle SIG33 nostop noprint
  4 #set $cr18=0x7d
  5 #set *0x0030a178 |= 0x7
  6 #set *0x0030a178 |= 0x3
  7 #set *0x0030a178 |= (1<<4)|(1<<5)
  8 
  9 load
 10 #b main
 11 #display /i $pc
 12 #b IMG.c:1244
 13 #b widget.c:968
 14 
 15 #c
 16 #q

 编译中间结果:

 2155  make ARCH=csky CROSS_COMPILE=csky-linux- uImage -j4
 2156  make ARCH=csky CROSS_COMPILE=csky-linux- uImage -j4 V=1
 2157  csky-linux-gcc -Wp,-MD,net/sunrpc/.rpcb_clnt.o.d  -nostdinc -isystem /usr/local/csky-linux/bin/../lib/gcc/csky-linux/4.5.1/include -D__KERNEL__ -Iinclude  -I/home/yingc/gcyin/vgitcode/3211-sync/linux-2.6.27.55/arch/csky/include -include include/linux/autoconf.h -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -fno-delete-null-pointer-checks -O0 -fsigned-char -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -fno-delete-null-pointer-checks -O0 -I -pipe -DNO_FPU -D__ELF__ -DMAGIC_ROM_PTR -D__linux__ -DNO_TEXT_SECTIONS -Wa,-no-jsri2bsr,-mcpu=ck610f -Iarch/csky/gx3xxx/include -Wframe-larger-than=1024 -fno-stack-protector -fomit-frame-pointer -g -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(rpcb_clnt)"  -D"KBUILD_MODNAME=KBUILD_STR(sunrpc)" -c -o net/sunrpc/rpcb_clnt.o net/sunrpc/rpcb_clnt.c
 2158  csky-linux-gcc -Wp,-MD,net/sunrpc/.rpcb_clnt.o.d  -nostdinc -isystem /usr/local/csky-linux/bin/../lib/gcc/csky-linux/4.5.1/include -D__KERNEL__ -Iinclude  -I/home/yingc/gcyin/vgitcode/3211-sync/linux-2.6.27.55/arch/csky/include -include include/linux/autoconf.h -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -fno-delete-null-pointer-checks -O0 -fsigned-char -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -fno-delete-null-pointer-checks -O0 -I -pipe -DNO_FPU -D__ELF__ -DMAGIC_ROM_PTR -D__linux__ -DNO_TEXT_SECTIONS -Wa,-no-jsri2bsr,-mcpu=ck610f -Iarch/csky/gx3xxx/include -Wframe-larger-than=1024 -fno-stack-protector -fomit-frame-pointer -g -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(rpcb_clnt)"  -D"KBUILD_MODNAME=KBUILD_STR(sunrpc)" -c -o net/sunrpc/rpcb_clnt.o net/sunrpc/rpcb_clnt.c --save-temps
 2159  vi rpcb_clnt.i
 2160  vi net/sunrpc/rpcb_clnt.c +122 rpcb_clnt.i -O
 2161  csky-linux-gcc -Wp,-MD,net/sunrpc/.rpcb_clnt.o.d  -nostdinc -isystem /usr/local/csky-linux/bin/../lib/gcc/csky-linux/4.5.1/include -D__KERNEL__ -Iinclude  -I/home/yingc/gcyin/vgitcode/3211-sync/linux-2.6.27.55/arch/csky/include -include include/linux/autoconf.h -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -fno-delete-null-pointer-checks -O0 -fsigned-char -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -fno-delete-null-pointer-checks -O2 -I -pipe -DNO_FPU -D__ELF__ -DMAGIC_ROM_PTR -D__linux__ -DNO_TEXT_SECTIONS -Wa,-no-jsri2bsr,-mcpu=ck610f -Iarch/csky/gx3xxx/include -Wframe-larger-than=1024 -fno-stack-protector -fomit-frame-pointer -g -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(rpcb_clnt)"  -D"KBUILD_MODNAME=KBUILD_STR(sunrpc)" -c -o net/sunrpc/rpcb_clnt.o net/sunrpc/rpcb_clnt.c --save-temps
 2162  vi rpcb_clnt.i
 2163  history 

 

 

linux下用GDB调试可加载模块

http://blog.csdn.net/justlinux2010/article/details/8508333

[root@gx6602 /]# lsmod
ohci_hcd 19028 0 - Live 0xc009e000
ehci_hcd 33276 0 - Live 0xc00b9000
fe 221368 0 - Live 0xc03c7000
av 1933852 0 - Live 0xc0b7f000

 

yingc@yingc:~/nfs/lib/modules/2.6.27.55$ csky-linux-objdump -h av.ko

av.ko:     file format elf32-csky-little

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .note.gnu.build-id 00000024  00000000  00000000  00000034  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .text         000c1824  00000000  00000000  00000058  2**2
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  2 .init.text    000001e8  00000000  00000000  000c187c  2**2

(cskygdb) add-symbol-file ~/nfs/lib/modules/2.6.27.55/av.ko
The address where /home/yingc/nfs/lib/modules/2.6.27.55/av.ko has been loaded is missing
(cskygdb) add-symbol-file ~/nfs/lib/modules/2.6.27.55/av.ko 0xc0b7f058
add symbol table from file "/home/yingc/nfs/lib/modules/2.6.27.55/av.ko" at
    .text_addr = 0xc0b7f058
(y or n) y
Reading symbols from /home/yingc/nfs/lib/modules/2.6.27.55/av.ko...done.
(cskygdb) b gx3201_hdmi_open

 

yingc@yingc:~/tmp$ csky-linux-gcc -O0 -g -I/usr/local/csky-linux -I/usr/local/csky-linux/csky-linux -I/usr/local/csky-linux/csky-linux/libc/usr/include  -L/usr/local/csky-linux/csky-linux/lib/libstdc++.a  thread.c -L/usr/local/csky-linux/libc/usr/lib/libpthread.a  -static
/tmp/ccCOd8La.o: In function `main':
/home/yingc/tmp/thread.c:31: undefined reference to `pthread_attr_init'
/home/yingc/tmp/thread.c:33: undefined reference to `pthread_attr_setstacksize'
/home/yingc/tmp/thread.c:35: undefined reference to `pthread_create'
/home/yingc/tmp/thread.c:41: undefined reference to `pthread_attr_destroy'
/tmp/ccCOd8La.o: In function `main':
thread.c:(.text+0x90): undefined reference to `pthread_attr_init'
thread.c:(.text+0x94): undefined reference to `pthread_attr_setstacksize'
thread.c:(.text+0x9c): undefined reference to `pthread_create'
thread.c:(.text+0xa8): undefined reference to `pthread_attr_destroy'
collect2: ld returned 1 exit status
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ csky-linux-gcc -O0 -g -I/usr/local/csky-linux -I/usr/local/csky-linux/csky-linux -I/usr/local/csky-linux/csky-linux/libc/usr/include  -L/usr/local/csky-linux/csky-linux/lib/libstdc++.a  thread.c -L/usr/local/csky-linux/libc/usr/lib lpthread  -static
csky-linux-gcc: lpthread: 没有那个文件或目录
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ csky-linux-gcc -O0 -g -I/usr/local/csky-linux -I/usr/local/csky-linux/csky-linux -I/usr/local/csky-linux/csky-linux/libc/usr/include  -L/usr/local/csky-linux/csky-linux/lib/libstdc++.a  thread.c -L/usr/local/csky-linux/libc/usr/lib -lpthread  -static
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ 
yingc@yingc:~/tmp$ csky-linux-gcc -O0 -g -I/usr/local/csky-linux -I/usr/local/csky-linux/csky-linux -I/usr/local/csky-linux/csky-linux/libc/usr/include  -L/usr/local/csky-linux/csky-linux/lib/libstdc++.a  thread.c -L/usr/local/csky-linux/libc/usr/lib -lpthread  -static

 

hello world的本质-----预编译,编译,汇编

gcc -E hello.c -o hello.i

这个命令就是用来生成预处理后的文件的。
生成汇编代码文件的命令
gcc -S hello.i -o hello.s
汇编命令
gcc -c hello.s -o hello.o

 

 

 

aa

posted @ 2014-11-27 10:26  静之深  阅读(11365)  评论(0编辑  收藏  举报