在阅读源代码的时候,如果能够通过GDB跟踪,会对代码的理解很有帮助。
1:编译DEBUG版本,以便于GDB跟踪
./configure –help
一列帮助信息,最后会有以下信息:
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L if you have libraries in a
nonstandarddirectory
CPPFLAGS C/C++ preprocessor flags, e.g.-I if you have
headersin a nonstandard directory
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 if you have
headers in a nonstandarddirectory
这个两个选项可以让我们添加编译参数,
export CFLAGS=-g
export CPPFLAGS=-g
现在进行配置环境和程序安装:
./configure
make
cd sql
这样,就生成了debug版的mysql
2:让GDB对宏定义的展开
gcc默认编译的时候,gdb调试过程中是不能看到宏定义的,"p宏名" 会显示错误,如下:
(gdb) p CLIENT6_BEGIN_FLG
No symbol "CLIENT6_BEGIN_FLG" in current context.
(gdb)
其中#define CLIENT6_BEGIN_FLG ""
这样gdb调试过程中就很不直观,尤其是很多大型程序中,宏定义比较复杂的时候。
如果需要在gdb中能查看宏定义,gcc编译的时候需要加上-gdwarf-2和-g3的参数。
下面看一下-gdward-2和-g3参数的意思。
-g3参数的意思:
man gcc,可以得出如下的解释:
-glevel
-ggdblevel
-gstabslevel
-gcofflevel
-gxcofflevel
-gvmslevel
Request debugging information and also use level to specify how much information. The default level is 2.
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don’t plan to debug.
This includes descriptions of functions and external variables, but no information about local variables and no line numbers.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro
expansion when you use -g3.
加了-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
debug information in version 1 of the DWARF format (which is very different from version 2), and it would have been too con-
fusing. That debug format is long obsolete, but the option cannot be changed now. Instead use an additional -glevel option
to change the debug level for DWARF2.
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
$1 = ""
(gdb)
需要查看宏定义是如何被展开的,可以使用如下的命令:
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(); }