1 //===-- MemoryHistoryASan.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MemoryHistoryASan.h"
12 #include "lldb/Target/MemoryHistory.h"
14 #include "Plugins/Process/Utility/HistoryThread.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/PluginInterface.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Core/ValueObject.h"
20 #include "lldb/Expression/UserExpression.h"
21 #include "lldb/Target/ExecutionContext.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Target/ThreadList.h"
25 #include "lldb/lldb-private.h"
30 using namespace lldb_private;
32 MemoryHistorySP MemoryHistoryASan::CreateInstance(const ProcessSP &process_sp) {
33 if (!process_sp.get())
36 Target &target = process_sp->GetTarget();
38 const ModuleList &target_modules = target.GetImages();
39 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
40 const size_t num_modules = target_modules.GetSize();
41 for (size_t i = 0; i < num_modules; ++i) {
42 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
44 const Symbol *symbol = module_pointer->FindFirstSymbolWithNameAndType(
45 ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
47 if (symbol != nullptr)
48 return MemoryHistorySP(new MemoryHistoryASan(process_sp));
51 return MemoryHistorySP();
54 void MemoryHistoryASan::Initialize() {
55 PluginManager::RegisterPlugin(
56 GetPluginNameStatic(), "ASan memory history provider.", CreateInstance);
59 void MemoryHistoryASan::Terminate() {
60 PluginManager::UnregisterPlugin(CreateInstance);
63 ConstString MemoryHistoryASan::GetPluginNameStatic() {
64 static ConstString g_name("asan");
68 MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp) {
70 m_process_wp = process_sp;
73 const char *memory_history_asan_command_prefix = R"(
76 size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, int *thread_id);
77 size_t __asan_get_free_stack(void *addr, void **trace, size_t size, int *thread_id);
81 void *alloc_trace[256];
85 void *free_trace[256];
91 const char *memory_history_asan_command_format =
95 t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64
96 R"(, t.alloc_trace, 256, &t.alloc_tid);
97 t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64
98 R"(, t.free_trace, 256, &t.free_tid);
103 static void CreateHistoryThreadFromValueObject(ProcessSP process_sp,
104 ValueObjectSP return_value_sp,
106 const char *thread_name,
107 HistoryThreads &result) {
108 std::string count_path = "." + std::string(type) + "_count";
109 std::string tid_path = "." + std::string(type) + "_tid";
110 std::string trace_path = "." + std::string(type) + "_trace";
112 ValueObjectSP count_sp =
113 return_value_sp->GetValueForExpressionPath(count_path.c_str());
114 ValueObjectSP tid_sp =
115 return_value_sp->GetValueForExpressionPath(tid_path.c_str());
117 if (!count_sp || !tid_sp)
120 int count = count_sp->GetValueAsUnsigned(0);
121 tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1;
126 ValueObjectSP trace_sp =
127 return_value_sp->GetValueForExpressionPath(trace_path.c_str());
132 std::vector<lldb::addr_t> pcs;
133 for (int i = 0; i < count; i++) {
134 addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0);
135 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS)
140 HistoryThread *history_thread =
141 new HistoryThread(*process_sp, tid, pcs, 0, false);
142 ThreadSP new_thread_sp(history_thread);
143 std::ostringstream thread_name_with_number;
144 thread_name_with_number << thread_name << " Thread " << tid;
145 history_thread->SetThreadName(thread_name_with_number.str().c_str());
146 // Save this in the Process' ExtendedThreadList so a strong pointer retains
148 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
149 result.push_back(new_thread_sp);
152 static constexpr std::chrono::seconds g_get_stack_function_timeout(2);
154 HistoryThreads MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address) {
155 HistoryThreads result;
157 ProcessSP process_sp = m_process_wp.lock();
162 process_sp->GetThreadList().GetExpressionExecutionThread();
166 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
170 ExecutionContext exe_ctx(frame_sp);
171 ValueObjectSP return_value_sp;
174 expr.Printf(memory_history_asan_command_format, address, address);
176 EvaluateExpressionOptions options;
177 options.SetUnwindOnError(true);
178 options.SetTryAllThreads(true);
179 options.SetStopOthers(true);
180 options.SetIgnoreBreakpoints(true);
181 options.SetTimeout(g_get_stack_function_timeout);
182 options.SetPrefix(memory_history_asan_command_prefix);
183 options.SetAutoApplyFixIts(false);
184 options.SetLanguage(eLanguageTypeObjC_plus_plus);
186 ExpressionResults expr_result = UserExpression::Evaluate(
187 exe_ctx, options, expr.GetString(), "", return_value_sp, eval_error);
188 if (expr_result != eExpressionCompleted) {
189 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
190 "Warning: Cannot evaluate AddressSanitizer expression:\n%s\n",
191 eval_error.AsCString());
195 if (!return_value_sp)
198 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free",
199 "Memory deallocated by", result);
200 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc",
201 "Memory allocated by", result);