forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlldb-inline-tool.cpp
More file actions
409 lines (342 loc) · 14 KB
/
Copy pathlldb-inline-tool.cpp
File metadata and controls
409 lines (342 loc) · 14 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* LLDB Inline Debug Tool
*
* This tool allows you to add inline debug points in your code using comments:
* // LOG: message here
* // LOG: variable value is {variable_name}
*
* The tool will set non-stopping breakpoints at these locations and print
* the messages when hit, without interrupting program execution.
*
* BUILD INSTRUCTIONS:
* ------------------
* On macOS with Homebrew LLVM:
* c++ -std=c++17 -o lldb-inline lldb-inline-tool.cpp \
* -llldb \
* -L/opt/homebrew/opt/llvm/lib \
* -I/opt/homebrew/opt/llvm/include \
* -Wl,-rpath,/opt/homebrew/opt/llvm/lib
*
* On Linux:
* c++ -std=c++17 -o lldb-inline lldb-inline-tool.cpp \
* -llldb \
* -L/usr/lib/llvm-14/lib \
* -I/usr/lib/llvm-14/include
*
* USAGE:
* ------
* ./lldb-inline <executable> [args...]
*
* The tool searches for // LOG: comments in all source files listed in
* cmake/sources/ *.txt and sets breakpoints at those locations.
*/
#include <lldb/API/LLDB.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <regex>
#include <unistd.h>
#include <glob.h>
#include <fstream>
#include <chrono>
#include <set>
#include <thread>
#include <atomic>
#include <signal.h>
#include <errno.h>
extern char **environ;
using namespace lldb;
struct DebugPoint {
std::string file;
int line;
int column;
enum Type { LOG, VAR } type;
std::string data;
};
std::vector<DebugPoint> debugPoints;
bool logpointCallback(void *baton, SBProcess &process, SBThread &thread, SBBreakpointLocation &location) {
auto start = std::chrono::high_resolution_clock::now();
auto* point = static_cast<DebugPoint*>(baton);
std::cout << point->file << ":" << point->line << ":" << point->column << " ";
// Parse the log message for {expressions}
std::string msg = point->data;
size_t pos = 0;
while ((pos = msg.find('{', pos)) != std::string::npos) {
size_t endPos = msg.find('}', pos);
if (endPos == std::string::npos) {
break;
}
// Extract expression
std::string expr = msg.substr(pos + 1, endPos - pos - 1);
// Evaluate expression
SBFrame frame = thread.GetFrameAtIndex(0);
SBValue result = frame.EvaluateExpression(expr.c_str());
std::string value;
if (result.GetError().Success() && result.GetValue()) {
value = result.GetValue();
} else {
value = "<error>";
}
// Replace {expression} with value
msg.replace(pos, endPos - pos + 1, value);
pos += value.length();
}
std::cout << msg << std::endl;
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Breakpoint callback took: " << duration.count() << "ms" << std::endl;
// Don't stop
return false;
}
std::vector<std::string> getSourceFiles() {
std::vector<std::string> files;
// Read cmake source files
glob_t globbuf;
if (glob("cmake/sources/*.txt", 0, nullptr, &globbuf) == 0) {
for (size_t i = 0; i < globbuf.gl_pathc; i++) {
std::ifstream file(globbuf.gl_pathv[i]);
std::string line;
while (std::getline(file, line)) {
if (!line.empty() && line[0] != '#' && line.find("${") == std::string::npos) {
files.push_back(line);
}
}
}
globfree(&globbuf);
}
return files;
}
void findDebugPoints() {
// Get source files
auto files = getSourceFiles();
if (files.empty()) {
return;
}
// Create temp file with list of files
std::string tmpfile = "/tmp/lldb-inline-files.txt";
std::ofstream out(tmpfile);
for (const auto& file : files) {
out << file << std::endl;
}
out.close();
// Use ripgrep with limited threads for speed
std::string cmd = "cat " + tmpfile + " | xargs rg -j4 --line-number --column --no-heading --color=never '//\\s*LOG:'";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) {
unlink(tmpfile.c_str());
return;
}
char buffer[1024];
std::regex logRegex(".*//\\s*LOG:\\s*(.+)");
while (fgets(buffer, sizeof(buffer), pipe)) {
std::string line(buffer);
// Remove trailing newline
if (!line.empty() && line.back() == '\n') {
line.pop_back();
}
// Parse ripgrep output: file:line:column:text
size_t pos1 = line.find(':');
if (pos1 == std::string::npos) continue;
size_t pos2 = line.find(':', pos1 + 1);
if (pos2 == std::string::npos) continue;
size_t pos3 = line.find(':', pos2 + 1);
if (pos3 == std::string::npos) continue;
DebugPoint point;
point.file = line.substr(0, pos1);
point.line = std::stoi(line.substr(pos1 + 1, pos2 - pos1 - 1));
point.column = std::stoi(line.substr(pos2 + 1, pos3 - pos2 - 1));
std::string text = line.substr(pos3 + 1);
std::smatch match;
if (std::regex_match(text, match, logRegex)) {
point.type = DebugPoint::LOG;
point.data = match[1]; // The message is in capture group 1
// Trim whitespace
point.data.erase(0, point.data.find_first_not_of(" \t"));
point.data.erase(point.data.find_last_not_of(" \t") + 1);
debugPoints.push_back(point);
}
}
pclose(pipe);
unlink(tmpfile.c_str());
}
int main(int argc, char* argv[]) {
if (argc < 2) {
return 1;
}
const char* executable = argv[1];
// Find debug points
auto start = std::chrono::high_resolution_clock::now();
findDebugPoints();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Ripgrep search took: " << duration.count() << "ms" << std::endl;
if (debugPoints.empty()) {
return 1;
}
// Initialize LLDB
start = std::chrono::high_resolution_clock::now();
SBDebugger::Initialize();
SBDebugger debugger = SBDebugger::Create(false); // Don't read .lldbinit
debugger.SetAsync(true);
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "LLDB init took: " << duration.count() << "ms" << std::endl;
// Keep LLDB's stdio handling enabled
SBCommandInterpreter interpreter = debugger.GetCommandInterpreter();
SBCommandReturnObject result;
interpreter.HandleCommand("settings set target.disable-stdio false", result);
interpreter.HandleCommand("settings set symbols.load-on-demand true", result);
interpreter.HandleCommand("settings set target.preload-symbols false", result);
interpreter.HandleCommand("settings set symbols.enable-external-lookup false", result);
interpreter.HandleCommand("settings set target.auto-import-clang-modules false", result);
interpreter.HandleCommand("settings set target.detach-on-error true", result);
// Create target
SBError error;
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
std::string fullPath = executable;
if (fullPath[0] != '/') {
fullPath = std::string(cwd) + "/" + executable;
}
start = std::chrono::high_resolution_clock::now();
SBTarget target = debugger.CreateTarget(fullPath.c_str(), nullptr, nullptr, false, error);
if (!target.IsValid()) {
std::cerr << "Failed to create target: " << error.GetCString() << std::endl;
return 1;
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Create target took: " << duration.count() << "ms" << std::endl;
// Set breakpoints
start = std::chrono::high_resolution_clock::now();
for (auto& point : debugPoints) {
std::string absPath = point.file;
if (absPath[0] != '/') {
absPath = std::string(cwd) + "/" + point.file;
}
SBBreakpoint bp = target.BreakpointCreateByLocation(absPath.c_str(), point.line);
if (bp.IsValid()) {
bp.SetCallback(logpointCallback, &point);
}
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Set breakpoints took: " << duration.count() << "ms" << std::endl;
// Build args
std::vector<const char*> args;
for (int i = 2; i < argc; i++) {
args.push_back(argv[i]);
}
args.push_back(nullptr);
// Launch process with proper settings
SBLaunchInfo launch_info(args.data());
launch_info.SetWorkingDirectory(cwd);
launch_info.SetLaunchFlags(0); // Don't disable stdio
// Pass through environment variables from parent
SBEnvironment env = launch_info.GetEnvironment();
for (char **p = environ; *p != nullptr; p++) {
env.PutEntry(*p);
}
launch_info.SetEnvironment(env, false);
start = std::chrono::high_resolution_clock::now();
SBProcess process = target.Launch(launch_info, error);
if (!process.IsValid()) {
std::cerr << "Failed to launch process: " << error.GetCString() << std::endl;
return 1;
}
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Launch process took: " << duration.count() << "ms" << std::endl;
// lldb::pid_t launchedPid = process.GetProcessID();
// std::cerr << "Launched process with PID: " << launchedPid << std::endl;
// Handle events properly
SBListener listener = debugger.GetListener();
start = std::chrono::high_resolution_clock::now();
auto lastOutput = start;
bool done = false;
bool gotOutput = false;
int eventCount = 0;
while (!done) {
SBEvent event;
if (listener.WaitForEvent(0, event)) { // Non-blocking
eventCount++;
auto eventTime = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(eventTime - start);
StateType state = SBProcess::GetStateFromEvent(event);
// std::cerr << "Event #" << eventCount << " at " << elapsed.count() << "ms: state=" << state << std::endl;
if (state == eStateExited) {
auto exitTime = std::chrono::high_resolution_clock::now();
auto totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(exitTime - start);
// std::cerr << "Process exited with code: " << process.GetExitStatus() << " after " << totalTime.count() << "ms in event loop" << std::endl;
}
switch (state) {
case eStateStopped:
process.Continue();
break;
case eStateRunning:
break;
case eStateExited:
case eStateCrashed:
case eStateDetached:
// std::cerr << "Exiting immediately on state: " << state << std::endl;
// Just exit immediately - skip all cleanup
exit(process.GetExitStatus());
break;
default:
break;
}
} else {
// No event, check if process is done
StateType currentState = process.GetState();
if (currentState == eStateExited || currentState == eStateCrashed || currentState == eStateDetached) {
done = true;
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - lastOutput);
// std::cerr << "Process already exited, detected by polling. Time from last output: " << duration.count() << "ms" << std::endl;
}
}
// Read and forward stdout/stderr
char buffer[1024];
size_t num_bytes;
bool hadStdout = false;
while ((num_bytes = process.GetSTDOUT(buffer, sizeof(buffer)-1)) > 0) {
buffer[num_bytes] = '\0';
std::cout << buffer;
std::cout.flush();
lastOutput = std::chrono::high_resolution_clock::now();
gotOutput = true;
hadStdout = true;
}
bool hadStderr = false;
while ((num_bytes = process.GetSTDERR(buffer, sizeof(buffer)-1)) > 0) {
buffer[num_bytes] = '\0';
std::cerr << buffer;
std::cerr.flush();
lastOutput = std::chrono::high_resolution_clock::now();
gotOutput = true;
hadStderr = true;
}
// Poll process state every iteration
StateType currentState = process.GetState();
if (currentState == eStateExited || currentState == eStateCrashed || currentState == eStateDetached) {
// Process has exited, break out of loop
done = true;
} else {
// Small sleep to avoid busy-waiting
usleep(10000); // 10ms
}
}
int exit_code = process.GetExitStatus();
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Total event loop time: " << duration.count() << "ms" << std::endl;
// Cleanup
start = std::chrono::high_resolution_clock::now();
SBDebugger::Destroy(debugger);
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// std::cerr << "Debugger destroy took: " << duration.count() << "ms" << std::endl;
SBDebugger::Terminate();
return exit_code;
}