JIYIK CN >

Current Location:Home > Learning > DATABASE > Redis >

Mac system uses clion to remotely debug redis4 source code

Author:JIYIK Last Updated:2025/04/09 Views:

The remote host uses the Linux system.

The first step is definitely to establish a code synchronization mechanism on the local and remote hosts - sftp is the first choice.

The second step is to write the CMakeLists.txt file of redis4. There is a complete CMakeLists.txt file in the Mac system clion debugging redis4 source code . You can use it, so I won’t write it here.

The third step is to build the project on the remote host and compile it with make, and then usegdbserver :port src/redis-server

The last step is to create a new one in clion gdb remote debug, which is also introduced in other notes, so I won’t go into details here.

The main thing that needs to be explained is the third step. Various problems will arise during building and compiling.

  1. cmake version, because we specified the minimum version of cmake required in CMakeLists.txt. By default, the cmake version in the Linux remote host may be lower, so you need to upgrade the cmake version.
  2. After the cmake project is built, the following error occurs when using make to compile
$ make
......
/root/workspace/c/redis4/src/redis-cli.c:368:5: 错误:只允许在 C99 模式下使用‘for’循环初始化声明
     for (size_t j = 0; j < reply->elements; j++) {
     ^
/root/workspace/c/redis4/src/redis-cli.c:368:5: 附注:使用 -std=c99 或 -std=gnu99 来编译您的代码
/root/workspace/c/redis4/src/redis-cli.c: 在函数‘LRUTestMode’中:
/root/workspace/c/redis4/src/redis-cli.c:2729:17: 错误:只允许在 C99 模式下使用‘for’循环初始化声明
                 for (int i = 0; i < 5; i++) val[i] = 'A'+rand()%('z'-'A');
                 ^
make[2]: *** [CMakeFiles/redis-cli.dir/src/redis-cli.c.o] 错误 1
make[1]: *** [CMakeFiles/redis-cli.dir/all] 错误 2
make: *** [all] 错误 2

The reason for this is that the default C standard in Linux does not allow forvariables to be declared directly in a loop. C99/gnu99It is only allowed in the standard. So you need to specify C99the standard.

Modify redis4/CMakeLists.txtthe file to the following content

cmake_minimum_required(VERSION 3.15)
project(redis4_1)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_C_FLAGS "-std=c99")  ### 新增
......

Add a new line of code, and keep the rest unchanged.

$ make
[ 93%] Building C object CMakeFiles/redis-server.dir/src/sparkline.c.o
[ 94%] Linking C executable src/redis-server
CMakeFiles/redis-server.dir/src/module.c.o:在函数‘moduleLoad’中:
/root/workspace/c/redis4/src/module.c:3935:对‘dlopen’未定义的引用
/root/workspace/c/redis4/src/module.c:3937:对‘dlerror’未定义的引用
/root/workspace/c/redis4/src/module.c:3940:对‘dlsym’未定义的引用
/root/workspace/c/redis4/src/module.c:3952:对‘dlclose’未定义的引用
CMakeFiles/redis-server.dir/src/module.c.o:在函数‘moduleUnload’中:
/root/workspace/c/redis4/src/module.c:3994:对‘dlclose’未定义的引用
/root/workspace/c/redis4/src/module.c:3995:对‘dlerror’未定义的引用
CMakeFiles/redis-server.dir/src/debug.c.o:在函数‘dumpX86Calls’中:
/root/workspace/c/redis4/src/debug.c:1011:对‘dladdr’未定义的引用
CMakeFiles/redis-server.dir/src/debug.c.o:在函数‘sigsegvHandler’中:
/root/workspace/c/redis4/src/debug.c:1078:对‘dladdr’未定义的引用
collect2: 错误:ld 返回 1
make[2]: *** [src/redis-server] 错误 1
make[1]: *** [CMakeFiles/redis-server.dir/all] 错误 2
make: *** [all] 错误 2

It is found that the for loop problem is solved, but a new problem arises. For this dlxxxx 函数未定义的引用problem, you need to specify it when compiling.-ldl

How to achieve it also requires changing redis4/CMakeLists.txtthe file.

......
add_executable(redis-server ${SRC_SERVER})
target_include_directories(redis-server
        PRIVATE ${REDIS_ROOT}/deps/linenoise
        PRIVATE ${REDIS_ROOT}/deps/hiredis
        PRIVATE ${REDIS_ROOT}/deps/lua/src)
target_link_libraries(redis-server
        PRIVATE pthread
        PRIVATE m
        PRIVATE lua
        PRIVATE linenoise
        PRIVATE hiredis
        -ldl)  ### 新增 -ldl 选项

The rest of the file remains unchanged.

$ rm -f CMakeCache.txt
$ cmake .
$ make
...
[ 97%] Linking C shared library helloblock.so
[ 97%] Built target helloblock
[ 98%] Building C object src/modules/CMakeFiles/hellotype.dir/hellotype.c.o
[ 99%] Linking C shared library hellotype.so
[ 99%] Built target hellotype
[ 99%] Building C object src/modules/CMakeFiles/helloworld.dir/helloworld.c.o
[100%] Linking C shared library helloworld.so
[100%] Built target helloworld

Very good, it worked.

Have fun with debugging!

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Redis password verification command AUTH

Publish Date:2025/04/09 Views:78 Category:Redis

Redis has not made much optimization in terms of security, but has made great efforts in terms of performance and ease of use. A very simple security method for Redis is password verification, which requires the use of the AUTH command. Let

Redis installation method and common problem solving under centos

Publish Date:2025/04/09 Views:158 Category:Redis

In this article, we will introduce how to install redis under CentOS. In fact, the installation steps are very simple, but there may be some problems in the middle, which is worthy of our attention. Let's take a look at how to install it. F

How to use clion to debug redis source code on mac system

Publish Date:2025/04/09 Views:172 Category:Redis

clion mainly uses cmake + make for compilation. So for redis4, the main thing is to write the CMakeLists.txt file first. CmakeLists.txt file redis4/CMakeLists.txt cmake_minimum_required (VERSION 3.15 ) project (redis4) set (CMAKE_BUILD_TYPE

gdbserver 配合 Clion 实现远程调试

Publish Date:2021/07/06 Views:699 Category:编程语言

gdbserver 配合 Clion 实现远程调试,首先使用clion新建项目,并且对其进行设置,从而使其可以和远程机器进行代码同步。项目新建完成,并且也能和远程机器进行同步之后。开始将我们的

如何使用 CLion 开发调试 PHP 扩展

Publish Date:2021/07/02 Views:288 Category:PHP

php 扩展的创建这里就不再赘述,使用ext_skel 生成一个框架,然后编辑相应的文件,编译安装,最后在php.ini 配置文件中加入生成的扩展 例如 my_ext.so

mac系统使用 clion远程调试redis4源码

Publish Date:2021/05/08 Views:209 Category:Redis

本篇介绍在mac系统下使用clion对Redis进行远程源代码调试。主要适用sftp同步代码,gdbserver开启远程调试服务。linux作为redis运行的远程服务器。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial