Skip to content

Commit eefda18

Browse files
committed
[lldb/Plugins] Move SBTarget::GetExtendedCrashInformation to SBProcess
This patch moves the SB API method GetExtendedCrashInformation from SBTarget to SBProcess since it only makes sense to call this method on a sane process which might not be the case on a SBTarget object. It also addresses some feedbacks received after landing the first patch for the 'crash-info' feature. Differential Revision: https://reviews.llvm.org/D75049 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 6980782 commit eefda18

File tree

13 files changed

+65
-73
lines changed

13 files changed

+65
-73
lines changed

lldb/bindings/interface/SBProcess.i

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ public:
346346
bool
347347
GetDescription (lldb::SBStream &description);
348348

349+
%feature("autodoc", "
350+
Returns the process' extended crash information.") GetExtendedCrashInformation;
351+
lldb::SBStructuredData
352+
GetExtendedCrashInformation ();
353+
349354
uint32_t
350355
GetNumSupportedHardwareWatchpoints (lldb::SBError &error) const;
351356

lldb/bindings/interface/SBTarget.i

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -949,12 +949,6 @@ public:
949949
void
950950
SetLaunchInfo (const lldb::SBLaunchInfo &launch_info);
951951

952-
%feature("autodoc", "
953-
Returns the platform's process extended crash information.") GetExtendedCrashInformation;
954-
lldb::SBStructuredData
955-
GetExtendedCrashInformation ();
956-
957-
958952
void SetCollectingStats(bool v);
959953

960954
bool GetCollectingStats();

lldb/include/lldb/API/SBProcess.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ class LLDB_API SBProcess {
222222

223223
bool GetDescription(lldb::SBStream &description);
224224

225+
SBStructuredData GetExtendedCrashInformation();
226+
225227
/// Start Tracing with the given SBTraceOptions.
226228
///
227229
/// \param[in] options

lldb/include/lldb/API/SBStructuredData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class SBStructuredData {
9191
friend class SBTraceOptions;
9292
friend class SBDebugger;
9393
friend class SBTarget;
94+
friend class SBProcess;
9495
friend class SBThread;
9596
friend class SBThreadPlan;
9697
friend class SBBreakpoint;

lldb/include/lldb/API/SBTarget.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,6 @@ class LLDB_API SBTarget {
819819

820820
void SetLaunchInfo(const lldb::SBLaunchInfo &launch_info);
821821

822-
SBStructuredData GetExtendedCrashInformation();
823-
824822
protected:
825823
friend class SBAddress;
826824
friend class SBBlock;

lldb/include/lldb/Target/Platform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,16 @@ class Platform : public PluginInterface {
831831
/// nullptr. This dictionnary is generic and extensible, as it contains an
832832
/// array for each different type of crash information.
833833
///
834-
/// \param[in] target
835-
/// The target running the crashed process.
834+
/// \param[in] process
835+
/// The crashed process.
836836
///
837837
/// \return
838838
/// A structured data dictionnary containing at each entry, the crash
839839
/// information type as the entry key and the matching an array as the
840840
/// entry value. \b nullptr if not implemented or if the process has no
841841
/// crash information entry. \b error if an error occured.
842842
virtual llvm::Expected<StructuredData::DictionarySP>
843-
FetchExtendedCrashInformation(lldb_private::Target &target) {
843+
FetchExtendedCrashInformation(lldb_private::Process &process) {
844844
return nullptr;
845845
}
846846

lldb/source/API/SBProcess.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lldb/Core/Module.h"
1919
#include "lldb/Core/PluginManager.h"
2020
#include "lldb/Core/StreamFile.h"
21+
#include "lldb/Core/StructuredDataImpl.h"
2122
#include "lldb/Target/MemoryRegionInfo.h"
2223
#include "lldb/Target/Process.h"
2324
#include "lldb/Target/RegisterContext.h"
@@ -1010,6 +1011,30 @@ bool SBProcess::GetDescription(SBStream &description) {
10101011
return true;
10111012
}
10121013

1014+
SBStructuredData SBProcess::GetExtendedCrashInformation() {
1015+
LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStructuredData, SBProcess,
1016+
GetExtendedCrashInformation);
1017+
SBStructuredData data;
1018+
ProcessSP process_sp(GetSP());
1019+
if (!process_sp)
1020+
return LLDB_RECORD_RESULT(data);
1021+
1022+
PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();
1023+
1024+
if (!platform_sp)
1025+
return LLDB_RECORD_RESULT(data);
1026+
1027+
auto expected_data =
1028+
platform_sp->FetchExtendedCrashInformation(*process_sp.get());
1029+
1030+
if (!expected_data)
1031+
return LLDB_RECORD_RESULT(data);
1032+
1033+
StructuredData::ObjectSP fetched_data = *expected_data;
1034+
data.m_impl_up->SetObjectSP(fetched_data);
1035+
return LLDB_RECORD_RESULT(data);
1036+
}
1037+
10131038
uint32_t
10141039
SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {
10151040
LLDB_RECORD_METHOD_CONST(uint32_t, SBProcess,
@@ -1385,6 +1410,8 @@ void RegisterMethods<SBProcess>(Registry &R) {
13851410
LLDB_REGISTER_METHOD(lldb::addr_t, SBProcess, ReadPointerFromMemory,
13861411
(lldb::addr_t, lldb::SBError &));
13871412
LLDB_REGISTER_METHOD(bool, SBProcess, GetDescription, (lldb::SBStream &));
1413+
LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBProcess,
1414+
GetExtendedCrashInformation, ());
13881415
LLDB_REGISTER_METHOD_CONST(uint32_t, SBProcess,
13891416
GetNumSupportedHardwareWatchpoints,
13901417
(lldb::SBError &));

lldb/source/API/SBTarget.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,30 +2388,6 @@ void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
23882388
m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
23892389
}
23902390

2391-
SBStructuredData SBTarget::GetExtendedCrashInformation() {
2392-
LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStructuredData, SBTarget,
2393-
GetExtendedCrashInformation);
2394-
SBStructuredData data;
2395-
TargetSP target_sp(GetSP());
2396-
if (!target_sp)
2397-
return LLDB_RECORD_RESULT(data);
2398-
2399-
PlatformSP platform_sp = target_sp->GetPlatform();
2400-
2401-
if (!target_sp)
2402-
return LLDB_RECORD_RESULT(data);
2403-
2404-
auto expected_data =
2405-
platform_sp->FetchExtendedCrashInformation(*target_sp.get());
2406-
2407-
if (!expected_data)
2408-
return LLDB_RECORD_RESULT(data);
2409-
2410-
StructuredData::ObjectSP fetched_data = *expected_data;
2411-
data.m_impl_up->SetObjectSP(fetched_data);
2412-
return LLDB_RECORD_RESULT(data);
2413-
}
2414-
24152391
namespace lldb_private {
24162392
namespace repro {
24172393

@@ -2654,8 +2630,6 @@ void RegisterMethods<SBTarget>(Registry &R) {
26542630
LLDB_REGISTER_METHOD_CONST(lldb::SBLaunchInfo, SBTarget, GetLaunchInfo, ());
26552631
LLDB_REGISTER_METHOD(void, SBTarget, SetLaunchInfo,
26562632
(const lldb::SBLaunchInfo &));
2657-
LLDB_REGISTER_METHOD(lldb::SBStructuredData, SBTarget,
2658-
GetExtendedCrashInformation, ());
26592633
LLDB_REGISTER_METHOD(
26602634
size_t, SBTarget, ReadMemory,
26612635
(const lldb::SBAddress, void *, size_t, lldb::SBError &));

lldb/source/Commands/CommandObjectProcess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ class CommandObjectProcessStatus : public CommandObjectParsed {
12831283
}
12841284

12851285
auto expected_crash_info =
1286-
platform_sp->FetchExtendedCrashInformation(process->GetTarget());
1286+
platform_sp->FetchExtendedCrashInformation(*process);
12871287

12881288
if (!expected_crash_info) {
12891289
result.AppendError(llvm::toString(expected_crash_info.takeError()));

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,10 +1503,10 @@ PlatformDarwin::ParseVersionBuildDir(llvm::StringRef dir) {
15031503
}
15041504

15051505
llvm::Expected<StructuredData::DictionarySP>
1506-
PlatformDarwin::FetchExtendedCrashInformation(lldb_private::Target &target) {
1506+
PlatformDarwin::FetchExtendedCrashInformation(Process &process) {
15071507
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
15081508

1509-
StructuredData::ArraySP annotations = ExtractCrashInfoAnnotations(target);
1509+
StructuredData::ArraySP annotations = ExtractCrashInfoAnnotations(process);
15101510

15111511
if (!annotations || !annotations->GetSize()) {
15121512
LLDB_LOG(log, "Couldn't extract crash information annotations");
@@ -1522,11 +1522,11 @@ PlatformDarwin::FetchExtendedCrashInformation(lldb_private::Target &target) {
15221522
}
15231523

15241524
StructuredData::ArraySP
1525-
PlatformDarwin::ExtractCrashInfoAnnotations(Target &target) {
1525+
PlatformDarwin::ExtractCrashInfoAnnotations(Process &process) {
15261526
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
15271527

15281528
ConstString section_name("__crash_info");
1529-
ProcessSP process_sp = target.GetProcessSP();
1529+
Target &target = process.GetTarget();
15301530
StructuredData::ArraySP array_sp = std::make_shared<StructuredData::Array>();
15311531

15321532
for (ModuleSP module : target.GetImages().Modules()) {
@@ -1562,8 +1562,8 @@ PlatformDarwin::ExtractCrashInfoAnnotations(Target &target) {
15621562
Status error;
15631563
CrashInfoAnnotations annotations;
15641564
size_t expected_size = sizeof(CrashInfoAnnotations);
1565-
size_t bytes_read = process_sp->ReadMemoryFromInferior(
1566-
load_addr, &annotations, expected_size, error);
1565+
size_t bytes_read = process.ReadMemoryFromInferior(load_addr, &annotations,
1566+
expected_size, error);
15671567

15681568
if (expected_size != bytes_read || error.Fail()) {
15691569
LLDB_LOG(log, "Failed to read {0} section from memory in module {1}: {2}",
@@ -1587,7 +1587,7 @@ PlatformDarwin::ExtractCrashInfoAnnotations(Target &target) {
15871587

15881588
std::string message;
15891589
bytes_read =
1590-
process_sp->ReadCStringFromMemory(annotations.message, message, error);
1590+
process.ReadCStringFromMemory(annotations.message, message, error);
15911591

15921592
if (message.empty() || bytes_read != message.size() || error.Fail()) {
15931593
LLDB_LOG(log, "Failed to read the message from memory in module {0}: {1}",
@@ -1603,8 +1603,8 @@ PlatformDarwin::ExtractCrashInfoAnnotations(Target &target) {
16031603
LLDB_LOG(log, "No message2 available for module {0}.", module_name);
16041604

16051605
std::string message2;
1606-
bytes_read = process_sp->ReadCStringFromMemory(annotations.message2,
1607-
message2, error);
1606+
bytes_read =
1607+
process.ReadCStringFromMemory(annotations.message2, message2, error);
16081608

16091609
if (!message2.empty() && bytes_read == message2.size() && error.Success())
16101610
if (message2.back() == '\n')

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class PlatformDarwin : public PlatformPOSIX {
8686
};
8787

8888
llvm::Expected<lldb_private::StructuredData::DictionarySP>
89-
FetchExtendedCrashInformation(lldb_private::Target &target) override;
89+
FetchExtendedCrashInformation(lldb_private::Process &process) override;
9090

9191
protected:
9292
struct CrashInfoAnnotations {
@@ -107,15 +107,15 @@ class PlatformDarwin : public PlatformPOSIX {
107107
/// extract the section to gather the messages annotations and the abort
108108
/// cause.
109109
///
110-
/// \param[in] target
111-
/// The target running the crashed process.
110+
/// \param[in] process
111+
/// The crashed process.
112112
///
113113
/// \return
114114
/// A structured data array containing at each entry in each entry, the
115115
/// module spec, its UUID, the crash messages and the abort cause.
116116
/// \b nullptr if process has no crash information annotations.
117117
lldb_private::StructuredData::ArraySP
118-
ExtractCrashInfoAnnotations(lldb_private::Target &target);
118+
ExtractCrashInfoAnnotations(lldb_private::Process &process);
119119

120120
void ReadLibdispatchOffsetsAddress(lldb_private::Process *process);
121121

lldb/test/API/functionalities/process_crash_info/TestProcessCrashInfo.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from lldbsuite.test.decorators import *
99
from lldbsuite.test.lldbtest import *
1010
from lldbsuite.test import lldbutil
11+
from lldbsuite.test import lldbtest
12+
1113

1214
class PlatformProcessCrashInfoTestCase(TestBase):
1315

@@ -17,7 +19,7 @@ def setUp(self):
1719
TestBase.setUp(self)
1820
self.runCmd("settings set auto-confirm true")
1921
self.source = "main.c"
20-
self.line = 3
22+
self.line = line_number(self.source, '// break here')
2123

2224
def tearDown(self):
2325
self.runCmd("settings clear auto-confirm")
@@ -52,7 +54,10 @@ def test_api(self):
5254
stream = lldb.SBStream()
5355
self.assertTrue(stream)
5456

55-
crash_info = target.GetExtendedCrashInformation()
57+
process = target.GetProcess()
58+
self.assertTrue(process)
59+
60+
crash_info = process.GetExtendedCrashInformation()
5661

5762
error = crash_info.GetAsJSON(stream)
5863

@@ -62,24 +67,6 @@ def test_api(self):
6267

6368
self.assertIn("pointer being freed was not allocated", stream.GetData())
6469

65-
@skipUnlessDarwin
66-
def test_before_launch(self):
67-
"""Test that lldb doesn't fetch the extended crash information
68-
dictionnary from if the process wasn't launched yet."""
69-
self.build()
70-
target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
71-
self.assertTrue(target, VALID_TARGET)
72-
73-
stream = lldb.SBStream()
74-
self.assertTrue(stream)
75-
76-
crash_info = target.GetExtendedCrashInformation()
77-
78-
error = crash_info.GetAsJSON(stream)
79-
self.assertFalse(error.Success())
80-
self.assertIn("No structured data.", error.GetCString())
81-
82-
@skipUnlessDarwin
8370
def test_on_sane_process(self):
8471
"""Test that lldb doesn't fetch the extended crash information
8572
dictionnary from a 'sane' stopped process."""
@@ -90,7 +77,10 @@ def test_on_sane_process(self):
9077
stream = lldb.SBStream()
9178
self.assertTrue(stream)
9279

93-
crash_info = target.GetExtendedCrashInformation()
80+
process = target.GetProcess()
81+
self.assertTrue(process)
82+
83+
crash_info = process.GetExtendedCrashInformation()
9484

9585
error = crash_info.GetAsJSON(stream)
9686
self.assertFalse(error.Success())

lldb/test/API/functionalities/process_crash_info/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
2+
23
int main() {
3-
int *var = malloc(sizeof(int));
4+
int *var = malloc(sizeof(int)); // break here
45
free(var);
56
free(var);
67
return 0;

0 commit comments

Comments
 (0)