Skip to content

Commit 43cd3f8

Browse files
ashgtiJDevlieghere
authored andcommitted
[lldb-dap] Refactoring JSONUtils to not use g_dap and instead passing in required arguments. (llvm#115561)
This is part of a larger refactor to remove the global `g_dap` variable. (cherry picked from commit faaf2db)
1 parent 425d862 commit 43cd3f8

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 {
@@ -1039,21 +1031,21 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
10391031
}
10401032
}
10411033
// "threadCausedFocus" is used in tests to validate breaking behavior.
1042-
if (tid == g_dap.focus_tid) {
1034+
if (tid == dap.focus_tid) {
10431035
body.try_emplace("threadCausedFocus", true);
10441036
}
1045-
body.try_emplace("preserveFocusHint", tid != g_dap.focus_tid);
1037+
body.try_emplace("preserveFocusHint", tid != dap.focus_tid);
10461038
body.try_emplace("allThreadsStopped", true);
10471039
event.try_emplace("body", std::move(body));
10481040
return llvm::json::Value(std::move(event));
10491041
}
10501042

1051-
const char *GetNonNullVariableName(lldb::SBValue v) {
1043+
const char *GetNonNullVariableName(lldb::SBValue &v) {
10521044
const char *name = v.GetName();
10531045
return name ? name : "<null>";
10541046
}
10551047

1056-
std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
1048+
std::string CreateUniqueVariableNameForDisplay(lldb::SBValue &v,
10571049
bool is_name_duplicated) {
10581050
lldb::SBStream name_builder;
10591051
name_builder.Print(GetNonNullVariableName(v));
@@ -1070,7 +1062,9 @@ std::string CreateUniqueVariableNameForDisplay(lldb::SBValue v,
10701062
return name_builder.GetData();
10711063
}
10721064

1073-
VariableDescription::VariableDescription(lldb::SBValue v, bool format_hex,
1065+
VariableDescription::VariableDescription(lldb::SBValue v,
1066+
bool auto_variable_summaries,
1067+
bool format_hex,
10741068
bool is_name_duplicated,
10751069
std::optional<std::string> custom_name)
10761070
: v(v) {
@@ -1101,7 +1095,7 @@ VariableDescription::VariableDescription(lldb::SBValue v, bool format_hex,
11011095
} else {
11021096
value = llvm::StringRef(v.GetValue()).str();
11031097
summary = llvm::StringRef(v.GetSummary()).str();
1104-
if (summary.empty())
1098+
if (summary.empty() && auto_variable_summaries)
11051099
auto_summary = TryCreateAutoSummary(v);
11061100

11071101
std::optional<std::string> effective_summary =
@@ -1185,7 +1179,7 @@ bool ValuePointsToCode(lldb::SBValue v) {
11851179

11861180
lldb::addr_t addr = v.GetValueAsAddress();
11871181
lldb::SBLineEntry line_entry =
1188-
g_dap.target.ResolveLoadAddress(addr).GetLineEntry();
1182+
v.GetTarget().ResolveLoadAddress(addr).GetLineEntry();
11891183

11901184
return line_entry.IsValid();
11911185
}
@@ -1346,9 +1340,12 @@ std::pair<int64_t, bool> UnpackLocation(int64_t location_id) {
13461340
// "required": [ "name", "value", "variablesReference" ]
13471341
// }
13481342
llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
1349-
bool format_hex, bool is_name_duplicated,
1343+
bool format_hex, bool auto_variable_summaries,
1344+
bool synthetic_child_debugging,
1345+
bool is_name_duplicated,
13501346
std::optional<std::string> custom_name) {
1351-
VariableDescription desc(v, format_hex, is_name_duplicated, custom_name);
1347+
VariableDescription desc(v, auto_variable_summaries, format_hex,
1348+
is_name_duplicated, custom_name);
13521349
llvm::json::Object object;
13531350
EmplaceSafeString(object, "name", desc.name);
13541351
EmplaceSafeString(object, "value", desc.display_value);
@@ -1384,7 +1381,7 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
13841381
size_t num_children = v.GetNumChildren();
13851382
// If we are creating a "[raw]" fake child for each synthetic type, we
13861383
// have to account for it when returning indexed variables.
1387-
if (g_dap.enable_synthetic_child_debugging)
1384+
if (synthetic_child_debugging)
13881385
++num_children;
13891386
object.try_emplace("indexedVariables", num_children);
13901387
}
@@ -1415,7 +1412,7 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t var_ref,
14151412
return llvm::json::Value(std::move(object));
14161413
}
14171414

1418-
llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit unit) {
1415+
llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit &unit) {
14191416
llvm::json::Object object;
14201417
char unit_path_arr[PATH_MAX];
14211418
unit.GetFileSpec().GetPath(unit_path_arr, sizeof(unit_path_arr));
@@ -1436,7 +1433,7 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14361433
// the terminal in a new window.
14371434
run_in_terminal_args.try_emplace("kind", "integrated");
14381435

1439-
auto launch_request_arguments = launch_request.getObject("arguments");
1436+
const auto *launch_request_arguments = launch_request.getObject("arguments");
14401437
// The program path must be the first entry in the "args" field
14411438
std::vector<std::string> args = {debug_adaptor_path.str(), "--comm-file",
14421439
comm_file.str()};
@@ -1462,11 +1459,7 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14621459
llvm::StringRef key = envs.GetNameAtIndex(index);
14631460
llvm::StringRef value = envs.GetValueAtIndex(index);
14641461

1465-
if (key.empty())
1466-
g_dap.SendOutput(OutputType::Stderr,
1467-
"empty environment variable for value: \"" +
1468-
value.str() + '\"');
1469-
else
1462+
if (!key.empty())
14701463
env_json.try_emplace(key, value);
14711464
}
14721465
run_in_terminal_args.try_emplace("env",
@@ -1478,8 +1471,8 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14781471
// Keep all the top level items from the statistics dump, except for the
14791472
// "modules" array. It can be huge and cause delay
14801473
// Array and dictionary value will return as <key, JSON string> pairs
1481-
void FilterAndGetValueForKey(const lldb::SBStructuredData data, const char *key,
1482-
llvm::json::Object &out) {
1474+
static void FilterAndGetValueForKey(const lldb::SBStructuredData data,
1475+
const char *key, llvm::json::Object &out) {
14831476
lldb::SBStructuredData value = data.GetValueForKey(key);
14841477
std::string key_utf8 = llvm::json::fixUTF8(key);
14851478
if (llvm::StringRef(key) == "modules")
@@ -1521,8 +1514,8 @@ void FilterAndGetValueForKey(const lldb::SBStructuredData data, const char *key,
15211514
}
15221515
}
15231516

1524-
void addStatistic(llvm::json::Object &event) {
1525-
lldb::SBStructuredData statistics = g_dap.target.GetStatistics();
1517+
static void addStatistic(lldb::SBTarget &target, llvm::json::Object &event) {
1518+
lldb::SBStructuredData statistics = target.GetStatistics();
15261519
bool is_dictionary =
15271520
statistics.GetType() == lldb::eStructuredDataTypeDictionary;
15281521
if (!is_dictionary)
@@ -1539,9 +1532,9 @@ void addStatistic(llvm::json::Object &event) {
15391532
event.try_emplace("statistics", std::move(stats_body));
15401533
}
15411534

1542-
llvm::json::Object CreateTerminatedEventObject() {
1535+
llvm::json::Object CreateTerminatedEventObject(lldb::SBTarget &target) {
15431536
llvm::json::Object event(CreateEventObject("terminated"));
1544-
addStatistic(event);
1537+
addStatistic(target, event);
15451538
return event;
15461539
}
15471540

0 commit comments

Comments
 (0)