1 //===-- MemoryHistoryASan.cpp -----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MemoryHistoryASan.h"
11 #include "lldb/Target/MemoryHistory.h"
13 #include "Plugins/Process/Utility/HistoryThread.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginInterface.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Expression/UserExpression.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/ThreadList.h"
24 #include "lldb/lldb-private.h"
29 using namespace lldb_private;
31 MemoryHistorySP MemoryHistoryASan::CreateInstance(const ProcessSP &process_sp) {
32 if (!process_sp.get())
35 Target &target = process_sp->GetTarget();
37 const ModuleList &target_modules = target.GetImages();
38 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
39 const size_t num_modules = target_modules.GetSize();
40 for (size_t i = 0; i < num_modules; ++i) {
41 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
43 const Symbol *symbol = module_pointer->FindFirstSymbolWithNameAndType(
44 ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny);
46 if (symbol != nullptr)
47 return MemoryHistorySP(new MemoryHistoryASan(process_sp));
50 return MemoryHistorySP();
53 void MemoryHistoryASan::Initialize() {
54 PluginManager::RegisterPlugin(
55 GetPluginNameStatic(), "ASan memory history provider.", CreateInstance);
58 void MemoryHistoryASan::Terminate() {
59 PluginManager::UnregisterPlugin(CreateInstance);
62 ConstString MemoryHistoryASan::GetPluginNameStatic() {
63 static ConstString g_name("asan");
67 MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp) {
69 m_process_wp = process_sp;
72 const char *memory_history_asan_command_prefix = R"(
75 size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size, int *thread_id);
76 size_t __asan_get_free_stack(void *addr, void **trace, size_t size, int *thread_id);
80 void *alloc_trace[256];
84 void *free_trace[256];
90 const char *memory_history_asan_command_format =
94 t.alloc_count = __asan_get_alloc_stack((void *)0x%)" PRIx64
95 R"(, t.alloc_trace, 256, &t.alloc_tid);
96 t.free_count = __asan_get_free_stack((void *)0x%)" PRIx64
97 R"(, t.free_trace, 256, &t.free_tid);
102 static void CreateHistoryThreadFromValueObject(ProcessSP process_sp,
103 ValueObjectSP return_value_sp,
105 const char *thread_name,
106 HistoryThreads &result) {
107 std::string count_path = "." + std::string(type) + "_count";
108 std::string tid_path = "." + std::string(type) + "_tid";
109 std::string trace_path = "." + std::string(type) + "_trace";
111 ValueObjectSP count_sp =
112 return_value_sp->GetValueForExpressionPath(count_path.c_str());
113 ValueObjectSP tid_sp =
114 return_value_sp->GetValueForExpressionPath(tid_path.c_str());
116 if (!count_sp || !tid_sp)
119 int count = count_sp->GetValueAsUnsigned(0);
120 tid_t tid = tid_sp->GetValueAsUnsigned(0) + 1;
125 ValueObjectSP trace_sp =
126 return_value_sp->GetValueForExpressionPath(trace_path.c_str());
131 std::vector<lldb::addr_t> pcs;
132 for (int i = 0; i < count; i++) {
133 addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0);
134 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS)
139 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs);
140 ThreadSP new_thread_sp(history_thread);
141 std::ostringstream thread_name_with_number;
142 thread_name_with_number << thread_name << " Thread " << tid;
143 history_thread->SetThreadName(thread_name_with_number.str().c_str());
144 // Save this in the Process' ExtendedThreadList so a strong pointer retains
146 process_sp->GetExtendedThreadList().AddThread(new_thread_sp);
147 result.push_back(new_thread_sp);
150 HistoryThreads MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address) {
151 HistoryThreads result;
153 ProcessSP process_sp = m_process_wp.lock();
158 process_sp->GetThreadList().GetExpressionExecutionThread();
162 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
166 ExecutionContext exe_ctx(frame_sp);
167 ValueObjectSP return_value_sp;
170 expr.Printf(memory_history_asan_command_format, address, address);
172 EvaluateExpressionOptions options;
173 options.SetUnwindOnError(true);
174 options.SetTryAllThreads(true);
175 options.SetStopOthers(true);
176 options.SetIgnoreBreakpoints(true);
177 options.SetTimeout(process_sp->GetUtilityExpressionTimeout());
178 options.SetPrefix(memory_history_asan_command_prefix);
179 options.SetAutoApplyFixIts(false);
180 options.SetLanguage(eLanguageTypeObjC_plus_plus);
182 ExpressionResults expr_result = UserExpression::Evaluate(
183 exe_ctx, options, expr.GetString(), "", return_value_sp, eval_error);
184 if (expr_result != eExpressionCompleted) {
185 process_sp->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
186 "Warning: Cannot evaluate AddressSanitizer expression:\n%s\n",
187 eval_error.AsCString());
191 if (!return_value_sp)
194 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free",
195 "Memory deallocated by", result);
196 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc",
197 "Memory allocated by", result);