在阅读源代码的时候,如果能够通过GDB跟踪,会对代码的理解很有帮助。
1:编译DEBUG版本,以便于GDB跟踪
./configure –help
一列帮助信息,最后会有以下信息:
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<libdir> if you have libraries in a
nonstandarddirectory <lib dir>
CPPFLAGS C/C++ preprocessor flags, e.g.-I<include dir> if you have
headersin a nonstandard directory <include dir>
CXX C++ compiler command
CXXFLAGS C++ compiler flags
CPP C preprocessor
CXXCPP C++ preprocessor
F77 Fortran 77 compilercommand
FFLAGS Fortran 77 compiler flags
CCAS assembler compiler command(defaults to CC)
CCASFLAGS assembler compiler flags(defaults to CFLAGS)
Use these variablesto override the choices made by `configure' or to help
it to findlibraries and programs with nonstandard names/locations.
在linux下编译,以下两个变量:
CFLAGS Ccompiler flags CPPFLAGS
CPPFLAGS C/C++preprocessor flags, e.g. -I<include dir> if you have
headers in a nonstandarddirectory <include dir>
这个两个选项可以让我们添加编译参数,
export CFLAGS=-g
export CPPFLAGS=-g
现在进行配置环境和程序安装:
./configure
make
cd sql
这样,就生成了debug版的mysql
2:让GDB对宏定义的展开
gcc默认编译的时候,gdb调试过程中是不能看到宏定义的,"p 宏名" 会显示错误,如下: (gdb) p CLIENT6_BEGIN_FLG 其中 #define CLIENT6_BEGIN_FLG "<Client6>" 这样gdb调试过程中就很不直观,尤其是很多大型程序中,宏定义比较复杂的时候。 如果需要在gdb中能查看宏定义,gcc编译的时候需要加上 -gdwarf-2和-g3的参数。 下面看一下-gdward-2 和-g3参数的意思。
-g3 参数的意思: man gcc,可以得出如下的解释: -glevel 加了-g3的参数后,gcc编译的时候,会将扩展的debug 信息编译进二进制文件里面,包括宏定义信息。 所以,如果要使用gdb调试二进制文件里面的宏定义信息,这个选项必须开启。
-gdwarf-2 参数的意思: man gcc, 可以得出如下相关的解释: -gdwarf-2 does not accept a concatenated debug level, because GCC used to support an option -gdwarf that meant to generate DWARF是一种应用的比较广泛的elf(可执行和链接格式),目前有dwarf1, dwarf2,dwarf3 3种版本, 其中版本1已经是比较老旧的,基本废弃不用了。 这边的-gdwarf-2意思是使用版本2的格式, 对dwarf格式感兴趣的,可以看一下ibm的开发者文档: http://www.ibm.com/developerworks/cn/opensource/os-debugging/ 里面会有一些解释。
加上相关参数后,编译: gcc -gdwarf-2 -g3 test.c gdb a.out后就能够使用 "p 宏名" 输出宏的内容了 (gdb) p CLIENT6_BEGIN_FLG
需要查看宏定义是如何被展开的,可以使用如下的命令: macro expand macro_name
|
export CFLAGS=-g3 –gdwarf-2
export CPPFLAGS=-g3–gdwarf-2
configure
make
宏定义的查看与展开:
[mysql@vmwarewuzksql]$ gdb mysqld
GNU gdb Red HatLinux (6.5-16.el5rh)
Copyright (C) 2006Free Software Foundation, Inc.
GDB is freesoftware, covered by the GNU General Public License, and you are
welcome to changeit and/or distribute copies of it under certain conditions.
Type "showcopying" to see the conditions.
There is absolutelyno warranty for GDB. Type "showwarranty" for details.
This GDB wasconfigured as "i386-redhat-linux-gnu"...Using host libthread_dblibrary "/lib/libthread_db.so.1".
(gdb) b main
Breakpoint 1 at0x81869b7: file mysqld.cc, line 3344.
(gdb) r
Starting program:/home/mysql/mysql-5.0.37/sql/mysqld
[Thread debuggingusing libthread_db enabled]
[New Thread-1208944944 (LWP 10307)]
[Switching toThread -1208944944 (LWP 10307)]
Breakpoint 1, main(argc=1, argv=0xbfc5cf64) at mysqld.cc:3344
3344 intmain(int argc, char **argv)
(gdb) next
3347 MY_INIT(argv[0]); // init my_sys library &pthreads
(gdb)info macro MY_INIT
Defined at../include/my_sys.h:43
included at /home/mysql/mysql-5.0.37/sql/mysql_priv.h:31
included at /home/mysql/mysql-5.0.37/sql/mysqld.cc:16
#defineMY_INIT(name) ; { my_progname= name; my_init(); }