1 //===-- MemoryHistoryASan.cpp -----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "MemoryHistoryASan.h" 11 12 #include "lldb/Target/MemoryHistory.h" 13 14 #include "lldb/lldb-private.h" 15 #include "lldb/Core/PluginInterface.h" 16 #include "lldb/Core/PluginManager.h" 17 #include "lldb/Target/ThreadList.h" 18 #include "lldb/Target/ExecutionContext.h" 19 #include "lldb/Target/Target.h" 20 #include "lldb/Target/Thread.h" 21 #include "lldb/Core/Module.h" 22 #include "Plugins/Process/Utility/HistoryThread.h" 23 #include "lldb/Core/ValueObject.h" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 28 MemoryHistorySP 29 MemoryHistoryASan::CreateInstance (const ProcessSP &process_sp) 30 { 31 if (!process_sp.get()) 32 return NULL; 33 34 Target & target = process_sp->GetTarget(); 35 36 bool found_asan_runtime = false; 37 38 const ModuleList &target_modules = target.GetImages(); 39 Mutex::Locker modules_locker(target_modules.GetMutex()); 40 const size_t num_modules = target_modules.GetSize(); 41 for (size_t i = 0; i < num_modules; ++i) 42 { 43 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i); 44 45 SymbolContextList sc_list; 46 const bool include_symbols = true; 47 const bool append = true; 48 const bool include_inlines = true; 49 50 size_t num_matches = module_pointer->FindFunctions(ConstString("__asan_get_alloc_stack"), NULL, eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list); 51 52 if (num_matches) 53 { 54 found_asan_runtime = true; 55 break; 56 } 57 } 58 59 if (! found_asan_runtime) 60 return MemoryHistorySP(); 61 62 return MemoryHistorySP(new MemoryHistoryASan(process_sp)); 63 } 64 65 void 66 MemoryHistoryASan::Initialize() 67 { 68 PluginManager::RegisterPlugin (GetPluginNameStatic(), 69 "ASan memory history provider.", 70 CreateInstance); 71 } 72 73 void 74 MemoryHistoryASan::Terminate() 75 { 76 PluginManager::UnregisterPlugin (CreateInstance); 77 } 78 79 80 ConstString 81 MemoryHistoryASan::GetPluginNameStatic() 82 { 83 static ConstString g_name("asan"); 84 return g_name; 85 } 86 87 MemoryHistoryASan::MemoryHistoryASan(const ProcessSP &process_sp) 88 { 89 this->m_process_sp = process_sp; 90 } 91 92 const char * 93 memory_history_asan_command_format = R"( 94 struct t { 95 void *alloc_trace[256]; 96 size_t alloc_count; 97 int alloc_tid; 98 99 void *free_trace[256]; 100 size_t free_count; 101 int free_tid; 102 } t; 103 104 t.alloc_count = ((size_t (*) (void *, void **, size_t, int *))__asan_get_alloc_stack)((void *)0x%)" PRIx64 R"(, t.alloc_trace, 256, &t.alloc_tid); 105 t.free_count = ((size_t (*) (void *, void **, size_t, int *))__asan_get_free_stack)((void *)0x%)" PRIx64 R"(, t.free_trace, 256, &t.free_tid); 106 107 t; 108 )"; 109 110 static void CreateHistoryThreadFromValueObject(ProcessSP process_sp, ValueObjectSP return_value_sp, const char *type, const char *thread_name, HistoryThreads & result) 111 { 112 std::string count_path = "." + std::string(type) + "_count"; 113 std::string tid_path = "." + std::string(type) + "_tid"; 114 std::string trace_path = "." + std::string(type) + "_trace"; 115 116 int count = return_value_sp->GetValueForExpressionPath(count_path.c_str())->GetValueAsUnsigned(0); 117 tid_t tid = return_value_sp->GetValueForExpressionPath(tid_path.c_str())->GetValueAsUnsigned(0); 118 119 if (count <= 0) 120 return; 121 122 ValueObjectSP trace_sp = return_value_sp->GetValueForExpressionPath(trace_path.c_str()); 123 124 std::vector<lldb::addr_t> pcs; 125 for (int i = 0; i < count; i++) 126 { 127 addr_t pc = trace_sp->GetChildAtIndex(i, true)->GetValueAsUnsigned(0); 128 if (pc == 0 || pc == 1 || pc == LLDB_INVALID_ADDRESS) 129 continue; 130 pcs.push_back(pc); 131 } 132 133 HistoryThread *history_thread = new HistoryThread(*process_sp, tid, pcs, 0, false); 134 ThreadSP new_thread_sp(history_thread); 135 // let's use thread name for the type of history thread, since history threads don't have names anyway 136 history_thread->SetThreadName(thread_name); 137 // Save this in the Process' ExtendedThreadList so a strong pointer retains the object 138 process_sp->GetExtendedThreadList().AddThread (new_thread_sp); 139 result.push_back(new_thread_sp); 140 } 141 142 #define GET_STACK_FUNCTION_TIMEOUT_USEC 2*1000*1000 143 144 HistoryThreads 145 MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address) 146 { 147 ProcessSP process_sp = m_process_sp; 148 ThreadSP thread_sp = m_process_sp->GetThreadList().GetSelectedThread(); 149 StackFrameSP frame_sp = thread_sp->GetSelectedFrame(); 150 151 if (!frame_sp) 152 { 153 return HistoryThreads(); 154 } 155 156 ExecutionContext exe_ctx (frame_sp); 157 ValueObjectSP return_value_sp; 158 StreamString expr; 159 expr.Printf(memory_history_asan_command_format, address, address); 160 161 EvaluateExpressionOptions options; 162 options.SetUnwindOnError(true); 163 options.SetTryAllThreads(true); 164 options.SetStopOthers(true); 165 options.SetIgnoreBreakpoints(true); 166 options.SetTimeoutUsec(GET_STACK_FUNCTION_TIMEOUT_USEC); 167 168 if (m_process_sp->GetTarget().EvaluateExpression(expr.GetData(), frame_sp.get(), return_value_sp, options) != eExpressionCompleted) 169 { 170 return HistoryThreads(); 171 } 172 if (!return_value_sp) 173 { 174 return HistoryThreads(); 175 } 176 177 HistoryThreads result; 178 179 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "alloc", "Memory allocated at", result); 180 CreateHistoryThreadFromValueObject(process_sp, return_value_sp, "free", "Memory deallocated at", result); 181 182 return result; 183 } 184