blob: 7aa33c39aa2a1cf4704260e49e1cdfc7a48a936d [file] [log] [blame]
Greg Clayton17f3d052011-08-22 22:30:57 +00001//===-- OperatingSystemDarwinKernel.cpp --------------------------------*- C++ -*-===//
Greg Clayton37f962e2011-08-22 02:49:39 +00002//
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 Clayton17f3d052011-08-22 22:30:57 +000010#include "OperatingSystemDarwinKernel.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000011// 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 Callanan3e80cd92011-10-12 02:08:07 +000022#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000023#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
34using namespace lldb;
35using namespace lldb_private;
36
37static ConstString &
38GetThreadGPRMemberName ()
39{
40 static ConstString g_gpr_member_name("gpr");
41 return g_gpr_member_name;
42}
43
44void
Greg Clayton17f3d052011-08-22 22:30:57 +000045OperatingSystemDarwinKernel::Initialize()
Greg Clayton37f962e2011-08-22 02:49:39 +000046{
47 PluginManager::RegisterPlugin (GetPluginNameStatic(),
48 GetPluginDescriptionStatic(),
49 CreateInstance);
50}
51
52void
Greg Clayton17f3d052011-08-22 22:30:57 +000053OperatingSystemDarwinKernel::Terminate()
Greg Clayton37f962e2011-08-22 02:49:39 +000054{
55 PluginManager::UnregisterPlugin (CreateInstance);
56}
57
58OperatingSystem *
Greg Clayton17f3d052011-08-22 22:30:57 +000059OperatingSystemDarwinKernel::CreateInstance (Process *process, bool force)
Greg Clayton37f962e2011-08-22 02:49:39 +000060{
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 Clayton17f3d052011-08-22 22:30:57 +000093 return new OperatingSystemDarwinKernel (process);
Greg Clayton37f962e2011-08-22 02:49:39 +000094#endif
95 return NULL;
96}
97
98
99const char *
Greg Clayton17f3d052011-08-22 22:30:57 +0000100OperatingSystemDarwinKernel::GetPluginNameStatic()
Greg Clayton37f962e2011-08-22 02:49:39 +0000101{
102 return "macosx-kernel";
103}
104
105const char *
Greg Clayton17f3d052011-08-22 22:30:57 +0000106OperatingSystemDarwinKernel::GetPluginDescriptionStatic()
Greg Clayton37f962e2011-08-22 02:49:39 +0000107{
108 return "Operating system plug-in that gathers OS information from darwin kernels.";
109}
110
111
Greg Clayton17f3d052011-08-22 22:30:57 +0000112OperatingSystemDarwinKernel::OperatingSystemDarwinKernel (lldb_private::Process *process) :
Greg Clayton37f962e2011-08-22 02:49:39 +0000113 OperatingSystem (process),
114 m_thread_list_valobj_sp (),
115 m_register_info_ap ()
116{
117}
118
Greg Clayton17f3d052011-08-22 22:30:57 +0000119OperatingSystemDarwinKernel::~OperatingSystemDarwinKernel ()
Greg Clayton37f962e2011-08-22 02:49:39 +0000120{
121}
122
123ValueObjectSP
Greg Clayton17f3d052011-08-22 22:30:57 +0000124OperatingSystemDarwinKernel::GetThreadListValueObject ()
Greg Clayton37f962e2011-08-22 02:49:39 +0000125{
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 Callanan3e80cd92011-10-12 02:08:07 +0000135 if (exe_module->FindGlobalVariables (g_thread_list_name,
136 NULL,
Greg Clayton37f962e2011-08-22 02:49:39 +0000137 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
148DynamicRegisterInfo *
Greg Clayton17f3d052011-08-22 22:30:57 +0000149OperatingSystemDarwinKernel::GetDynamicRegisterInfo ()
Greg Clayton37f962e2011-08-22 02:49:39 +0000150{
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 Granata91544802011-09-06 19:20:51 +0000161 base_addr = gpr_valobj_sp->GetPointerValue (&addr_type);
Greg Clayton37f962e2011-08-22 02:49:39 +0000162 else
Enrico Granata91544802011-09-06 19:20:51 +0000163 base_addr = gpr_valobj_sp->GetAddressOf (true, &addr_type);
Greg Clayton37f962e2011-08-22 02:49:39 +0000164
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 Granata91544802011-09-06 19:20:51 +0000193 reg_info.byte_offset = child_valobj_sp->GetAddressOf(true, &addr_type) - base_addr;
Greg Clayton37f962e2011-08-22 02:49:39 +0000194 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//------------------------------------------------------------------
213const char *
Greg Clayton17f3d052011-08-22 22:30:57 +0000214OperatingSystemDarwinKernel::GetPluginName()
Greg Clayton37f962e2011-08-22 02:49:39 +0000215{
Greg Clayton17f3d052011-08-22 22:30:57 +0000216 return "OperatingSystemDarwinKernel";
Greg Clayton37f962e2011-08-22 02:49:39 +0000217}
218
219const char *
Greg Clayton17f3d052011-08-22 22:30:57 +0000220OperatingSystemDarwinKernel::GetShortPluginName()
Greg Clayton37f962e2011-08-22 02:49:39 +0000221{
222 return GetPluginNameStatic();
223}
224
225uint32_t
Greg Clayton17f3d052011-08-22 22:30:57 +0000226OperatingSystemDarwinKernel::GetPluginVersion()
Greg Clayton37f962e2011-08-22 02:49:39 +0000227{
228 return 1;
229}
230
231uint32_t
Greg Clayton17f3d052011-08-22 22:30:57 +0000232OperatingSystemDarwinKernel::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
Greg Clayton37f962e2011-08-22 02:49:39 +0000233{
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
274void
Greg Clayton17f3d052011-08-22 22:30:57 +0000275OperatingSystemDarwinKernel::ThreadWasSelected (Thread *thread)
Greg Clayton37f962e2011-08-22 02:49:39 +0000276{
277}
278
279RegisterContextSP
Greg Clayton17f3d052011-08-22 22:30:57 +0000280OperatingSystemDarwinKernel::CreateRegisterContextForThread (Thread *thread)
Greg Clayton37f962e2011-08-22 02:49:39 +0000281{
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 Granata91544802011-09-06 19:20:51 +0000295 base_addr = gpr_valobj_sp->GetPointerValue (&addr_type);
Greg Clayton37f962e2011-08-22 02:49:39 +0000296 else
Enrico Granata91544802011-09-06 19:20:51 +0000297 base_addr = gpr_valobj_sp->GetAddressOf (true, &addr_type);
Greg Clayton37f962e2011-08-22 02:49:39 +0000298 reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), base_addr));
299 }
300 }
301 return reg_ctx_sp;
302}
303
304StopInfoSP
Greg Clayton17f3d052011-08-22 22:30:57 +0000305OperatingSystemDarwinKernel::CreateThreadStopReason (lldb_private::Thread *thread)
Greg Clayton37f962e2011-08-22 02:49:39 +0000306{
307 StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
308 return stop_info_sp;
309}
310
311