Skip to content

Commit faaf2db

Browse files
authored
[lldb-dap] Refactoring JSONUtils to not use g_dap and instead passing in required arguments. (#115561)
This is part of a larger refactor to remove the global `g_dap` variable.
1 parent ccddb6f commit faaf2db

File tree

3 files changed

+102
-77
lines changed

3 files changed

+102
-77
lines changed

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ DecodeMemoryReference(llvm::StringRef memoryReference) {
154154
std::vector<std::string> GetStrings(const llvm::json::Object *obj,
155155
llvm::StringRef key) {
156156
std::vector<std::string> strs;
157-
auto json_array = obj->getArray(key);
157+
const auto *json_array = obj->getArray(key);
158158
if (!json_array)
159159
return strs;
160160
for (const auto &value : *json_array) {
@@ -210,12 +210,6 @@ static bool IsClassStructOrUnionType(lldb::SBType t) {
210210
/// glance.
211211
static std::optional<std::string>
212212
TryCreateAutoSummaryForContainer(lldb::SBValue &v) {
213-
// We gate this feature because it performs GetNumChildren(), which can
214-
// cause performance issues because LLDB needs to complete possibly huge
215-
// types.
216-
if (!g_dap.enable_auto_variable_summaries)
217-
return std::nullopt;
218-
219213
if (!v.MightHaveChildren())
220214
return std::nullopt;
221215
/// As this operation can be potentially slow, we limit the total time spent
@@ -271,10 +265,7 @@ TryCreateAutoSummaryForContainer(lldb::SBValue &v) {
271265

272266
/// Try to create a summary string for the given value that doesn't have a
273267
/// summary of its own.
274-
static std::optional<std::string> TryCreateAutoSummary(lldb::SBValue value) {
275-
if (!g_dap.enable_auto_variable_summaries)
276-
return std::nullopt;
277-
268+
static std::optional<std::string> TryCreateAutoSummary(lldb::SBValue &value) {
278269
// We use the dereferenced value for generating the summary.
279270
if (value.GetType().IsPointerType() || value.GetType().IsReferenceType())
280271
value = value.Dereference();
@@ -485,10 +476,12 @@ static std::string ConvertDebugInfoSizeToString(uint64_t debug_info) {
485476
}
486477
return oss.str();
487478
}
488-
llvm::json::Value CreateModule(lldb::SBModule &module) {
479+
480+
llvm::json::Value CreateModule(lldb::SBTarget &target, lldb::SBModule &module) {
489481
llvm::json::Object object;
490-
if (!module.IsValid())
482+
if (!target.IsValid() || !module.IsValid())
491483
return llvm::json::Value(std::move(object));
484+
492485
const char *uuid = module.GetUUIDString();
493486
object.try_emplace("id", uuid ? std::string(uuid) : std::string(""));
494487
object.try_emplace("name", std::string(module.GetFileSpec().GetFilename()));
@@ -514,7 +507,7 @@ llvm::json::Value CreateModule(lldb::SBModule &module) {
514507
object.try_emplace("symbolStatus", "Symbols not found.");
515508
}
516509
std::string loaded_addr = std::to_string(
517-
module.GetObjectFileHeaderAddress().GetLoadAddress(g_dap.target));
510+
module.GetObjectFileHeaderAddress().GetLoadAddress(target));
518511
object.try_emplace("addressRange", loaded_addr);
519512
std::string version_str;
520513
uint32_t version_nums[3];
@@ -705,7 +698,7 @@ llvm::json::Value CreateSource(llvm::StringRef source_path) {
705698
return llvm::json::Value(std::move(source));
706699
}
707700

708-
std::optional<llvm::json::Value> CreateSource(lldb::SBFrame &frame) {
701+
static std::optional<llvm::json::Value> CreateSource(lldb::SBFrame &frame) {
709702
auto line_entry = frame.GetLineEntry();
710703
// A line entry of 0 indicates the line is compiler generated i.e. no source
711704
// file is associated with the frame.
@@ -776,15 +769,15 @@ std::optional<llvm::json::Value> CreateSource(lldb::SBFrame &frame) {
776769
// },
777770
// "required": [ "id", "name", "line", "column" ]
778771
// }
779-
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame) {
772+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
773+
lldb::SBFormat &format) {
780774
llvm::json::Object object;
781775
int64_t frame_id = MakeDAPFrameID(frame);
782776
object.try_emplace("id", frame_id);
783777

784778
std::string frame_name;
785779
lldb::SBStream stream;
786-
if (g_dap.frame_format &&
787-
frame.GetDescriptionWithFormat(g_dap.frame_format, stream).Success()) {
780+
if (format && frame.GetDescriptionWithFormat(format, stream).Success()) {
788781
frame_name = stream.GetData();
789782

790783
// `function_name` can be a nullptr, which throws an error when assigned to
@@ -801,7 +794,7 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame) {
801794
}
802795

803796
// We only include `[opt]` if a custom frame format is not specified.
804-
if (!g_dap.frame_format && frame.GetFunction().GetIsOptimized())
797+
if (!format && frame.GetFunction().GetIsOptimized())
805798
frame_name += " [opt]";
806799

807800
EmplaceSafeString(object, "name", frame_name);
@@ -835,11 +828,11 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame) {
835828
return llvm::json::Value(std::move(object));
836829
}
837830

838-
llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread) {
831+
llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread,
832+
lldb::SBFormat &format) {
839833
std::string name;
840834
lldb::SBStream stream;
841-
if (g_dap.thread_format &&
842-
thread.GetDescriptionWithFormat(g_dap.thread_format, stream).Success()) {
835+
if (format && thread.GetDescriptionWithFormat(format, stream).Success()) {
843836
name = stream.GetData();
844837
} else {
845838
const uint32_t thread_idx = thread.GetExtendedBacktraceOriginatingIndexID();
@@ -872,13 +865,12 @@ llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread) {
872865
// },
873866
// "required": [ "id", "name" ]
874867
// }
875-
llvm::json::Value CreateThread(lldb::SBThread &thread) {
868+
llvm::json::Value CreateThread(lldb::SBThread &thread, lldb::SBFormat &format) {
876869
llvm::json::Object object;
877870
object.try_emplace("id", (int64_t)thread.GetThreadID());
878871
std::string thread_str;
879872
lldb::SBStream stream;
880-
if (g_dap.thread_format &&
881-
thread.GetDescriptionWithFormat(g_dap.thread_format, stream).Success()) {
873+
if (format && thread.GetDescriptionWithFormat(format, stream).Success()) {
882874
thread_str = stream.GetData();
883875
} else {
884876
const char *thread_name = thread.GetName();
@@ -966,7 +958,7 @@ llvm::json::Value CreateThread(lldb::SBThread &thread) {
966958
// "required": [ "event", "body" ]
967959
// }]
968960
// }
969-
llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
961+
llvm::json::Value CreateThreadStopped(DAP &dap, lldb::SBThread &thread,
970962
uint32_t stop_id) {
971963
llvm::json::Object event(CreateEventObject("stopped"));
972964
llvm::json::Object body;
@@ -976,13 +968,13 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
976968
body.try_emplace("reason", "step");
977969
break;
978970
case lldb::eStopReasonBreakpoint: {
979-
ExceptionBreakpoint *exc_bp = g_dap.GetExceptionBPFromStopReason(thread);
971+
ExceptionBreakpoint *exc_bp = dap.GetExceptionBPFromStopReason(thread);
980972
if (exc_bp) {
981973
body.try_emplace("reason", "exception");
982974
EmplaceSafeString(body, "description", exc_bp->label);
983975
} else {
984976
InstructionBreakpoint *inst_bp =
985-
g_dap.GetInstructionBPFromStopReason(thread);
977+
dap.GetInstructionBPFromStopReason(thread);
986978
if (inst_bp) {
987979
body.try_emplace("reason", "instruction breakpoint");
988980
} else {
@@ -1042,21 +1034,21 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
10421034
}
10431035
}
10441036
// "threadCausedFocus" is used in tests to validate breaking behavior.
1045-
if (tid == g_dap.focus_tid) {
1037+
if (tid == dap.focus_tid) {
10461038
body.try_emplace("threadCausedFocus", true);
10471039
}
1048-
body.try_emplace("preserveFocusHint", tid != g_dap.focus_tid);
1040+
body.try_emplace("preserveFocusHint", tid != dap.focus_tid);
10491041
body.try_emplace("allThreadsStopped", true);
10501042
event.try_emplace("body", std::move(body));
10511043
return llvm::json::Value(std::move(event));
10521044
}
10531045

1054-
const char *GetNonNullVariableName(lldb::SBValue v) {
1046+
const char *GetNonNullVariableName(lldb::SBValue &v) {
10551047
const char *name = v.GetName();
10561048
return name ? name : "<null>";
10571049
}
10581050

1059-
std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
1051+
std::string CreateUniqueVariableNameForDisplay(lldb::SBValue &v,
10601052
bool is_name_duplicated) {
10611053
lldb::SBStream name_builder;
10621054
name_builder.Print(GetNonNullVariableName(v));
@@ -1073,7 +1065,9 @@ std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
10731065
return name_builder.GetData();
10741066
}
10751067

1076-
VariableDescription::VariableDescription(lldb::SBValue v, bool format_hex,
1068+
VariableDescription::VariableDescription(lldb::SBValue v,
1069+
bool auto_variable_summaries,
1070+
bool format_hex,
10771071
bool is_name_duplicated,
10781072
std::optional<std::string> custom_name)
10791073
: v(v) {
@@ -1104,7 +1098,7 @@ VariableDescription::VariableDescription(lldb::SBValue v, bool format_hex,
11041098
} else {
11051099
value = llvm::StringRef(v.GetValue()).str();
11061100
summary = llvm::StringRef(v.GetSummary()).str();
1107-
if (summary.empty())
1101+
if (summary.empty() && auto_variable_summaries)
11081102
auto_summary = TryCreateAutoSummary(v);
11091103

11101104
std::optional<std::string> effective_summary =
@@ -1188,7 +1182,7 @@ bool ValuePointsToCode(lldb::SBValue v) {
11881182

11891183
lldb::addr_t addr = v.GetValueAsAddress();
11901184
lldb::SBLineEntry line_entry =
1191-
g_dap.target.ResolveLoadAddress(addr).GetLineEntry();
1185+
v.GetTarget().ResolveLoadAddress(addr).GetLineEntry();
11921186

11931187
return line_entry.IsValid();
11941188
}
@@ -1349,9 +1343,12 @@ std::pair<int64_t, bool> UnpackLocation(int64_t location_id) {
13491343
// "required": [ "name", "value", "variablesReference" ]
13501344
// }
13511345
llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
1352-
bool format_hex, bool is_name_duplicated,
1346+
bool format_hex, bool auto_variable_summaries,
1347+
bool synthetic_child_debugging,
1348+
bool is_name_duplicated,
13531349
std::optional<std::string> custom_name) {
1354-
VariableDescription desc(v, format_hex, is_name_duplicated, custom_name);
1350+
VariableDescription desc(v, auto_variable_summaries, format_hex,
1351+
is_name_duplicated, custom_name);
13551352
llvm::json::Object object;
13561353
EmplaceSafeString(object, "name", desc.name);
13571354
EmplaceSafeString(object, "value", desc.display_value);
@@ -1387,7 +1384,7 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
13871384
size_t num_children = v.GetNumChildren();
13881385
// If we are creating a "[raw]" fake child for each synthetic type, we
13891386
// have to account for it when returning indexed variables.
1390-
if (g_dap.enable_synthetic_child_debugging)
1387+
if (synthetic_child_debugging)
13911388
++num_children;
13921389
object.try_emplace("indexedVariables", num_children);
13931390
}
@@ -1418,7 +1415,7 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
14181415
return llvm::json::Value(std::move(object));
14191416
}
14201417

1421-
llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit unit) {
1418+
llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit &unit) {
14221419
llvm::json::Object object;
14231420
char unit_path_arr[PATH_MAX];
14241421
unit.GetFileSpec().GetPath(unit_path_arr, sizeof(unit_path_arr));
@@ -1439,7 +1436,7 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14391436
// the terminal in a new window.
14401437
run_in_terminal_args.try_emplace("kind", "integrated");
14411438

1442-
auto launch_request_arguments = launch_request.getObject("arguments");
1439+
const auto *launch_request_arguments = launch_request.getObject("arguments");
14431440
// The program path must be the first entry in the "args" field
14441441
std::vector<std::string> args = {debug_adaptor_path.str(), "--comm-file",
14451442
comm_file.str()};
@@ -1465,11 +1462,7 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14651462
llvm::StringRef key = envs.GetNameAtIndex(index);
14661463
llvm::StringRef value = envs.GetValueAtIndex(index);
14671464

1468-
if (key.empty())
1469-
g_dap.SendOutput(OutputType::Stderr,
1470-
"empty environment variable for value: \"" +
1471-
value.str() + '\"');
1472-
else
1465+
if (!key.empty())
14731466
env_json.try_emplace(key, value);
14741467
}
14751468
run_in_terminal_args.try_emplace("env",
@@ -1481,8 +1474,8 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14811474
// Keep all the top level items from the statistics dump, except for the
14821475
// "modules" array. It can be huge and cause delay
14831476
// Array and dictionary value will return as <key, JSON string> pairs
1484-
void FilterAndGetValueForKey(const lldb::SBStructuredData data, const char *key,
1485-
llvm::json::Object &out) {
1477+
static void FilterAndGetValueForKey(const lldb::SBStructuredData data,
1478+
const char *key, llvm::json::Object &out) {
14861479
lldb::SBStructuredData value = data.GetValueForKey(key);
14871480
std::string key_utf8 = llvm::json::fixUTF8(key);
14881481
if (llvm::StringRef(key) == "modules")
@@ -1524,8 +1517,8 @@ void FilterAndGetValueForKey(const lldb::SBStructuredData data, const char *key,
15241517
}
15251518
}
15261519

1527-
void addStatistic(llvm::json::Object &event) {
1528-
lldb::SBStructuredData statistics = g_dap.target.GetStatistics();
1520+
static void addStatistic(lldb::SBTarget &target, llvm::json::Object &event) {
1521+
lldb::SBStructuredData statistics = target.GetStatistics();
15291522
bool is_dictionary =
15301523
statistics.GetType() == lldb::eStructuredDataTypeDictionary;
15311524
if (!is_dictionary)
@@ -1542,9 +1535,9 @@ void addStatistic(llvm::json::Object &event) {
15421535
event.try_emplace("statistics", std::move(stats_body));
15431536
}
15441537

1545-
llvm::json::Object CreateTerminatedEventObject() {
1538+
llvm::json::Object CreateTerminatedEventObject(lldb::SBTarget &target) {
15461539
llvm::json::Object event(CreateEventObject("terminated"));
1547-
addStatistic(event);
1540+
addStatistic(target, event);
15481541
return event;
15491542
}
15501543

0 commit comments

Comments
 (0)