Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 1 | //===-- OperatingSystemDarwinKernel.cpp --------------------------------*- C++ -*-===// |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 10 | #include "OperatingSystemDarwinKernel.h" |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 11 | // C Includes |
| 12 | // C++ Includes |
| 13 | // Other libraries and framework includes |
| 14 | #include "llvm/ADT/Triple.h" |
| 15 | |
| 16 | #include "lldb/Core/ArchSpec.h" |
| 17 | #include "lldb/Core/DataBufferHeap.h" |
| 18 | #include "lldb/Core/Module.h" |
| 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/RegisterValue.h" |
| 21 | #include "lldb/Core/ValueObjectVariable.h" |
Sean Callanan | 3e80cd9 | 2011-10-12 02:08:07 +0000 | [diff] [blame^] | 22 | #include "lldb/Symbol/ClangNamespaceDecl.h" |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ObjectFile.h" |
| 24 | #include "lldb/Symbol/VariableList.h" |
| 25 | #include "lldb/Target/Process.h" |
| 26 | #include "lldb/Target/StopInfo.h" |
| 27 | #include "lldb/Target/Target.h" |
| 28 | #include "lldb/Target/ThreadList.h" |
| 29 | #include "lldb/Target/Thread.h" |
| 30 | #include "Plugins/Process/Utility/DynamicRegisterInfo.h" |
| 31 | #include "Plugins/Process/Utility/RegisterContextMemory.h" |
| 32 | #include "Plugins/Process/Utility/ThreadMemory.h" |
| 33 | |
| 34 | using namespace lldb; |
| 35 | using namespace lldb_private; |
| 36 | |
| 37 | static ConstString & |
| 38 | GetThreadGPRMemberName () |
| 39 | { |
| 40 | static ConstString g_gpr_member_name("gpr"); |
| 41 | return g_gpr_member_name; |
| 42 | } |
| 43 | |
| 44 | void |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 45 | OperatingSystemDarwinKernel::Initialize() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 46 | { |
| 47 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 48 | GetPluginDescriptionStatic(), |
| 49 | CreateInstance); |
| 50 | } |
| 51 | |
| 52 | void |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 53 | OperatingSystemDarwinKernel::Terminate() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 54 | { |
| 55 | PluginManager::UnregisterPlugin (CreateInstance); |
| 56 | } |
| 57 | |
| 58 | OperatingSystem * |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 59 | OperatingSystemDarwinKernel::CreateInstance (Process *process, bool force) |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 60 | { |
| 61 | #if 0 |
| 62 | bool create = force; |
| 63 | if (!create) |
| 64 | { |
| 65 | Module* exe_module = process->GetTarget().GetExecutableModulePointer(); |
| 66 | if (exe_module) |
| 67 | { |
| 68 | ObjectFile *object_file = exe_module->GetObjectFile(); |
| 69 | if (object_file) |
| 70 | { |
| 71 | SectionList *section_list = object_file->GetSectionList(); |
| 72 | if (section_list) |
| 73 | { |
| 74 | static ConstString g_kld_section_name ("__KLD"); |
| 75 | if (section_list->FindSectionByName (g_kld_section_name)) |
| 76 | { |
| 77 | create = true; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // We can limit the creation of this plug-in to "*-apple-darwin" triples |
| 84 | // if we command out the lines below... |
| 85 | // if (create) |
| 86 | // { |
| 87 | // const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); |
| 88 | // create = triple_ref.getOS() == llvm::Triple::Darwin && triple_ref.getVendor() == llvm::Triple::Apple; |
| 89 | // } |
| 90 | } |
| 91 | |
| 92 | if (create) |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 93 | return new OperatingSystemDarwinKernel (process); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 94 | #endif |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | const char * |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 100 | OperatingSystemDarwinKernel::GetPluginNameStatic() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 101 | { |
| 102 | return "macosx-kernel"; |
| 103 | } |
| 104 | |
| 105 | const char * |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 106 | OperatingSystemDarwinKernel::GetPluginDescriptionStatic() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 107 | { |
| 108 | return "Operating system plug-in that gathers OS information from darwin kernels."; |
| 109 | } |
| 110 | |
| 111 | |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 112 | OperatingSystemDarwinKernel::OperatingSystemDarwinKernel (lldb_private::Process *process) : |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 113 | OperatingSystem (process), |
| 114 | m_thread_list_valobj_sp (), |
| 115 | m_register_info_ap () |
| 116 | { |
| 117 | } |
| 118 | |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 119 | OperatingSystemDarwinKernel::~OperatingSystemDarwinKernel () |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | |
| 123 | ValueObjectSP |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 124 | OperatingSystemDarwinKernel::GetThreadListValueObject () |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 125 | { |
| 126 | if (m_thread_list_valobj_sp.get() == NULL) |
| 127 | { |
| 128 | VariableList variable_list; |
| 129 | const uint32_t max_matches = 1; |
| 130 | const bool append = true; |
| 131 | static ConstString g_thread_list_name("g_thread_list"); |
| 132 | Module *exe_module = m_process->GetTarget().GetExecutableModulePointer(); |
| 133 | if (exe_module) |
| 134 | { |
Sean Callanan | 3e80cd9 | 2011-10-12 02:08:07 +0000 | [diff] [blame^] | 135 | if (exe_module->FindGlobalVariables (g_thread_list_name, |
| 136 | NULL, |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 137 | append, |
| 138 | max_matches, |
| 139 | variable_list)) |
| 140 | { |
| 141 | m_thread_list_valobj_sp = ValueObjectVariable::Create (m_process, variable_list.GetVariableAtIndex(0)); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | return m_thread_list_valobj_sp; |
| 146 | } |
| 147 | |
| 148 | DynamicRegisterInfo * |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 149 | OperatingSystemDarwinKernel::GetDynamicRegisterInfo () |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 150 | { |
| 151 | if (m_register_info_ap.get() == NULL && m_thread_list_valobj_sp) |
| 152 | { |
| 153 | m_register_info_ap.reset (new DynamicRegisterInfo()); |
| 154 | ConstString empty_name; |
| 155 | const bool can_create = true; |
| 156 | AddressType addr_type; |
| 157 | addr_t base_addr = LLDB_INVALID_ADDRESS; |
| 158 | ValueObjectSP gpr_valobj_sp (m_thread_list_valobj_sp->GetChildMemberWithName(GetThreadGPRMemberName (), can_create)); |
| 159 | |
| 160 | if (gpr_valobj_sp->IsPointerType ()) |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 161 | base_addr = gpr_valobj_sp->GetPointerValue (&addr_type); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 162 | else |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 163 | base_addr = gpr_valobj_sp->GetAddressOf (true, &addr_type); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 164 | |
| 165 | ValueObjectSP child_valobj_sp; |
| 166 | if (gpr_valobj_sp) |
| 167 | { |
| 168 | ABI *abi = m_process->GetABI().get(); |
| 169 | assert (abi); |
| 170 | uint32_t num_children = gpr_valobj_sp->GetNumChildren(); |
| 171 | |
| 172 | ConstString gpr_name (gpr_valobj_sp->GetName()); |
| 173 | uint32_t reg_num = 0; |
| 174 | for (uint32_t i=0; i<num_children; ++i) |
| 175 | { |
| 176 | child_valobj_sp = gpr_valobj_sp->GetChildAtIndex(i, can_create); |
| 177 | |
| 178 | ConstString reg_name(child_valobj_sp->GetName()); |
| 179 | if (reg_name) |
| 180 | { |
| 181 | const char *reg_name_cstr = reg_name.GetCString(); |
| 182 | while (reg_name_cstr[0] == '_') |
| 183 | ++reg_name_cstr; |
| 184 | if (reg_name_cstr != reg_name.GetCString()) |
| 185 | reg_name.SetCString (reg_name_cstr); |
| 186 | } |
| 187 | |
| 188 | RegisterInfo reg_info; |
| 189 | if (abi->GetRegisterInfoByName(reg_name, reg_info)) |
| 190 | { |
| 191 | // Adjust the byte size and the offset to match the layout of registers in our struct |
| 192 | reg_info.byte_size = child_valobj_sp->GetByteSize(); |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 193 | reg_info.byte_offset = child_valobj_sp->GetAddressOf(true, &addr_type) - base_addr; |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 194 | reg_info.kinds[eRegisterKindLLDB] = reg_num++; |
| 195 | m_register_info_ap->AddRegister (reg_info, reg_name, empty_name, gpr_name); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | printf ("not able to find register info for %s\n", reg_name.GetCString()); // REMOVE THIS printf before checkin!!! |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | m_register_info_ap->Finalize(); |
| 204 | } |
| 205 | } |
| 206 | assert (m_register_info_ap.get()); |
| 207 | return m_register_info_ap.get(); |
| 208 | } |
| 209 | |
| 210 | //------------------------------------------------------------------ |
| 211 | // PluginInterface protocol |
| 212 | //------------------------------------------------------------------ |
| 213 | const char * |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 214 | OperatingSystemDarwinKernel::GetPluginName() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 215 | { |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 216 | return "OperatingSystemDarwinKernel"; |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | const char * |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 220 | OperatingSystemDarwinKernel::GetShortPluginName() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 221 | { |
| 222 | return GetPluginNameStatic(); |
| 223 | } |
| 224 | |
| 225 | uint32_t |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 226 | OperatingSystemDarwinKernel::GetPluginVersion() |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 227 | { |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | uint32_t |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 232 | OperatingSystemDarwinKernel::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 233 | { |
| 234 | // Make any constant strings once and cache the uniqued C string values |
| 235 | // so we don't have to rehash them each time through this function call |
| 236 | static ConstString g_tid_member_name("tid"); |
| 237 | static ConstString g_next_member_name("next"); |
| 238 | |
| 239 | ValueObjectSP root_valobj_sp (GetThreadListValueObject ()); |
| 240 | ValueObjectSP valobj_sp = root_valobj_sp; |
| 241 | const bool can_create = true; |
| 242 | while (valobj_sp) |
| 243 | { |
| 244 | if (valobj_sp->GetValueAsUnsigned(0) == 0) |
| 245 | break; |
| 246 | |
| 247 | ValueObjectSP tid_valobj_sp(valobj_sp->GetChildMemberWithName(g_tid_member_name, can_create)); |
| 248 | if (!tid_valobj_sp) |
| 249 | break; |
| 250 | |
| 251 | tid_t tid = tid_valobj_sp->GetValueAsUnsigned (LLDB_INVALID_THREAD_ID); |
| 252 | if (tid == LLDB_INVALID_THREAD_ID) |
| 253 | break; |
| 254 | |
| 255 | ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); |
| 256 | if (!thread_sp) |
| 257 | thread_sp.reset (new ThreadMemory (*m_process, tid, valobj_sp)); |
| 258 | |
| 259 | new_thread_list.AddThread(thread_sp); |
| 260 | |
| 261 | ValueObjectSP next_valobj_sp (valobj_sp->GetChildMemberWithName(g_next_member_name, can_create)); |
| 262 | |
| 263 | if (next_valobj_sp) |
| 264 | { |
| 265 | // Watch for circular linked lists |
| 266 | if (next_valobj_sp.get() == root_valobj_sp.get()) |
| 267 | break; |
| 268 | } |
| 269 | next_valobj_sp.swap(valobj_sp); |
| 270 | } |
| 271 | return new_thread_list.GetSize(false); |
| 272 | } |
| 273 | |
| 274 | void |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 275 | OperatingSystemDarwinKernel::ThreadWasSelected (Thread *thread) |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 276 | { |
| 277 | } |
| 278 | |
| 279 | RegisterContextSP |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 280 | OperatingSystemDarwinKernel::CreateRegisterContextForThread (Thread *thread) |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 281 | { |
| 282 | ThreadMemory *generic_thread = (ThreadMemory *)thread; |
| 283 | RegisterContextSP reg_ctx_sp; |
| 284 | |
| 285 | ValueObjectSP thread_valobj_sp (generic_thread->GetValueObject()); |
| 286 | if (thread_valobj_sp) |
| 287 | { |
| 288 | const bool can_create = true; |
| 289 | AddressType addr_type; |
| 290 | addr_t base_addr = LLDB_INVALID_ADDRESS; |
| 291 | ValueObjectSP gpr_valobj_sp (thread_valobj_sp->GetChildMemberWithName(GetThreadGPRMemberName (), can_create)); |
| 292 | if (gpr_valobj_sp) |
| 293 | { |
| 294 | if (gpr_valobj_sp->IsPointerType ()) |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 295 | base_addr = gpr_valobj_sp->GetPointerValue (&addr_type); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 296 | else |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 297 | base_addr = gpr_valobj_sp->GetAddressOf (true, &addr_type); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 298 | reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), base_addr)); |
| 299 | } |
| 300 | } |
| 301 | return reg_ctx_sp; |
| 302 | } |
| 303 | |
| 304 | StopInfoSP |
Greg Clayton | 17f3d05 | 2011-08-22 22:30:57 +0000 | [diff] [blame] | 305 | OperatingSystemDarwinKernel::CreateThreadStopReason (lldb_private::Thread *thread) |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 306 | { |
| 307 | StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); |
| 308 | return stop_info_sp; |
| 309 | } |
| 310 | |
| 311 | |