Skip to content

Commit a4fa2e2

Browse files
committedAug 11, 2016
[InstrumentationRuntime] Refactor the API (Part 1/N) (NFCI)
Adapters for instrumentation runtimes have to do two basic things: 1) Load a runtime library. 2) Install breakpoints in that library. This logic is duplicated in the adapters for asan and tsan. Factor it out and document bits of it to make it easier to add new adapters. I tested this with check-lldb, and double-checked testcases/functionalities/{a,t}san. Differential Revision: https://reviews.llvm.org/D23043 llvm-svn: 278367
1 parent 76837df commit a4fa2e2

File tree

6 files changed

+87
-103
lines changed

6 files changed

+87
-103
lines changed
 

‎lldb/include/lldb/Target/InstrumentationRuntime.h

+67-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
// Other libraries and framework includes
1919
// Project includes
20+
#include "lldb/lldb-forward.h"
2021
#include "lldb/lldb-private.h"
2122
#include "lldb/lldb-types.h"
2223
#include "lldb/Core/PluginInterface.h"
@@ -30,16 +31,77 @@ class InstrumentationRuntime :
3031
public std::enable_shared_from_this<InstrumentationRuntime>,
3132
public PluginInterface
3233
{
34+
/// The instrumented process.
35+
lldb::ProcessWP m_process_wp;
36+
37+
/// The module containing the instrumentation runtime.
38+
lldb::ModuleSP m_runtime_module;
39+
40+
/// The breakpoint in the instrumentation runtime.
41+
lldb::user_id_t m_breakpoint_id;
42+
43+
/// Indicates whether or not breakpoints have been registered in the instrumentation runtime.
44+
bool m_is_active;
45+
46+
protected:
47+
InstrumentationRuntime(const lldb::ProcessSP &process_sp)
48+
: m_process_wp(), m_runtime_module(), m_breakpoint_id(0), m_is_active(false)
49+
{
50+
if (process_sp)
51+
m_process_wp = process_sp;
52+
}
53+
54+
lldb::ProcessSP
55+
GetProcessSP()
56+
{
57+
return m_process_wp.lock();
58+
}
59+
60+
lldb::ModuleSP
61+
GetRuntimeModuleSP()
62+
{
63+
return m_runtime_module;
64+
}
65+
66+
void
67+
SetRuntimeModuleSP(lldb::ModuleSP module_sp)
68+
{
69+
m_runtime_module = module_sp;
70+
}
71+
72+
lldb::user_id_t
73+
GetBreakpointID() const
74+
{
75+
return m_breakpoint_id;
76+
}
77+
78+
void
79+
SetBreakpointID(lldb::user_id_t ID)
80+
{
81+
m_breakpoint_id = ID;
82+
}
83+
84+
void
85+
SetActive(bool IsActive)
86+
{
87+
m_is_active = IsActive;
88+
}
89+
3390
public:
3491

3592
static void
3693
ModulesDidLoad(lldb_private::ModuleList &module_list, Process *process, InstrumentationRuntimeCollection &runtimes);
37-
94+
95+
/// Look for the instrumentation runtime in \p module_list. Register and activate the runtime if this hasn't already
96+
/// been done.
3897
virtual void
39-
ModulesDidLoad(lldb_private::ModuleList &module_list);
40-
41-
virtual bool
42-
IsActive();
98+
ModulesDidLoad(lldb_private::ModuleList &module_list) = 0;
99+
100+
bool
101+
IsActive() const
102+
{
103+
return m_is_active;
104+
}
43105

44106
virtual lldb::ThreadCollectionSP
45107
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info);

‎lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp

+10-26
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ AddressSanitizerRuntime::GetTypeStatic()
6363
return eInstrumentationRuntimeTypeAddressSanitizer;
6464
}
6565

66-
AddressSanitizerRuntime::AddressSanitizerRuntime(const ProcessSP &process_sp) :
67-
m_is_active(false),
68-
m_runtime_module(),
69-
m_process_wp(),
70-
m_breakpoint_id(0)
71-
{
72-
if (process_sp)
73-
m_process_wp = process_sp;
74-
}
75-
7666
AddressSanitizerRuntime::~AddressSanitizerRuntime()
7767
{
7868
Deactivate();
@@ -93,7 +83,7 @@ AddressSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
9383
if (IsActive())
9484
return;
9585

96-
if (m_runtime_module) {
86+
if (GetRuntimeModuleSP()) {
9787
Activate();
9888
return;
9989
}
@@ -112,20 +102,14 @@ AddressSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
112102
{
113103
if (ModuleContainsASanRuntime(module_pointer))
114104
{
115-
m_runtime_module = module_pointer->shared_from_this();
105+
SetRuntimeModuleSP(module_pointer->shared_from_this());
116106
Activate();
117107
return;
118108
}
119109
}
120110
}
121111
}
122112

