Collecting MySQL Trace Data
This section uses the MySQL source code as an example to describe how to add a trace to the dispatch_command function in the /home/mysql-8.0.17/sql/sql_parse.cc file.
In the following steps, the root directory of the MySQL source code is /home/mysql-8.0.17. Replace it with the actual root directory.
- Download the Babeltrace source code.
Download the source code at GitHub.
Figure 1 Babeltrace source code
- Compile and install Babeltrace.
Run the following commands in the source code directory generated after the decompression:
./bootstrap
./configure
make && make install
- Download the LTTng source code.
Download the source code of the required LTTng version at GitHub.
Figure 2 Downloading LTTng
- Compile and install LTTng.
Run the following commands in the source code directory generated after the decompression:
./booststrap
./configure
make && make install
- Create a function-tp.h file.
Run the mkdir /home/mysql-8.0.17/trace command to create a trace directory in /home/mysql-8.0.17 and create a function-tp.h file in /home/mysql-8.0.17/trace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#undef TRACEPOINT_PROVIDER #define TRACEPOINT_PROVIDER function_trace #undef TRACEPOINT_INCLUDE #define TRACEPOINT_INCLUDE "trace/function-tp.h" #if !defined(_FUNCTION_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ) #define _FUNCTION_TP_H #include <lttng/tracepoint.h> TRACEPOINT_EVENT( function_trace, function_enter, TP_ARGS( char *, function_name ), TP_FIELDS( ctf_string(funcname, function_name) ) ) TRACEPOINT_EVENT( function_trace, function_exit, TP_ARGS( char *, function_name ), TP_FIELDS( ctf_string(funcname, function_name) ) ) #endif #include <lttng/tracepoint-event.h>
- Create a function-tp.c file.
Create a source file function-tp.c in the /home/mysql-8.0.17/trace directory.
1 2 3
#define TRACEPOINT_CREATE_PROBES #define TRACEPOINT_DEFINE #include "function-tp.h"
- Compile the dynamic library.
Run the following commands to perform the compilation:
1 2
gcc -I/home/mysql-8.0.17 -fpic -c -o function-tp.o function-tp.c gcc -Wl,--no-as-needed -o lttng-ust-function.so -shared -fpic function-tp.o -ldl -lttng-ust

After the preceding steps are performed, a dynamic link library that provides the tracepoint function is compiled successfully.
- Encapsulate the tracepoint function to expose more friendly interfaces.
Create a function_trace.h file in /home/mysql-8.0.17/trace.
The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef _FUNCTIONTRACE_h_ #define _FUNCTIONTRACE_h_ #define FUNC_TRACE(funcname) \ FunctionTrace lttng_func_trace(funcname, funcname) class FunctionTrace { public: FunctionTrace(const char *category, const char *name); ~FunctionTrace(); private: const char *category_; const char *name_; }; #endif
In addition, create a function_trace.cc file in /home/mysql-8.0.17/trace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <sys/time.h> #define TRACEPOINT_DEFINE #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE #include "trace/function-tp.h" #include "trace/function_trace.h" FunctionTrace::FunctionTrace(const char *category, const char *name) : category_(category), name_(name) { tracepoint(function_trace, function_enter, const_cast<char *>(name_)); } FunctionTrace::~FunctionTrace() { tracepoint(function_trace, function_exit, const_cast<char *>(name_)); }
- Add required information to the service code to trace the service logic.
Open the sql_parse.cc file in the sql directory of the MySQL source code and add the following code after #include.
1#include "trace/function_trace.h"

- Add parameters to sql_parse.cc.
In sql_parse.cc, find the location where dispatch_command is called and add FUNC_TRACE("dispatch_command") to the beginning of the function.

- Modify the CMakeLists.txt file.
Open the CMakeLists.txt file in /home/mysql-8.0.17/sql. Add function_trace.cc to the location shown in the following figure to enable the source file to be included in the MySQL compilation.

In the location shown in the following figure, add the dl dynamic library to sql_main.

- Initialize MySQL.
Recompile MySQL. After the installation and deployment are complete, run the necessary initialization command and then start MySQL:
1LD_PRELOAD="/home/mysql-8.0.17/trace/lttng-ust-function.so" mysqld --defaults-file=/etc/my.cnf
- Check whether the trace event has taken effect.
1 2 3 4
lttng create hellotest1 lttng enable-event -u function_trace:* lttng add-context --userspace --type vtid --type vpid lttng start
Use mysql –h 127.0.0.1 –uroot –pxxx to log in to the MySQL client and run some SQL statements.
1 2
lttng stop lttng destroy
Run the babeltrace /root/lttng-traces/hellotest1-20210505-104018 command. If the following information is displayed, the configuration is successful.

- Add the password-free execution permission for LTTng and Babeltrace at the end of the /etc/sudoers.d/malluma_sudoers file.
1 2
echo 'malluma ALL=(root) NOPASSWD:/usr/local/bin/lttng' >> /etc/sudoers.d/malluma_sudoers echo 'malluma ALL=(root) NOPASSWD:/usr/bin/babeltrace' >> /etc/sudoers.d/malluma_sudoers