Debug MySQL with VS Code

Introduction

MySQL debugging is a basic skill for exploring MySQL source code. I have previously introduced how to use LLDB to debug MySQL , but the command line operation is inconvenient, switching back and forth between the editor and the terminal, and viewing the code is inconvenient. This article will introduce how to use VS Code in Mac OS. Local and remote debugging can greatly improve debugging efficiency.


Visual Studio Code

Visual Studio Code (VS Code for short) is an open source text editor developed by Microsoft and supports Windows, Linux and macOS operating systems. It supports debugging, has built-in Git version control, and also has development environment features such as code completion (similar to IntelliSense), code snippets, code refactoring, and more. The editor supports user-defined configurations, such as changing theme colors, keyboard shortcuts, editor properties, and other parameters. It also supports extensions and has built-in extension management capabilities in the editor.


Install LLDB

LLDB is a part of the LLVM compiler. It is recommended to use Homebrew to install the LLVM toolset. It is not recommended to use the LLDB that comes with the system. Before installation, you must create a certificate or you cannot install it. The steps are as follows:

640?wx_fmt=png

640?wx_fmt=png


Once created, start installing LLVM

brew install llvm --with-python@2 --with-lldb


Install the plugin

VS Code comes with a debug function, here I recommend using the LLDB Debugger plugin.

640?wx_fmt=jpeg


Next , configure the debug parameters for the project.


Configure debugging parameters

Use VS Code to open the MySQL source directory, select the debug column in the sidebar, add the configuration, program input the path of the program to be debugged, here select the path of your compiled mysqld, and args input the parameters required to start the program, usually mysqld is specified. configuration file. This is configured, is not very simple.

640?wx_fmt=jpeg

start debugging

Click the start button. After startup, if no breakpoint is set, mysqld will start normally. If the breakpoint is triggered, it will be displayed as shown in the following figure.

640?wx_fmt=jpeg

The entire debugging window is basically divided into six parts, and all debugging operations are done here:

1:  Display variable information

2:  Set variables to focus on

3:  Display call stack information

4:  Set breakpoint information, you can also set breakpoints before the code line number

5:  The code display area, above is the debug button, including continue/stepover/step in/step out/restart/stop

6:  Debug terminal input and output area


breakpoint setting

Click before the code line number to set a breakpoint on the behavior, or set a breakpoint based on a condition. Take setting ConditionalBreakpoint as an example. When the program starts, it will judge whether to trigger the breakpoint according to the conditional expression you set.

640?wx_fmt=jpeg

Conditional Breakpoint This method is used to trigger a breakpoint when the target variable reaches a certain condition, and the rest will skip and continue execution. For example, when the variable is set equal to the target table name, the breakpoint is triggered, and the rest of the tables are skipped. The relative function name breakpoint saves a lot of manual skipping operations.

640?wx_fmt=jpeg

Remote debugging

If you want to debug MySQL on a remote Linux server, the above method is not suitable, then remote debugging is required. Both lldb and gdb support remote debugging, here we take lldb as an example.

You need to install lldb on the remote host first, use yum to install, the source address is here http://mirror.centos.org/centos/7/sclo/x86_64/rh


remote$ yum install -y llvm-toolset-7


After the installation is complete, start lldb-server


remote$ /opt/rh/llvm-toolset-7/root/usr/bin/lldb-serverplatform --listen "*:9191" --server


Next, add configuration items in the VS Code debugging interface.


{

   "type": "lldb",

   "request": "attach",

   "name": "Remote attach",

   "program": "~/mysql5626/usr/local/mysql/bin/mysqld",

   "pid":"<target_pid>",

   "initCommands": [

        "platform select remote-linux",

        "platform connect connect://<remote_host>:9191"

   ],

   "sourceMap": {

        "/export/home/pb2/build/sb_0-15908961-1436910670.17/mysql-5.6.26": "/Users/hongbin/workbench/mysql-server"

   }

},


program: This machine also copies a copy of the target program and loads it 

pid: fill in the mysqld process id of the remote host 

sourceMap: Fill in the mapping between the code path compiled by mysqld and the path of the native code library, so that the code can be associated with the program when debugging 

Note: Remember to switch the code to the branch consistent with the target program version before debugging


refer to

http://lldb.llvm.org/remote.html

https://github.com/vadimcn/vscode-lldb


 This article is reproduced from the public account: Aikesheng Cloud Database

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326728931&siteId=291194637