gdb 脚本内容如下:
b dispatch_command commands ? ? print command ? ? continue end
执行 gdb 脚本,然后使用 mysql
客户端连接数据库,并执行 SQL 语句操作,即可查看到 gdb 调试信息。
(gdb) b dispatch_command Breakpoint 3 at 0x555558ddb37c: file /home/kyc/mysql8/sql/sql_parse.cc, line 1581. (gdb) commands Type commands for breakpoint(s) 3, one per line. End with a line saying just "end". >print command >continue >end (gdb) c Continuing. [Switching to Thread 0x7fffe01fc700 (LWP 5941)] Thread 49 "mysqld" hit Breakpoint 3, dispatch_command (thd=0x7fff4c000f70, com_data=0x7fffe01fbba0, command=COM_QUERY) at /home/kyc/galaxyengine/sql/sql_parse.cc:1581 1581 ? ? ? ? ? ? ? ? ? ? ? ? ?enum enum_server_command command) { $4 = COM_QUERY
三、使用 Trace 文件调试
MySQL 的 debug
版提供了一个专门的 DBUG 包[3]。通过这个 DBUG 包,可获取正在执行操作程序的 Trace 文件。
通过控制 DBUG 开关,可以将 MySQL 的任何操作,以及所涉及的调用模块、函数、状态信息记录在 Trace 文件中。
设置 debug 参数
通过设置 debug 参数选项,指定跟踪方式。
--debug [ = debug_options ]
[ = debug _ options ] 可识别字符 d、t、i 、o 等。
Debug 示例
若需获取代码中DBUG_PRINT
("info:" 打印的日志,可以使用 MySQL 客户端连上服务器,并执行如下命令,开启 debug 参数。
set debug = 'd,info'; use test;
看 mysqld.trace
文件,可获取 use test 在 MySQL 中的执行流程。
do_command: info: Command on socket (46) = 3 (Query) do_command: info: packet: ' ? ? ? ? ? ? ? ? '; command: 3 dispatch_command: info: command: 3 gtid_pre_statement_checks: info: gtid_next->type=0 owned_gtid.{sidno,gno}={0,0} THD::is_ddl_gtid_compatible: info: SQLCOM_CREATE:0 CREATE-TMP:0 SELECT:1 SQLCOM_DROP:0 DROP-TMP:0 trx:0 SELECT_LEX::prepare: info: setup_ref_array this 0x7fff1400d298 ? ?3 : ? ?0 ? ?0 ? ?1 ? ?2 ? ?0 ? ?0 setup_fields: info: thd->mark_used_columns: 1 setup_fields: info: thd->mark_used_columns: 1 SELECT_LEX::setup_conds: info: thd->mark_used_columns: 1 THD::decide_logging_format: info: query: SELECT DATABASE() THD::decide_logging_format: info: variables.binlog_format: 2 ................ MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff14019ae0 MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff1412dd20 MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff1412dcc0 net_send_ok: info: affected_rows: 0 ?id: 0 ?status: 2 ?warning_count: 0 net_send_ok: info: OK sent, so no more error sending allowed
本文使用几个简单的示例,演示了 MySQL 内核的 Debug 的几种常见方法。当然,仅仅起到抛砖引玉的作用,更多好玩的技巧,还需读者自行深度挖掘。
参考:
[2]: GDB 脚本调试:https://sourceware.org/gdb/current/onlinedocs/gdb/Commands.html#Commands
[3]: DBUG Package[:https://dev.mysql.com/doc/refman/8.0/en/dbug-package.html
更多相关Mysql内容来自木庄网络博客
标签:Mysql
相关阅读 >>
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。