123-
bool
124-
AddressSanitizerRuntime::IsActive()
125-
{
126-
return m_is_active;
127-
}
128-
129113
#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000
130114
const char *
131115
address_sanitizer_retrieve_report_data_prefix = R"(
@@ -305,15 +289,15 @@ AddressSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackConte
305289
void
306290
AddressSanitizerRuntime::Activate()
307291
{
308-
if (m_is_active)
292+
if (IsActive())
309293
return;
310294

311295
ProcessSP process_sp = GetProcessSP();
312296
if (!process_sp)
313297
return;
314298

315299
ConstString symbol_name ("__asan::AsanDie()");
316-
const Symbol *symbol = m_runtime_module->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode);
300+
const Symbol *symbol = GetRuntimeModuleSP()->FindFirstSymbolWithNameAndType (symbol_name, eSymbolTypeCode);
317301

318302
if (symbol == NULL)
319303
return;
@@ -332,28 +316,28 @@ AddressSanitizerRuntime::Activate()
332316
Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get();
333317
breakpoint->SetCallback (AddressSanitizerRuntime::NotifyBreakpointHit, this, true);
334318
breakpoint->SetBreakpointKind ("address-sanitizer-report");
335-
m_breakpoint_id = breakpoint->GetID();
319+
SetBreakpointID(breakpoint->GetID());
336320

337321
StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
338322
if (stream_sp)
339323
{
340324
stream_sp->Printf ("AddressSanitizer debugger support is active. Memory error breakpoint has been installed and you can now use the 'memory history' command.\n");
341325
}
342326

343-
m_is_active = true;
327+
SetActive(true);
344328
}
345329

346330
void
347331
AddressSanitizerRuntime::Deactivate()
348332
{
349-
if (m_breakpoint_id != LLDB_INVALID_BREAK_ID)
333+
if (GetBreakpointID() != LLDB_INVALID_BREAK_ID)
350334
{
351335
ProcessSP process_sp = GetProcessSP();
352336
if (process_sp)
353337
{
354-
process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id);
355-
m_breakpoint_id = LLDB_INVALID_BREAK_ID;
338+
process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
339+
SetBreakpointID(LLDB_INVALID_BREAK_ID);
356340
}
357341
}
358-
m_is_active = false;
342+
SetActive(false);
359343
}

‎lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,8 @@ class AddressSanitizerRuntime : public lldb_private::InstrumentationRuntime
5959
void
6060
ModulesDidLoad(lldb_private::ModuleList &module_list) override;
6161

62-
bool
63-
IsActive() override;
64-
6562
private:
66-
AddressSanitizerRuntime(const lldb::ProcessSP &process_sp);
67-
68-
lldb::ProcessSP
69-
GetProcessSP ()
70-
{
71-
return m_process_wp.lock();
72-
}
63+
AddressSanitizerRuntime(const lldb::ProcessSP &process_sp) : lldb_private::InstrumentationRuntime(process_sp) {}
7364

7465
void
7566
Activate();
@@ -85,11 +76,6 @@ class AddressSanitizerRuntime : public lldb_private::InstrumentationRuntime
8576

8677
std::string
8778
FormatDescription(StructuredData::ObjectSP report);
88-
89-
bool m_is_active;
90-
lldb::ModuleSP m_runtime_module;
91-
lldb::ProcessWP m_process_wp;
92-
lldb::user_id_t m_breakpoint_id;
9379
};
9480

9581
} // namespace lldb_private

‎lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp

+8-24
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ ThreadSanitizerRuntime::GetTypeStatic()
6868
return eInstrumentationRuntimeTypeThreadSanitizer;
6969
}
7070

71-
ThreadSanitizerRuntime::ThreadSanitizerRuntime(const ProcessSP &process_sp) :
72-
m_is_active(false),
73-
m_runtime_module_wp(),
74-
m_process_wp(),
75-
m_breakpoint_id(0)
76-
{
77-
if (process_sp)
78-
m_process_wp = process_sp;
79-
}
80-
8171
ThreadSanitizerRuntime::~ThreadSanitizerRuntime()
8272
{
8373
Deactivate();
@@ -113,7 +103,7 @@ ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
113103
{
114104
if (ModuleContainsTSanRuntime(module_sp))
115105
{
116-
m_runtime_module_wp = module_sp;
106+
SetRuntimeModuleSP(module_sp);
117107
Activate();
118108
return false; // Stop iterating
119109
}
@@ -123,12 +113,6 @@ ThreadSanitizerRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
123113
});
124114
}
125115

