Skip to content

Commit c39f620

Browse files
authored
Merge pull request #11239 from medismailben/stable/20240723
[lldb] Avoid crashlog scripted process launch StopAtEntry
2 parents 8adab4f + e562b16 commit c39f620

File tree

9 files changed

+557
-62
lines changed

9 files changed

+557
-62
lines changed

lldb/bindings/python/static-binding/LLDBWrapPython.cpp

Lines changed: 325 additions & 48 deletions
Large diffs are not rendered by default.

lldb/bindings/python/static-binding/lldb.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was automatically generated by SWIG (https://www.swig.org).
2-
# Version 4.3.0
2+
# Version 4.3.1
33
#
44
# Do not make changes to this file unless you know what you are doing - modify
55
# the SWIG interface file instead.
@@ -99,7 +99,7 @@ class _SwigNonDynamicMeta(type):
9999
#3.0.18.
100100
def _to_int(hex):
101101
return hex // 0x10 % 0x10 * 10 + hex % 0x10
102-
swig_version = (_to_int(0x040300 // 0x10000), _to_int(0x040300 // 0x100), _to_int(0x040300))
102+
swig_version = (_to_int(0x040301 // 0x10000), _to_int(0x040301 // 0x100), _to_int(0x040301))
103103
del _to_int
104104

105105

@@ -1021,6 +1021,8 @@ def lldb_iter(obj, getsize, getelem):
10211021

10221022
eArgTypeCPUFeatures = _lldb.eArgTypeCPUFeatures
10231023

1024+
eArgTypeProtocol = _lldb.eArgTypeProtocol
1025+
10241026
eArgTypeLastArg = _lldb.eArgTypeLastArg
10251027

10261028
eSymbolTypeAny = _lldb.eSymbolTypeAny
@@ -10766,6 +10768,34 @@ def GetGenericValue(self):
1076610768
r"""GetGenericValue(SBStructuredData self) -> SBScriptObject"""
1076710769
return _lldb.SBStructuredData_GetGenericValue(self)
1076810770

10771+
def SetValueForKey(self, key, value):
10772+
r"""SetValueForKey(SBStructuredData self, char const * key, SBStructuredData value)"""
10773+
return _lldb.SBStructuredData_SetValueForKey(self, key, value)
10774+
10775+
def SetUnsignedIntegerValue(self, value):
10776+
r"""SetUnsignedIntegerValue(SBStructuredData self, uint64_t value)"""
10777+
return _lldb.SBStructuredData_SetUnsignedIntegerValue(self, value)
10778+
10779+
def SetSignedIntegerValue(self, value):
10780+
r"""SetSignedIntegerValue(SBStructuredData self, int64_t value)"""
10781+
return _lldb.SBStructuredData_SetSignedIntegerValue(self, value)
10782+
10783+
def SetFloatValue(self, value):
10784+
r"""SetFloatValue(SBStructuredData self, double value)"""
10785+
return _lldb.SBStructuredData_SetFloatValue(self, value)
10786+
10787+
def SetBooleanValue(self, value):
10788+
r"""SetBooleanValue(SBStructuredData self, bool value)"""
10789+
return _lldb.SBStructuredData_SetBooleanValue(self, value)
10790+
10791+
def SetStringValue(self, value):
10792+
r"""SetStringValue(SBStructuredData self, char const * value)"""
10793+
return _lldb.SBStructuredData_SetStringValue(self, value)
10794+
10795+
def SetGenericValue(self, value):
10796+
r"""SetGenericValue(SBStructuredData self, SBScriptObject value)"""
10797+
return _lldb.SBStructuredData_SetGenericValue(self, value)
10798+
1076910799
def __repr__(self):
1077010800
r"""__repr__(SBStructuredData self) -> std::string"""
1077110801
return _lldb.SBStructuredData___repr__(self)

lldb/examples/python/crashlog.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,23 +1540,26 @@ def load_crashlog_in_scripted_process(debugger, crashlog_path, options, result):
15401540
}
15411541
)
15421542
)
1543+
1544+
crashlog_sd = lldb.SBStructuredData()
1545+
crashlog_sd.SetGenericValue(
1546+
lldb.SBScriptObject(crashlog, lldb.eScriptLanguagePython)
1547+
)
1548+
structured_data.SetValueForKey("crashlog", crashlog_sd)
1549+
15431550
launch_info = lldb.SBLaunchInfo(None)
15441551
launch_info.SetProcessPluginName("ScriptedProcess")
15451552
launch_info.SetScriptedProcessClassName(
15461553
"crashlog_scripted_process.CrashLogScriptedProcess"
15471554
)
15481555
launch_info.SetScriptedProcessDictionary(structured_data)
1549-
launch_info.SetLaunchFlags(lldb.eLaunchFlagStopAtEntry)
15501556

15511557
error = lldb.SBError()
15521558
process = target.Launch(launch_info, error)
15531559

15541560
if not process or error.Fail():
15551561
raise InteractiveCrashLogException("couldn't launch Scripted Process", error)
15561562

1557-
process.GetScriptedImplementation().set_crashlog(crashlog)
1558-
process.Continue()
1559-
15601563
if not options.skip_status:
15611564

15621565
@contextlib.contextmanager

lldb/examples/python/crashlog_scripted_process.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111

1212
class CrashLogScriptedProcess(ScriptedProcess):
13-
def set_crashlog(self, crashlog):
14-
self.crashlog = crashlog
13+
def parse_crashlog(self):
1514
if self.crashlog.process_id:
1615
if type(self.crashlog.process_id) is int:
1716
self.pid = self.crashlog.process_id
@@ -29,8 +28,6 @@ def set_crashlog(self, crashlog):
2928
if hasattr(self.crashlog, "asb"):
3029
self.extended_thread_info = self.crashlog.asb
3130

32-
crashlog.load_images(self.options, self.loaded_images)
33-
3431
for thread in self.crashlog.threads:
3532
if (
3633
hasattr(thread, "app_specific_backtrace")
@@ -92,10 +89,21 @@ def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData
9289
no_parallel_image_loading.GetBooleanValue()
9390
)
9491

92+
self.crashlog = None
93+
crashlog = args.GetValueForKey("crashlog")
94+
if crashlog and crashlog.IsValid():
95+
if crashlog.GetType() == lldb.eStructuredDataTypeGeneric:
96+
self.crashlog = crashlog.GetGenericValue()
97+
98+
if not self.crashlog:
99+
# Return error
100+
return
101+
95102
self.pid = super().get_process_id()
96103
self.crashed_thread_idx = 0
97104
self.exception = None
98105
self.extended_thread_info = None
106+
self.parse_crashlog()
99107

100108
def read_memory_at_address(
101109
self, addr: int, size: int, error: lldb.SBError
@@ -104,8 +112,8 @@ def read_memory_at_address(
104112
return lldb.SBData()
105113

106114
def get_loaded_images(self):
107-
# TODO: Iterate over corefile_target modules and build a data structure
108-
# from it.
115+
if len(self.loaded_images) == 0:
116+
self.crashlog.load_images(self.options, self.loaded_images)
109117
return self.loaded_images
110118

111119
def should_stop(self) -> bool:

lldb/include/lldb/API/SBStructuredData.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,35 @@ class SBStructuredData {
109109
/// Return the generic pointer if this data structure is a generic type.
110110
lldb::SBScriptObject GetGenericValue() const;
111111

112+
/// Set the value corresponding to a key. If this data structure
113+
/// is not a dictionary type, reset the type to be dictionary and overwrite
114+
/// the previous data.
115+
void SetValueForKey(const char *key, SBStructuredData &value);
116+
117+
/// Change the type to unsigned interger and overwrite the previous data with
118+
/// the new value.
119+
void SetUnsignedIntegerValue(uint64_t value);
120+
121+
/// Change the type to signed interger and overwrite the previous data with
122+
/// the new value.
123+
void SetSignedIntegerValue(int64_t value);
124+
125+
/// Change the type to float and overwrite the previous data with the new
126+
/// value.
127+
void SetFloatValue(double value);
128+
129+
/// Change the type to boolean and overwrite the previous data with the new
130+
/// value.
131+
void SetBooleanValue(bool value);
132+
133+
/// Change the type to string and overwrite the previous data with the new
134+
/// value.
135+
void SetStringValue(const char *value);
136+
137+
/// Change the type to generic and overwrite the previous data with the new
138+
/// value.
139+
void SetGenericValue(SBScriptObject value);
140+
112141
protected:
113142
friend class SBAttachInfo;
114143
friend class SBCommandReturnObject;

lldb/include/lldb/Core/StructuredDataImpl.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@ class StructuredDataImpl {
8181

8282
void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
8383

84+
void SetValueForKey(llvm::StringRef key,
85+
const StructuredData::ObjectSP &value) {
86+
if (!m_data_sp ||
87+
m_data_sp->GetType() != lldb::eStructuredDataTypeDictionary) {
88+
m_data_sp = StructuredData::FromKeyValue(key, value);
89+
} else if (StructuredData::Dictionary *dict =
90+
m_data_sp->GetAsDictionary()) {
91+
dict->AddItem(key, value);
92+
}
93+
}
94+
95+
void SetUnsignedIntegerValue(uint64_t value) {
96+
m_data_sp = StructuredData::FromInteger(value);
97+
}
98+
99+
void SetSignedIntegerValue(int64_t value) {
100+
m_data_sp = StructuredData::FromInteger(value);
101+
}
102+
103+
void SetFloatValue(double value) {
104+
m_data_sp = StructuredData::FromFloat(value);
105+
}
106+
107+
void SetBooleanValue(bool value) {
108+
m_data_sp = StructuredData::FromBoolean(value);
109+
}
110+
111+
void SetStringValue(std::string value) {
112+
m_data_sp = StructuredData::FromString(std::move(value));
113+
}
114+
115+
void SetGenericValue(void *value) {
116+
m_data_sp = StructuredData::FromGeneric(value);
117+
}
118+
84119
lldb::StructuredDataType GetType() const {
85120
return (m_data_sp ? m_data_sp->GetType() :
86121
lldb::eStructuredDataTypeInvalid);

lldb/include/lldb/Utility/StructuredData.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class StructuredData {
432432
}
433433
return success;
434434
}
435-
435+
436436
template <class IntType>
437437
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const {
438438
ObjectSP value_sp = GetValueForKey(key);
@@ -574,6 +574,30 @@ class StructuredData {
574574
void *m_object;
575575
};
576576

577+
template <typename T> static ObjectSP FromInteger(T value) {
578+
return std::make_shared<Integer<T>>(value);
579+
}
580+
581+
static StructuredData::ObjectSP FromFloat(double value) {
582+
return std::make_shared<StructuredData::Float>(value);
583+
}
584+
static StructuredData::ObjectSP FromBoolean(bool value) {
585+
return std::make_shared<StructuredData::Boolean>(value);
586+
}
587+
static StructuredData::ObjectSP FromString(std::string value) {
588+
return std::make_shared<StructuredData::String>(value);
589+
}
590+
static StructuredData::ObjectSP FromGeneric(void *value) {
591+
return std::make_shared<StructuredData::Generic>(value);
592+
}
593+
594+
static StructuredData::ObjectSP
595+
FromKeyValue(llvm::StringRef key, const StructuredData::ObjectSP &value_sp) {
596+
auto dict_sp = std::make_shared<StructuredData::Dictionary>();
597+
dict_sp->AddItem(key, value_sp);
598+
return dict_sp;
599+
}
600+
577601
static ObjectSP ParseJSON(llvm::StringRef json_text);
578602
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error);
579603
static bool IsRecordType(const ObjectSP object_sp);

lldb/source/API/SBStructuredData.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,47 @@ lldb::SBScriptObject SBStructuredData::GetGenericValue() const {
232232

233233
return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
234234
}
235+
236+
void SBStructuredData::SetValueForKey(const char *key,
237+
SBStructuredData &value) {
238+
LLDB_INSTRUMENT_VA(this, key, value);
239+
240+
if (StructuredData::ObjectSP obj_sp = value.m_impl_up->GetObjectSP())
241+
m_impl_up->SetValueForKey(key, obj_sp);
242+
}
243+
244+
void SBStructuredData::SetUnsignedIntegerValue(uint64_t value) {
245+
LLDB_INSTRUMENT_VA(this, value);
246+
247+
m_impl_up->SetUnsignedIntegerValue(value);
248+
}
249+
250+
void SBStructuredData::SetSignedIntegerValue(int64_t value) {
251+
LLDB_INSTRUMENT_VA(this, value);
252+
253+
m_impl_up->SetSignedIntegerValue(value);
254+
}
255+
256+
void SBStructuredData::SetFloatValue(double value) {
257+
LLDB_INSTRUMENT_VA(this, value);
258+
259+
m_impl_up->SetFloatValue(value);
260+
}
261+
262+
void SBStructuredData::SetBooleanValue(bool value) {
263+
LLDB_INSTRUMENT_VA(this, value);
264+
265+
m_impl_up->SetBooleanValue(value);
266+
}
267+
268+
void SBStructuredData::SetStringValue(const char *value) {
269+
LLDB_INSTRUMENT_VA(this, value);
270+
271+
m_impl_up->SetStringValue(value);
272+
}
273+
274+
void SBStructuredData::SetGenericValue(SBScriptObject value) {
275+
LLDB_INSTRUMENT_VA(this, value);
276+
277+
m_impl_up->SetGenericValue(value.GetPointer());
278+
}

lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import json
1212

13-
1413
class TestStructuredDataAPI(TestBase):
1514
NO_DEBUG_INFO_TESTCASE = True
1615

@@ -130,6 +129,52 @@ class MyRandomClass:
130129
self.assertSuccess(example.SetFromJSON("null"))
131130
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeNull)
132131

132+
example = lldb.SBStructuredData()
133+
example.SetUnsignedIntegerValue(1)
134+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeInteger)
135+
self.assertEqual(example.GetIntegerValue(), 1)
136+
137+
example.SetSignedIntegerValue(-42)
138+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeSignedInteger)
139+
self.assertEqual(example.GetSignedIntegerValue(), -42)
140+
141+
example.SetFloatValue(4.19)
142+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeFloat)
143+
self.assertEqual(example.GetFloatValue(), 4.19)
144+
145+
example.SetStringValue("Bonjour, 123!")
146+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeString)
147+
self.assertEqual(example.GetStringValue(42), "Bonjour, 123!")
148+
149+
value = lldb.SBStructuredData()
150+
example.SetValueForKey("Hello", value)
151+
self.assertEqual(example.GetSize(), 0)
152+
153+
nested_obj = lldb.SBStructuredData()
154+
nested_obj.SetStringValue("World")
155+
example.SetValueForKey("Hello", nested_obj)
156+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeDictionary)
157+
nested_obj = None
158+
nested_obj = example.GetValueForKey("Hello")
159+
self.assertTrue(nested_obj.IsValid())
160+
self.assertEqual(nested_obj.GetType(), lldb.eStructuredDataTypeString)
161+
self.assertEqual(nested_obj.GetStringValue(42), "World")
162+
163+
example.SetBooleanValue(True)
164+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeBoolean)
165+
self.assertTrue(example.GetBooleanValue())
166+
167+
rnd_obj = MyRandomClass()
168+
stp = lldb.SBScriptObject(rnd_obj, lldb.eScriptLanguagePython)
169+
self.assertEqual(stp.ptr, rnd_obj)
170+
171+
example.SetGenericValue(stp)
172+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeGeneric)
173+
174+
my_random_class = example.GetGenericValue()
175+
self.assertTrue(my_random_class)
176+
self.assertEqual(my_random_class.payload, MyRandomClass.payload)
177+
133178
example_arr = [1, 2.3, "4", {"5": False}]
134179
arr_str = json.dumps(example_arr)
135180
s.Clear()

0 commit comments

Comments
 (0)