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 "lldb/lldb-private.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/PluginInterface.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Expression/UserExpression.h"
19 #include "lldb/Target/ThreadList.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Core/Module.h"
24 #include "Plugins/Process/Utility/HistoryThread.h"
25 #include "lldb/Core/ValueObject.h"
30 using namespace lldb_private;
33 MemoryHistoryASan::CreateInstance (const ProcessSP &process_sp)
35 if (!process_sp.get())
38 Target & target = process_sp->GetTarget();
40 const ModuleList &target_modules = target.GetImages();
41 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
42 const size_t num_modules = target_modules.GetSize();
43 for (size_t i = 0; i < num_modules; ++i)
45 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
47 const Symbol* symbol = module_pointer->FindFirstSymbolWithNameAndType(
48 ConstString("__asan_get_alloc_stack"),
49 lldb::eSymbolTypeAny);
51 if (symbol != nullptr)
52 return MemoryHistorySP(new MemoryHistoryASan(process_sp));
55 return MemoryHistorySP();
59 MemoryHistoryASan::Initialize()
61 PluginManager::RegisterPlugin (GetPluginNameStatic(),
62 "ASan memory history provider.",
67 MemoryHistoryASan::Terminate()
69 PluginManager::UnregisterPlugin (CreateInstance);
74 MemoryHistoryASan::GetPluginNameStatic()
76 static ConstString g_name("asan");
80 MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp)
83 m_process_wp = process_sp;
87 memory_history_asan_command_prefix = R"(
90 size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, int *thread_id);
91 size_t __asan_get_free_stack(void *addr, void **trace, size_t size, int *thread_id);
95 void *alloc_trace[256];
99 void *free_trace[256];
106 memory_history_asan_command_format = R"(
109 t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64 R"(, t.alloc_trace, 256, &t.alloc_tid);
110 t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64 R"(, t.free_trace, 256, &t.free_tid);
115 static void CreateHistoryThreadFromValueObject(ProcessSP process_sp, ValueObjectSP return_value_sp, const char *type, const char *thread_name, HistoryThreads & result)
117 std::string count_path = "." + std::string(type) + "_count";
118 std::string tid_path = "." + std::string(type) + "_tid";
119 std::string trace_path = "." + std::string(type) + "_trace";
121 ValueObjectSP count_sp = return_value_sp->GetValueForExpressionPath(count_path.c_str());
122 ValueObjectSP tid_sp = return_value_sp->GetValueForExpressionPath(tid_path.c_str());
124 if (!count_sp || !tid_sp)
127 int count = count_sp->GetValueAsUnsigned(0);
128 tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1;
133 ValueObjectSP trace_sp = return_value_sp->GetValueForExpressionPath(trace_path.c_str());
138 std::vector<lldb::addr_t> pcs;
139 for (int i = 0; i < count; i++)
141 addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0);
142 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS)
147 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, 0, false);
148 ThreadSP new_thread_sp(history_thread);
149 std::ostringstream thread_name_with_number;
150 thread_name_with_number << thread_name << " Thread " << tid;
151 history_thread->SetThreadName(thread_name_with_number.str().c_str());
152 // Save this in the Process' ExtendedThreadList so a strong pointer retains the object
153 process_sp->GetExtendedThreadList().AddThread (new_thread_sp);
154 result.push_back(new_thread_sp);
157 #define GET_STACK_FUNCTION_TIMEOUT_USEC 2*1000*1000
160 MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address)
162 HistoryThreads result;
164 ProcessSP process_sp = m_process_wp.lock();
168 ThreadSP thread_sp = process_sp->GetThreadList().GetExpressionExecutionThread();
172 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
176 ExecutionContext exe_ctx (frame_sp);
177 ValueObjectSP return_value_sp;
180 expr.Printf(memory_history_asan_command_format, address, address);
182 EvaluateExpressionOptions options;
183 options.SetUnwindOnError(true);
184 options.SetTryAllThreads(true);
185 options.SetStopOthers(true);
186 options.SetIgnoreBreakpoints(true);
187 options.SetTimeoutUsec(GET_STACK_FUNCTION_TIMEOUT_USEC);
188 options.SetPrefix(memory_history_asan_command_prefix);
189 options.SetAutoApplyFixIts(false);
190 options.SetLanguage(eLanguageTypeObjC_plus_plus);
192 ExpressionResults expr_result = UserExpression::Evaluate (exe_ctx,
198 if (expr_result != eExpressionCompleted) {
199 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: Cannot evaluate AddressSanitizer expression:\n%s\n", eval_error.AsCString());
203 if (!return_value_sp)
206 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free", "Memory deallocated by", result);
207 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc", "Memory allocated by", result);