bitsCN.com
一、前言
在规模稍微大点的项目中,为了方便快速找到bug的所在,我们往往需要在代码中加入一些调试用的代码,比如加入一些printf,打印出一些重点的信息;加入assert,进行断言判断。这些比较随意的调试代码会使整个系统显得比较凌乱。于是Fred Fish开发了一套用于嵌入代码中的库,开发人员只需要调用相应的函数即可加入调试信息。
对于MySQL这种多线程的程序来说,调试也是比较困难的,MySQL选择了DBUG作为其调试代码(在原始代码的基础上进行了略微的改造),充分说明了DBUG的实用性,本文就DBUG的原始代码进行介绍。
二、DBUG下载与使用
这里以linux下的开发进行介绍。
首先需要下载DBUG源码,下载地址:http://sourceforge.net/projects/dbug/
解压后在dbug/src下即是debug所需的源码文件:dbug.h dbug.c
dbug/example.c是一个简单的例子,源码如下:
/* * This file is Public Domain * * Just short example, how Fred Fish's dbug package should be used * * Tonu Samuel <tonu> * */#include "dbug.h"static int sub1 (void);static void sub2 (char *arg);static intsub1 (void){ DBUG_ENTER ("sub1"); sub2 ("Hello world!"); sub2 ("Hello earth!"); sub2 ("Hello programmer!"); DBUG_RETURN (0);}static voidsub2 (char *arg){ DBUG_ENTER ("sub2"); DBUG_PRINT ("info", ("Got argument: '%s'", arg)); printf ("%s/n", arg); DBUG_VOID_RETURN;}intmain (void){ int ret = 0; DBUG_PUSH ("d:t:O"); DBUG_PROCESS ("example"); ret = sub1 (); DBUG_PRINT ("info", ("Returned value: %d", ret)); return 0;}</tonu>
我们将example.c放到src目录下,然后编译example文件:
dbug/src$ gcc -Wall -g dbug.c example.c -o example
运行example输出如下:
>sub1| >sub2| | info: Got argument: 'Hello world!'Hello world!| <sub2>sub2| | info: Got argument: 'Hello earth!'Hello earth!| <sub2>sub2| | info: Got argument: 'Hello programmer!'Hello programmer!| <sub2 pre="">< value: Returned info: <sub1></sub1></sub2></sub2></sub2>
结合源码,我们通过打印出来的信息,可以很容易的看出调试信息所表达的意思:
函数sub1调用了三次sub2,三次的参数分别是'Hello world!' ,'Hello earth!' ,'Hello programmer!' ,最后return 0.
下面初略介绍下上面用到的几个宏定义:
DBUG_PUSH ("d:t:O"); 设置调试的启动参数,参数列表在MySQL手册上也有:
标记 | 描述 |
d |
允许对当前状态从DBUG_ |
D |
在每个调试起输出行后延迟。参量一个十分之一秒为单位来延迟的数,它受限于机器的能 |
f |
限制调试和/或跟踪,以及简单设定于列出名字的函数。注意,空列将禁止所用函数。应 |
F | 对调试或跟踪输出的每一行识别源文件名。 |
i | 对调试或跟踪输出的每一行用PID或线程ID识别进程。 |
g |
允许解析,创建名为的dbugmon.out文件,它包含可用来简单设定程序的信息。可能跟着 |
L | 为调试或跟踪输出的每一行识别源文件行号。 |
n | 为调试或跟踪输出的每一行打印当前函数嵌套深度。 |
N | 给调试输出的每一行编号。 |
o | 重定向调试器输出流到指定文件。默认输出是stderr 文件。 |
O |
类似于 o,但是文件在每次写操作之间被冲刷。当需要之时,文件在每次写操作之间被关 |
p |
限制调试器作用于指定进程。为使调试器动作,一个进程必须用DBUG_PROCESS宏来识 |
P | 为调试或跟踪输出的每一行打印当前进程名字。 |
r |
当推出一个新状态时,不继承前状态的操作嵌套深度级别。当输出在左边空白开始时有 |
S |
在每个调试过的函数做_sanity(_file_,_line_)函数直到 _sanity() 返回不同于0的结果。(大多 |
t |
允许函数调用/退出跟踪行。可能跟着一个给出最大跟踪级别的数字列(只含一个修改 |
DBUG_PROCESS ("example"); 设置进程名为example
DBUG_ENTER ("sub1"); 表示进入函数sub1
DBUG_PRINT ("info", ("Got argument: '%s'", arg)); 和printf效果差不多,打印出需要的调试信息。
DBUG_RETURN (0); 在return 0 的基础上,打印出return 0的调试信息。
三、小结
本文简单介绍了DBUG的使用方法,没有深究,源码很少,却很实用,在源码的注释部分,我看到了是Fred Fish1987年写的,呃,我才刚出生....,没想到现在还在看那时候的代码,可能这就是所谓的经典吧。
兄弟们,你们还在为复杂环境下的bug调试而苦恼么?还在为堆栈被写乱掉无从下手而凌乱么?淡定吧,一起来用DBUG吧~~
bitsCN.com
MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
