How to use CLion to debug the project built by the make tool

How to use Clion to debug a project built with the make tool

Background introduction

CLion only supports the use of Cmake to build projects, but many projects are built using make (such as php core source code/redis source code).

So how to import a make project into CLion and convert it into a CMake build method, so that we can use CLion to read some open source software, and use our familiar little ladybug for breakpoint debugging

Review the relationship between CMake and make

Make is a tool to help build (build is the collective name of a series of operating procedures for compiling and generating executable programs) C/C++ projects.
It can parse the content in the makefile and determine the order of compilation, dependencies, and the
same set according to the rules in the makefile. The content of the makefiles of the source code on different platforms are different, so the makefile on Centos may need to be changed a lot before it can be used on Ubuntu. CMake is responsible for generating makefiles for different platforms.
CMake generates makefiles that depend on one A file called CMakeLists.txt. The content of the file is also some rules that control the compilation order/dependency. CMakeLists.txt can be used across platforms, but makefiles cannot be used across platforms.

Development environment

[sujianhui@nlp ~]$ cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
[sujianhui@nlp ~]$ gcc -v
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
[sujianhui@nlp ~]$ gdb -v
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
[sujianhui@nlp ~]$ make -v
GNU Make 3.82

cmake 用的是CLion套件里自带的
CLion的版本是 clion2020.1

Solution analysis (take php source code as an example)

Before CLion was used, we compiled it like this

wget https://www.php.net/distributions/php-7.1.0.tar.gz
tar -zxvf php-7.1.0.tar.gz
cd php-7.1.0.tar.gz
# 使用官方提供的侦测shell脚本生成makefile
./configure --prefix=/home/sujianhui/php_debug/ --enable-fpm --enable-debug  
make 
make install 
# 终端中使用gdb 进行源码调试
gdb /home/sujianhui/php_debug/bin/php 

My purpose is very simple, I want to use CLion breakpoints to debug the php kernel source code, which is easy to read. Only with a purpose can there be a solution. There are probably two solutions.

  • Compared with the makefile generated by the ./configure command, the CMakeLists.txt is generated by reverse engineering
  • Use the makefile generated by ./configure, no need to use CMake to generate a new makefile

The first way of thinking will be passed away soon. This will not only take a long time, but also easily cause problems.
The second way of thinking is to replace the rule in the makefile generation link in CMakeLists.txt with the use of php source code. The included configure detection script can be generated, which is feasible in principle

Practice

1 CLion -> File -> New CMake Project From Source Open the source directory that you have decompressed. In this step, CLion will create a CMakeLists.txt file in the root directory of the new project, cmake-build-debug folder

2 vim CMakeLists.txt, modified to the following format

cmake_minimum_required(VERSION 3.16)
project(php_7_1_0)

set(CMAKE_CXX_STANDARD 14)

#定义php源码路径,这里根据自己的真实路径来更改
set(PHP_SOURCE /home/sujianhui/CLionProjects/php-7.1.0)
set(PHP_BIN_OUTPUT /home/sujianhui/CLionProjects/output/php7_1/)

#引入php需要的扩展源码,这里也是根据自己需要的来更改
include_directories(${PHP_SOURCE}/main)
include_directories(${PHP_SOURCE}/Zend)
include_directories(${PHP_SOURCE}/sapi)
include_directories(${PHP_SOURCE}/pear)
include_directories(${PHP_SOURCE}/TSRM)
include_directories(${PHP_SOURCE}/ext)
include_directories(${PHP_SOURCE})

# 注意 --enable-debug 必须得加上,否则断点调试会缺少调试符号信息
add_custom_target(makefile COMMAND ./configure --prefix=${PHP_BIN_OUTPUT} --enable-fpm --enable-debug && make 
    	WORKING_DIRECTORY ${PHP_SOURCE})

add_custom_target(learn_extend COMMAND sudo ${PHP_BIN_OUTPUT}bin/phpize && ./configure --with-php-config=${PHP_BIN_OUTPUT}/bin/php-config &&  make
    	WORKING_DIRECTORY ${PHP_SOURCE})

In the above

add_custom_target(makefile COMMAND ./configure --prefix=${PHP_BIN_OUTPUT} --enable-fpm --enable-debug && make
             WORKING_DIRECTORY ${PHP_SOURCE})

The command is realized by executing the ./configure script to generate a makefile, replacing the complicated cmake rule that should have appeared. After configuring this step, you will find that the little ladybug is already lit up

3 Click Edit Configurations on the left side of the small ladybug in the navigation bar above CLion, and select the makefile target under CMake Applications in the pop-up box to configure

# 该参数指定当点击导航栏 run(小三角icon) 按钮时,执行哪一个可执行文件, 这里我选择了build后生成的php,相当于 gdb $(PHP_BIN_OUTPUT)/bin/php
Executable : 选择$(PHP_BIN_OUTPUT)/bin/php                       

# 该参数制定的是当点击导航兰 run 按钮时,传入到可执行程序的参数,相当于在gdb bash 中执行 : r /home/sujianhui/php/1.php (1.php 是随便写的一个php文件)
Programs Arguments : -f /home/sujianhui/php/1.php 

# 该列中指定的是在点击run按钮后,在真正的可执行程序被执行之前,需要执行的操作,默认添加了build操作,即每次run/debug之前,都会强制重新Build一下 ,这个配置看个人需要吧
Before Launch : 去掉 Build 

4 Open sapi/cli/php_cli.c in CLion, set a breakpoint at the beginning of the main function (I am here at line 1192), click the little ladybug, if it is normal, the breakpoint will have taken effect

summary

It is normal to encounter various problems during the configuration process. If you need this thing, you must first understand the idea of ​​the solution, and then follow this idea to solve various problems in the configuration process. For
example, you may encounter Build Multi-core compilation warning problem...

The difference between Build run Debug in CLion navigation column

  • Build: ./configure generates a makefile, the make tool parses the makefile, compiles, installs, and generates a binary executable file.
  • run: Execute the binary file generated by Build. For example, after we compile and install the php source code, we usually see which modules are turned on. At this time, we will execute php -mit in the shell , which is run
  • Debug: Run a process in debug mode, debug is essentially the parent process to detect the child process (for example, gdb is the parent process, and the process debugged by gdb is the child process)

The Setting Path of Debug is: File -> Setting -> Build, Execution, Deployment -> CMake In the pop-up window, here you can set some Debug parameters

Reference

There are pictures in this, my local environment is a bit problem, so there is no screenshot, please refer to
https://blog.csdn.net/weixin_44056915/article/details/102859556

Guess you like

Origin blog.csdn.net/qq_30549099/article/details/105772679