gdb 调试动态库

原文链接

cat get.h

int   get   (); 
int   set   (int   a); 

 

cat get.c

#include   <stdio.h> 
#include   "get.h" 

static   int   x=0; 
int   get   () 
{ 
                printf   ( "get   x=%d\n ",   x); 
                return   x; 
} 
int   set   (int   a) 
{ 
                printf   ( "set   a=%d\n ",   a); 
                x   =   a; 
                return   x; 
} 

 

cat main.c

#include   <stdio.h> 
#include   "get.h " 
int   main   (int   argc,   char**   argv) 
{ 
                int   a   =   100; 
                int   b   =   get   (); 
                int   c   =   set   (a); 
                int   d   =   get   (); 

                printf   ( "a=%d,b=%d,c=%d,d=%d\n ",a,b,c,d); 
                return   0; 
} 

 

 

 cat   ./makefile 

all:main 
libget.so:get.c 
                gcc   -g   -c   -o   get.o   get.c 
                gcc   -g   -shared   -o   libget.so   get.o 

main:main.c   libget.so 
                gcc   -g   -c   -o   main.o   main.c 
                gcc   -g   -o   main   main.o   -L./   -lget 

clean: 
                @rm   libget.so   get.o   main.o   main 

 

5)用GDB调试(step   in) 

>   gdb   ./main 

(gdb)   break   main 
Breakpoint   1   at   0x80484d0:   file   main.c,   line   5. 
(gdb)   r 
Starting   program:   /home/hchen/test/so/main 

Breakpoint   1,   main   (argc=1,   argv=0xbfe62fc4)   at   main.c:5 
5                               int   a   =   100; 
(gdb)   n 
6                               int   b   =   get   (); 
(gdb)   s         <-------   进入动态库 
get   ()   at   get.c:7 
7                               printf   ( "get   x=%d\n ",   x); 
(gdb)   s 
get   x=0 
8                               return   x; 

6)用GDB调试(break   set) 
(gdb)   break   set 
Function   "set "   not   defined. 
Make   breakpoint   pending   on   future   shared   library   load?   (y   or   [n])   y 

Breakpoint   1   (set)   pending. 

 

posted @ 2012-06-26 01:11  wangkangluo1  阅读(4552)  评论(0编辑  收藏  举报