xref: /docs/en/application-dev/tools/lldb-tool.md (revision e41f4b71)
1e41f4b71Sopenharmony_ci# LLDB
2e41f4b71Sopenharmony_ci## Overview
3e41f4b71Sopenharmony_ciLow Level Debugger (LLDB) is a next-generation, high-performance debugger. For details, visit the [LLDB official website](https://lldb.llvm.org/).
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciLLDB used in OpenHarmony is developed on [LLVM 15.0.4](https://github.com/llvm/llvm-project/releases/tag/llvmorg-15.0.4). It is the default debugger in DevEco Studio and supports debugging of C and C++ applications.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## How to Obtain
8e41f4b71Sopenharmony_ciObtain the OpenHarmony SDK from the following website: http://ci.openharmony.cn/workbench/cicd/dailybuild
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ciFind LLDB in the **\ohos-sdk\[system]\native\llvm** directory of the SDK, where **system** can be **windows**, **linux**, or **darwin**.
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciFor example, for Windows, **lldb.exe** is stored in **\ohos-sdk\windows\native\llvm\bin** after the SDK is decompressed.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Functions
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciThe following lists some functions supported by LLDB. For more functions and related commands, see [LLDB Usage Guide](https://gitee.com/openharmony/third_party_llvm-project/blob/master/lldb/README.md) and [LLDB official manual](https://lldb.llvm.org/use/map.html#).
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci- Logging
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci- Breakpoint management
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- Watchpoint management
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- Expression processing
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci- Viewing variables
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- Process/Thread management
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci- Assembly processing
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci- Obtaining source code information
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci- Signal processing
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci- Launching a process
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci- Attaching to a process
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## Use Scenarios
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci### Local Debugging
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci- Linux x86_64 local debugging
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci  LLDB supports debugging of C and C++ applications in the Linux x86_64 environment.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci- macOS local debugging on the macOS desktop
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci  LLDB supports debugging of C and C++ applications on the macOS desktop (including macOS x86_64 and M1).
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci### Remote Debugging
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci- Remote debugging based on DevEco Studio
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci  LLDB supports remote debugging of native C++ applications by connecting to OpenHarmony devices or emulators on the Windows and macOS desktops based on DevEco Studio.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci- Remote debugging through direct connection
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci  LLDB supports remote debugging of C and C++ applications by directly connecting to OpenHarmony devices in Windows, macOS desktop, and Linux x86_64 environment.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci## How to Use
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci### Local Debugging
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci> **NOTE**
67e41f4b71Sopenharmony_ci>
68e41f4b71Sopenharmony_ci> The local debugging procedure for Linux x86_64 is the same as that for macOS.	
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci**Procedure**
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci- Using LLDB to start and debug an application
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci   The following walks you through on how to debug an executable file named **a.out** in the Linux x86_64 environment. The file contains debugging information and is generated by the Clang compiler, which is of the same version as LLDB.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci   1. Obtain the executable file **a.out**.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci   2. Run LLDB and specify the file to debug as **a.out**.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci      ```lldb
81e41f4b71Sopenharmony_ci      ./lldb a.out
82e41f4b71Sopenharmony_ci      ```
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci   3. Set breakpoints at the **main** function in the code.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci      ```lldb
87e41f4b71Sopenharmony_ci      (lldb) b main
88e41f4b71Sopenharmony_ci      ```
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci   4. Run the application, and it stops at the first breakpoint.
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci      ```lldb
93e41f4b71Sopenharmony_ci      (lldb) run
94e41f4b71Sopenharmony_ci      ```
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci   5. Continue to run the application.
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci      ```lldb
99e41f4b71Sopenharmony_ci      (lldb) continue
100e41f4b71Sopenharmony_ci      ```
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci   6. List all the breakpoints.
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci      ```lldb
105e41f4b71Sopenharmony_ci      (lldb) breakpoint list
106e41f4b71Sopenharmony_ci      ```
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci   7. Show the arguments and local variables of the current frame.
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci      ```lldb
111e41f4b71Sopenharmony_ci      (lldb) frame variable
112e41f4b71Sopenharmony_ci      ```
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci   8. Run debugging commands as required to continue debugging.
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci   9. Exit debugging.
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci      ```lldb
119e41f4b71Sopenharmony_ci      (lldb) quit
120e41f4b71Sopenharmony_ci      ```
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci- Using LLDB to debug a started application
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci   The following walks you through on how to debug an executable file named **a.out** in the macOS environment. The file contains user input and debugging information and is generated by the Clang compiler. 
125e41f4b71Sopenharmony_ci   
126e41f4b71Sopenharmony_ci   1. Start the application on Command Line Interface (CLI) 1. (The message "Please input a number of type int" is displayed.)
127e41f4b71Sopenharmony_ci      ```shell
128e41f4b71Sopenharmony_ci      ./a.out
129e41f4b71Sopenharmony_ci      ```
130e41f4b71Sopenharmony_ci   
131e41f4b71Sopenharmony_ci   2. Run LLDB on CLI 2.
132e41f4b71Sopenharmony_ci      ```shell
133e41f4b71Sopenharmony_ci      ./lldb
134e41f4b71Sopenharmony_ci      ```
135e41f4b71Sopenharmony_ci   
136e41f4b71Sopenharmony_ci   3. Attach to the application.
137e41f4b71Sopenharmony_ci   
138e41f4b71Sopenharmony_ci      ```lldb
139e41f4b71Sopenharmony_ci      (lldb) process attach --name a.out
140e41f4b71Sopenharmony_ci      ```
141e41f4b71Sopenharmony_ci   
142e41f4b71Sopenharmony_ci   4. Set a breakpoint in line 10 of **hello.cpp**.
143e41f4b71Sopenharmony_ci   
144e41f4b71Sopenharmony_ci      ```lldb
145e41f4b71Sopenharmony_ci      (lldb) breakpoint set --file hello.cpp --line 12
146e41f4b71Sopenharmony_ci      ```
147e41f4b71Sopenharmony_ci   
148e41f4b71Sopenharmony_ci   5. On CLI 1, enter a number of the int type.
149e41f4b71Sopenharmony_ci   
150e41f4b71Sopenharmony_ci      ```shell
151e41f4b71Sopenharmony_ci      88
152e41f4b71Sopenharmony_ci      ```
153e41f4b71Sopenharmony_ci   
154e41f4b71Sopenharmony_ci   6. Continue to run the application on CLI 2. The application stops at the breakpoint.
155e41f4b71Sopenharmony_ci   
156e41f4b71Sopenharmony_ci      ````lldb
157e41f4b71Sopenharmony_ci      (lldb) continue
158e41f4b71Sopenharmony_ci      ````
159e41f4b71Sopenharmony_ci   
160e41f4b71Sopenharmony_ci   7. Run debugging commands as required to continue debugging.
161e41f4b71Sopenharmony_ci   
162e41f4b71Sopenharmony_ci   8. Detach from the application.
163e41f4b71Sopenharmony_ci   
164e41f4b71Sopenharmony_ci      ```lldb
165e41f4b71Sopenharmony_ci      (lldb) detach
166e41f4b71Sopenharmony_ci      ```
167e41f4b71Sopenharmony_ci   
168e41f4b71Sopenharmony_ci   9. Exit debugging.
169e41f4b71Sopenharmony_ci   
170e41f4b71Sopenharmony_ci      ```lldb
171e41f4b71Sopenharmony_ci      (lldb) quit
172e41f4b71Sopenharmony_ci      ```
173e41f4b71Sopenharmony_ci   
174e41f4b71Sopenharmony_ci   > **NOTE**
175e41f4b71Sopenharmony_ci   >
176e41f4b71Sopenharmony_ci   > You can also perform step 4 in prior to step 3.
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci### Remote Debugging
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci> **NOTE**
182e41f4b71Sopenharmony_ci>
183e41f4b71Sopenharmony_ci> - During remote debugging, **lldb-server** and **lldb** must be used together.
184e41f4b71Sopenharmony_ci> - The remote debugging procedures for Windows, Linux x86_64, and macOS are the same.
185e41f4b71Sopenharmony_ci>
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci**Procedure**
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ciThe following walks you through on how to remotely debug an executable file named **a.out** by connecting to an ARM-based OpenHarmony device (for example, RK3568 development board) on the Windows platform.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci> **NOTE**
192e41f4b71Sopenharmony_ci>
193e41f4b71Sopenharmony_ci> In the command below, **/data/local/tmp** indicates the specified directory on the device.
194e41f4b71Sopenharmony_ci>
195e41f4b71Sopenharmony_ci> **8080** is the listening port, which can be customized.
196e41f4b71Sopenharmony_ci>
197e41f4b71Sopenharmony_ci> You must have the execute permission on the **lldb-server** and **a.out** files of the device.
198e41f4b71Sopenharmony_ci>
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci1. Open CLI 1 and push **lldb-server** and **a.out** to the device. (**a.out** is generated when you compile **hello.cpp** using the Clang compiler.)
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci   ```shell
203e41f4b71Sopenharmony_ci   hdc file send lldb-server /data/local/tmp
204e41f4b71Sopenharmony_ci   hdc file send a.out /data/local/tmp
205e41f4b71Sopenharmony_ci   ```
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci2. Run **lldb-server**.
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci   ```shell
210e41f4b71Sopenharmony_ci   hdc shell ./data/local/tmp/lldb-server p --server --listen "*:8080"
211e41f4b71Sopenharmony_ci   ```
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci3. Open CLI 2 and run the binary file **lldb**.
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci   ```shell
216e41f4b71Sopenharmony_ci   ./lldb
217e41f4b71Sopenharmony_ci   ```
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci4. Select and connect to the remote device on the LLDB CLI.
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci   ```lldb
222e41f4b71Sopenharmony_ci   (lldb) platform select remote-ohos
223e41f4b71Sopenharmony_ci   (lldb) platform connect connect://localhost:8080 
224e41f4b71Sopenharmony_ci   ```
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci5. Specify the binary file **a.out** on the device to be debugged.
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci   ```lldb
229e41f4b71Sopenharmony_ci   (lldb) target create /data/local/tmp/a.out
230e41f4b71Sopenharmony_ci   ```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci6. Set breakpoints at the **main** function in the code.
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci   ```lldb
235e41f4b71Sopenharmony_ci   (lldb) b main
236e41f4b71Sopenharmony_ci   ```
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci7. Start the application.
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci   ```lldb
241e41f4b71Sopenharmony_ci   (lldb) run
242e41f4b71Sopenharmony_ci   ```
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci8. Display source code for the current target process.
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci   ```lldb
247e41f4b71Sopenharmony_ci   (lldb) source list
248e41f4b71Sopenharmony_ci   ```
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci9. Run debugging commands as required to continue debugging.
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci10. Exit debugging.
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci    ```lldb
255e41f4b71Sopenharmony_ci    (lldb) quit
256e41f4b71Sopenharmony_ci    ```
257