MySQL编译DEBUG版本

在阅读源代码的时候,如果能够通过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
No symbol "CLIENT6_BEGIN_FLG" in current context.
(gdb)

其中 #define CLIENT6_BEGIN_FLG "<Client6>"

这样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 = "<Client6>"
(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(); }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值