126-
bool
127-
ThreadSanitizerRuntime::IsActive()
128-
{
129-
return m_is_active;
130-
}
131-
132116
#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2*1000*1000
133117

134118
const char *
@@ -732,7 +716,7 @@ ThreadSanitizerRuntime::NotifyBreakpointHit(void *baton, StoppointCallbackContex
732716
void
733717
ThreadSanitizerRuntime::Activate()
734718
{
735-
if (m_is_active)
719+
if (IsActive())
736720
return;
737721

738722
ProcessSP process_sp = GetProcessSP();
@@ -759,30 +743,30 @@ ThreadSanitizerRuntime::Activate()
759743
Breakpoint *breakpoint = process_sp->GetTarget().CreateBreakpoint(symbol_address, internal, hardware).get();
760744
breakpoint->SetCallback (ThreadSanitizerRuntime::NotifyBreakpointHit, this, true);
761745
breakpoint->SetBreakpointKind ("thread-sanitizer-report");
762-
m_breakpoint_id = breakpoint->GetID();
746+
SetBreakpointID(breakpoint->GetID());
763747

764748
StreamFileSP stream_sp (process_sp->GetTarget().GetDebugger().GetOutputFile());
765749
if (stream_sp)
766750
{
767751
stream_sp->Printf ("ThreadSanitizer debugger support is active.\n");
768752
}
769753

770-
m_is_active = true;
754+
SetActive(true);
771755
}
772756

773757
void
774758
ThreadSanitizerRuntime::Deactivate()
775759
{
776-
if (m_breakpoint_id != LLDB_INVALID_BREAK_ID)
760+
if (GetBreakpointID() != LLDB_INVALID_BREAK_ID)
777761
{
778762
ProcessSP process_sp = GetProcessSP();
779763
if (process_sp)
780764
{
781-
process_sp->GetTarget().RemoveBreakpointByID(m_breakpoint_id);
782-
m_breakpoint_id = LLDB_INVALID_BREAK_ID;
765+
process_sp->GetTarget().RemoveBreakpointByID(GetBreakpointID());
766+
SetBreakpointID(LLDB_INVALID_BREAK_ID);
783767
}
784768
}
785-
m_is_active = false;
769+
SetActive(false);
786770
}
787771

788772
static std::string

‎lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.h

+1-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "lldb/lldb-private.h"
1818
#include "lldb/Target/ABI.h"
1919
#include "lldb/Target/InstrumentationRuntime.h"
20-
#include "lldb/Target/Process.h"
2120
#include "lldb/Core/StructuredData.h"
2221

2322
namespace lldb_private {
@@ -60,26 +59,11 @@ class ThreadSanitizerRuntime : public lldb_private::InstrumentationRuntime
6059
void
6160
ModulesDidLoad(lldb_private::ModuleList &module_list) override;
6261

63-
bool
64-
IsActive() override;
65-
6662
lldb::ThreadCollectionSP
6763
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info) override;
6864

6965
private:
70-
ThreadSanitizerRuntime(const lldb::ProcessSP &process_sp);
71-
72-
lldb::ProcessSP
73-
GetProcessSP ()
74-
{
75-
return m_process_wp.lock();
76-
}
77-
78-
lldb::ModuleSP
79-
GetRuntimeModuleSP ()
80-
{
81-
return m_runtime_module_wp.lock();
82-
}
66+
ThreadSanitizerRuntime(const lldb::ProcessSP &process_sp) : lldb_private::InstrumentationRuntime(process_sp) {}
8367

8468
void
8569
Activate();
@@ -107,11 +91,6 @@ class ThreadSanitizerRuntime : public lldb_private::InstrumentationRuntime
10791

10892
lldb::addr_t
10993
GetFirstNonInternalFramePc(StructuredData::ObjectSP trace);
110-
111-
bool m_is_active;
112-
lldb::ModuleWP m_runtime_module_wp;
113-
lldb::ProcessWP m_process_wp;
114-
lldb::user_id_t m_breakpoint_id;
11594
};
11695

11796
} // namespace lldb_private

‎lldb/source/Target/InstrumentationRuntime.cpp

-11
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ InstrumentationRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list, ll
4040
}
4141
}
4242

43-
void
44-
InstrumentationRuntime::ModulesDidLoad(lldb_private::ModuleList &module_list)
45-
{
46-
}
47-
48-
bool
49-
InstrumentationRuntime::IsActive()
50-
{
51-
return false;
52-
}
53-
5443
lldb::ThreadCollectionSP
5544
InstrumentationRuntime::GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)
5645
{

0 commit comments

Comments
 (0)
Please sign in to comment.