LLDB mainline
MemoryHistoryASan.cpp
Go to the documentation of this file.
1//===-- MemoryHistoryASan.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MemoryHistoryASan.h"
10
12
14#include "lldb/Core/Debugger.h"
15#include "lldb/Core/Module.h"
21#include "lldb/Target/Target.h"
22#include "lldb/Target/Thread.h"
24#include "lldb/lldb-private.h"
25
26#include <sstream>
27
28using namespace lldb;
29using namespace lldb_private;
30
32
33MemoryHistorySP MemoryHistoryASan::CreateInstance(const ProcessSP &process_sp) {
34 if (!process_sp.get())
35 return nullptr;
36
37 Target &target = process_sp->GetTarget();
38
39 for (ModuleSP module_sp : target.GetImages().Modules()) {
40 const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType(
41 ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
42
43 if (symbol != nullptr)
44 return MemoryHistorySP(new MemoryHistoryASan(process_sp));
45 }
46
47 return MemoryHistorySP();
48}
49
52 GetPluginNameStatic(), "ASan memory history provider.", CreateInstance);
53}
54
57}
58
60 if (process_sp)
61 m_process_wp = process_sp;
62}
63
65 extern "C"
66 {
67 size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, int *thread_id);
68 size_t __asan_get_free_stack(void *addr, void **trace, size_t size, int *thread_id);
69 }
70)";
71
73 R"(
74 struct {
75 void *alloc_trace[256];
76 size_t alloc_count;
77 int alloc_tid;
78
79 void *free_trace[256];
80 size_t free_count;
81 int free_tid;
82 } t;
83
84 t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64
85 R"(, t.alloc_trace, 256, &t.alloc_tid);
86 t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64
87 R"(, t.free_trace, 256, &t.free_tid);
88
89 t;
90)";
91
93 ValueObjectSP return_value_sp,
94 const char *type,
95 const char *thread_name,
96 HistoryThreads &result) {
97 std::string count_path = "." + std::string(type) + "_count";
98 std::string tid_path = "." + std::string(type) + "_tid";
99 std::string trace_path = "." + std::string(type) + "_trace";
100
101 ValueObjectSP count_sp =
102 return_value_sp->GetValueForExpressionPath(count_path.c_str());
103 ValueObjectSP tid_sp =
104 return_value_sp->GetValueForExpressionPath(tid_path.c_str());
105
106 if (!count_sp || !tid_sp)
107 return;
108
109 int count = count_sp->GetValueAsUnsigned(0);
110 lldb::tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1;
111
112 if (count <= 0)
113 return;
114
115 ValueObjectSP trace_sp =
116 return_value_sp->GetValueForExpressionPath(trace_path.c_str());
117
118 if (!trace_sp)
119 return;
120
121 std::vector<lldb::addr_t> pcs;
122 for (int i = 0; i < count; i++) {
123 addr_t pc = trace_sp->GetChildAtIndex(i)->GetValueAsUnsigned(0);
124 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS)
125 continue;
126 pcs.push_back(pc);
127 }
128
129 // The ASAN runtime already massages the return addresses into call
130 // addresses, we don't want LLDB's unwinder to try to locate the previous
131 // instruction again as this might lead to us reporting a different line.
132 bool pcs_are_call_addresses = true;
133 HistoryThread *history_thread =
134 new HistoryThread(*process_sp, tid, pcs, pcs_are_call_addresses);
135 ThreadSP new_thread_sp(history_thread);
136 std::ostringstream thread_name_with_number;
137 thread_name_with_number << thread_name << " Thread " << tid;
138 history_thread->SetThreadName(thread_name_with_number.str().c_str());
139 // Save this in the Process' ExtendedThreadList so a strong pointer retains
140 // the object
141 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
142 result.push_back(new_thread_sp);
143}
144
146 HistoryThreads result;
147
148 ProcessSP process_sp = m_process_wp.lock();
149 if (!process_sp)
150 return result;
151
152 ThreadSP thread_sp =
153 process_sp->GetThreadList().GetExpressionExecutionThread();
154 if (!thread_sp)
155 return result;
156
157 StackFrameSP frame_sp =
158 thread_sp->GetSelectedFrame(DoNoSelectMostRelevantFrame);
159 if (!frame_sp)
160 return result;
161
162 ExecutionContext exe_ctx(frame_sp);
163 ValueObjectSP return_value_sp;
164 StreamString expr;
165 Status eval_error;
166 expr.Printf(memory_history_asan_command_format, address, address);
167
169 options.SetUnwindOnError(true);
170 options.SetTryAllThreads(true);
171 options.SetStopOthers(true);
172 options.SetIgnoreBreakpoints(true);
173 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
175 options.SetAutoApplyFixIts(false);
177
179 exe_ctx, options, expr.GetString(), "", return_value_sp, eval_error);
180 if (expr_result != eExpressionCompleted) {
181 StreamString ss;
182 ss << "cannot evaluate AddressSanitizer expression:\n";
183 ss << eval_error.AsCString();
185 process_sp->GetTarget().GetDebugger().GetID());
186 return result;
187 }
188
189 if (!return_value_sp)
190 return result;
191
192 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free",
193 "Memory deallocated by", result);
194 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc",
195 "Memory allocated by", result);
196
197 return result;
198}
static void CreateHistoryThreadFromValueObject(ProcessSP process_sp, ValueObjectSP return_value_sp, const char *type, const char *thread_name, HistoryThreads &result)
const char * memory_history_asan_command_prefix
const char * memory_history_asan_command_format
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
A uniqued constant string class.
Definition: ConstString.h:40
static void ReportWarning(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report warning events.
Definition: Debugger.cpp:1587
void SetUnwindOnError(bool unwind=false)
Definition: Target.h:348
void SetLanguage(lldb::LanguageType language_type)
Definition: Target.h:320
void SetPrefix(const char *prefix)
Definition: Target.h:337
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:381
void SetTimeout(const Timeout< std::micro > &timeout)
Definition: Target.h:369
void SetStopOthers(bool stop_others=true)
Definition: Target.h:385
void SetIgnoreBreakpoints(bool ignore=false)
Definition: Target.h:352
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A thread object representing a backtrace from a previous point in the process execution.
Definition: HistoryThread.h:33
void SetThreadName(const char *name)
Definition: HistoryThread.h:70
MemoryHistoryASan(const lldb::ProcessSP &process_sp)
lldb_private::HistoryThreads GetHistoryThreads(lldb::addr_t address) override
static llvm::StringRef GetPluginNameStatic()
static lldb::MemoryHistorySP CreateInstance(const lldb::ProcessSP &process_sp)
ModuleIterable Modules() const
Definition: ModuleList.h:527
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
An error handling class.
Definition: Status.h:44
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:129
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:986
static lldb::ExpressionResults Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, llvm::StringRef expr_cstr, llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp, Status &error, std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Evaluate one expression in the scratch context of the target passed in the exe_ctx and return its res...
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
@ DoNoSelectMostRelevantFrame
A class that represents a running process on the host machine.
std::vector< lldb::ThreadSP > HistoryThreads
Definition: MemoryHistory.h:20
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:446
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:480
@ eLanguageTypeObjC_plus_plus
Objective-C++.
std::shared_ptr< lldb_private::MemoryHistory > MemoryHistorySP
Definition: lldb-forward.h:368
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
uint64_t addr_t
Definition: lldb-types.h:80
uint64_t tid_t
Definition: lldb-types.h:84
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371