暂无图片
暂无图片
6
暂无图片
暂无图片
暂无图片

如何在Clion 中调试 repmgrd

clion_repmgrd.jpg
大家好, 今天和大家分享的是 如何在clion 中调试 repmgrd。

最近有篇文章很火,DBA 需不需要懂开发? 个人正好借着这篇文章表达一下观点: 不需要!!!

这里指的开发语言是 C,C++,JAVA这种可以独立作为职业谋生的工作。 (国内现在卷的乌烟瘴气!!!你越卷,待业青年就越多!)

DBA如果是定位于自动化运维的话, shell , python , ansiable 这些主流的脚本型的语言(甚至golang),是必须要掌握的。 我个人认为这些本身是运维的一部分,就像DBA必须要懂SQL一样。

=========================================================================================================================

下面我们切入正题: 调式源码的目的不是为了学习C,是为了理解REPMGR的内部工作原理。
毕竟如果你的公司核心系统采用的这个方案的话, 是很有必要深入的理解的。

打开 clion 编辑器:

Image.png

导入repmgr 的源代码:

Image.png

设置CMAKER的编译路径:

CMake options: 这里需要配置 debug 相关的参数: -ggdb -O0 (十分重要,否则不能debug到源代码)
Build directory: 编译路径
Build options: 并行参数 -j 6
Environment: 配置PATH,PG_HOME, LD_LIBRARY_PATH

Image.png

Build directory: 编译路径
commands: 执行configure命令,生成 Makefile文件

Image.png
编译程序:
实际上就是在clion中执行: configure & make install (GCC 是debug模式的编译)

Image.png

编译完成后,我们可以看到 repmgrd.so 以及repmgr 的可执行文件:

Image.png

接下来, 我们新建一个debug 程序:

Image.png

Name: repmgrd
Executable: 配置到编译后的可执行文件 repmgrd
Program argumens: 配置启动的参数: -f /data/pref6001/repmgr/repmgr.conf --pid-file /data/pref6001/repmgr/repmgrd.pid

Image.png

我们在函数打一个断点程序的入口main 函数: repmgrd.c

Image.png

点击debug 按钮,启动我们的程序:

Image.png

我们可以看到程序已经进入到设置的main 函数:

Image.png

repmgrd 的 主函数main 启动的时候, 会 fork 出来一个后台进程:

Image.png

我们看到后台的进程号是 110567

Image.png

fork 出来的进程 pid = 110567 会退出后,将子进程110568 挂到后台:

Image.png

Image.png

我们可以在clion 中 attach process 110568 , 即可debug 整个repmgrd 的后台程序:

Image.png
我们可以看到debug 已经进入到我们断点中: repmgrd.c -> monitor_streaming_primary(void)

Image.png

Image.png

okay, 到这里我们就配置好了Clion 中 debug repmgr 的源码环境, 接下来就可以愉快的debug 代码了。

总体来说配置还是很简单的, 唯一遇到的一个问题就是GCC 编译的时候 没有加参数 -ggdb 导致可执行程序和源码文件没有建立链接,造成无法断点debug的问题。

错误如下:

Type "apropos word" to search for commands related to "word". No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. No symbol table is loaded. Use the "file" command. [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1".
复制

解决方案参考:https://stackoverflow.com/questions/9245685/gdb-no-symbol-table-is-loaded
https://coderwall.com/p/wyb2ug/how-fix-the-no-symbol-table-is-loaded-gdb-issue-on-mountain-lion

下一期会基于这个环境,会写一篇内部选举工作机制的文章。

Have a fun 🙂 !

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论