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
59MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp) {
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 struct data {
72 void *alloc_trace[256];
73 size_t alloc_count;
74 int alloc_tid;
75
76 void *free_trace[256];
77 size_t free_count;
78 int free_tid;
79 };
80)";
81
83 R"(
84 data t;
85
86 t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64
87 R"(, t.alloc_trace, 256, &t.alloc_tid);
88 t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64
89 R"(, t.free_trace, 256, &t.free_tid);
90
91 t;
92)";
93
94static void CreateHistoryThreadFromValueObject(ProcessSP process_sp,
95 ValueObjectSP return_value_sp,
96 const char *type,
97 const char *thread_name,
98 HistoryThreads &result) {
99 std::string count_path = "." + std::string(type) + "_count";
100 std::string tid_path = "." + std::string(type) + "_tid";
101 std::string trace_path = "." + std::string(type) + "_trace";
102
103 ValueObjectSP count_sp =
104 return_value_sp->GetValueForExpressionPath(count_path.c_str());
105 ValueObjectSP tid_sp =
106 return_value_sp->GetValueForExpressionPath(tid_path.c_str());
107
108 if (!count_sp || !tid_sp)
109 return;
110
111 int count = count_sp->GetValueAsUnsigned(0);
112 tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1;
113
114 if (count <= 0)
115 return;
116
117 ValueObjectSP trace_sp =
118 return_value_sp->GetValueForExpressionPath(trace_path.c_str());
119
120 if (!trace_sp)
121 return;
122
123 std::vector<lldb::addr_t> pcs;
124 for (int i = 0; i < count; i++) {
125 addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0);
126 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS)
127 continue;
128 pcs.push_back(pc);
129 }
130
131 // The ASAN runtime already massages the return addresses into call
132 // addresses, we don't want LLDB's unwinder to try to locate the previous
133 // instruction again as this might lead to us reporting a different line.
134 bool pcs_are_call_addresses = true;
135 HistoryThread *history_thread =
136 new HistoryThread(*process_sp, tid, pcs, pcs_are_call_addresses);
137 ThreadSP new_thread_sp(history_thread);
138 std::ostringstream thread_name_with_number;
139 thread_name_with_number << thread_name << " Thread " << tid;
140 history_thread->SetThreadName(thread_name_with_number.str().c_str());
141 // Save this in the Process' ExtendedThreadList so a strong pointer retains
142 // the object
143 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
144 result.push_back(new_thread_sp);
145}
146
148 HistoryThreads result;
149
150 ProcessSP process_sp = m_process_wp.lock();
151 if (!process_sp)
152 return result;
153
154 ThreadSP thread_sp =
155 process_sp->GetThreadList().GetExpressionExecutionThread();
156 if (!thread_sp)
157 return result;
158
159 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
160 if (!frame_sp)
161 return result;
162
163 ExecutionContext exe_ctx(frame_sp);
164 ValueObjectSP return_value_sp;
165 StreamString expr;
166 Status eval_error;
167 expr.Printf(memory_history_asan_command_format, address, address);
168
170 options.SetUnwindOnError(true);
171 options.SetTryAllThreads(true);
172 options.SetStopOthers(true);
173 options.SetIgnoreBreakpoints(true);
174 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
176 options.SetAutoApplyFixIts(false);
178
180 exe_ctx, options, expr.GetString(), "", return_value_sp, eval_error);
181 if (expr_result != eExpressionCompleted) {
182 StreamString ss;
183 ss << "cannot evaluate AddressSanitizer expression:\n";
184 ss << eval_error.AsCString();
186 process_sp->GetTarget().GetDebugger().GetID());
187 return result;
188 }
189
190 if (!return_value_sp)
191 return result;
192
193 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free",
194 "Memory deallocated by", result);
195 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc",
196 "Memory allocated by", result);
197
198 return result;
199}
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:31
A uniqued constant string class.
Definition: ConstString.h:39
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:1396
void SetLanguage(lldb::LanguageType language)
Definition: Target.h:305
void SetUnwindOnError(bool unwind=false)
Definition: Target.h:324
void SetPrefix(const char *prefix)
Definition: Target.h:313
void SetTryAllThreads(bool try_others=true)
Definition: Target.h:357
void SetTimeout(const Timeout< std::micro > &timeout)
Definition: Target.h:345
void SetStopOthers(bool stop_others=true)
Definition: Target.h:361
void SetIgnoreBreakpoints(bool ignore=false)
Definition: Target.h:328
"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:507
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:130
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:946
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:74
A class that represents a running process on the host machine.
std::vector< lldb::ThreadSP > HistoryThreads
Definition: MemoryHistory.h:21
Definition: SBAddress.h:15
@ eLanguageTypeObjC_plus_plus
Objective-C++.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
uint64_t addr_t
Definition: lldb-types.h:83
uint64_t tid_t
Definition: lldb-types.h:86