1 //===-- ItaniumABILanguageRuntime.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 "ItaniumABILanguageRuntime.h"
11 
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/ConstString.h"
14 #include "lldb/Core/Error.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Scalar.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Core/ValueObjectMemory.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/StopInfo.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 
27 #include <vector>
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 static const char *pluginName = "ItaniumABILanguageRuntime";
33 static const char *pluginDesc = "Itanium ABI for the C++ language";
34 static const char *pluginShort = "language.itanium";
35 static const char *vtable_demangled_prefix = "vtable for ";
36 
37 bool
38 ItaniumABILanguageRuntime::CouldHaveDynamicValue (ValueObject &in_value)
39 {
40     return in_value.IsPossibleCPlusPlusDynamicType();
41 }
42 
43 bool
44 ItaniumABILanguageRuntime::GetDynamicTypeAndAddress (ValueObject &in_value,
45                                                      lldb::DynamicValueType use_dynamic,
46                                                      TypeAndOrName &class_type_or_name,
47                                                      Address &dynamic_address)
48 {
49     // For Itanium, if the type has a vtable pointer in the object, it will be at offset 0
50     // in the object.  That will point to the "address point" within the vtable (not the beginning of the
51     // vtable.)  We can then look up the symbol containing this "address point" and that symbol's name
52     // demangled will contain the full class name.
53     // The second pointer above the "address point" is the "offset_to_top".  We'll use that to get the
54     // start of the value object which holds the dynamic type.
55     //
56 
57     // Only a pointer or reference type can have a different dynamic and static type:
58     if (CouldHaveDynamicValue (in_value))
59     {
60         // FIXME: Can we get the Clang Type and ask it if the thing is really virtual?  That would avoid false positives,
61         // at the cost of not looking for the dynamic type of objects if DWARF->Clang gets it wrong.
62 
63         // First job, pull out the address at 0 offset from the object.
64         AddressType address_type;
65         lldb::addr_t original_ptr = in_value.GetPointerValue(&address_type);
66         if (original_ptr == LLDB_INVALID_ADDRESS)
67             return false;
68 
69         Target *target = in_value.GetUpdatePoint().GetTargetSP().get();
70         Process *process = in_value.GetUpdatePoint().GetProcessSP().get();
71 
72         char memory_buffer[16];
73         DataExtractor data(memory_buffer, sizeof(memory_buffer),
74                            process->GetByteOrder(),
75                            process->GetAddressByteSize());
76         size_t address_byte_size = process->GetAddressByteSize();
77         Error error;
78         size_t bytes_read = process->ReadMemory (original_ptr,
79                                                  memory_buffer,
80                                                  address_byte_size,
81                                                  error);
82         if (!error.Success() || (bytes_read != address_byte_size))
83         {
84             return false;
85         }
86 
87         uint32_t offset_ptr = 0;
88         lldb::addr_t vtable_address_point = data.GetAddress (&offset_ptr);
89 
90         if (offset_ptr == 0)
91             return false;
92 
93         // Now find the symbol that contains this address:
94 
95         SymbolContext sc;
96         Address address_point_address;
97         if (target && !target->GetSectionLoadList().IsEmpty())
98         {
99             if (target->GetSectionLoadList().ResolveLoadAddress (vtable_address_point, address_point_address))
100             {
101                 target->GetImages().ResolveSymbolContextForAddress (address_point_address, eSymbolContextSymbol, sc);
102                 Symbol *symbol = sc.symbol;
103                 if (symbol != NULL)
104                 {
105                     const char *name = symbol->GetMangled().GetDemangledName().AsCString();
106                     if (strstr(name, vtable_demangled_prefix) == name)
107                     {
108                          // We are a C++ class, that's good.  Get the class name and look it up:
109                         const char *class_name = name + strlen(vtable_demangled_prefix);
110                         class_type_or_name.SetName (class_name);
111                         TypeList class_types;
112                         uint32_t num_matches = target->GetImages().FindTypes (sc,
113                                                                               ConstString(class_name),
114                                                                               true,
115                                                                               UINT32_MAX,
116                                                                               class_types);
117                         if (num_matches == 1)
118                         {
119                             class_type_or_name.SetTypeSP(class_types.GetTypeAtIndex(0));
120                         }
121                         else if (num_matches > 1)
122                         {
123                             for (size_t i = 0; i < num_matches; i++)
124                             {
125                                 lldb::TypeSP this_type(class_types.GetTypeAtIndex(i));
126                                 if (this_type)
127                                 {
128                                     if (ClangASTContext::IsCXXClassType(this_type->GetClangFullType()))
129                                     {
130                                         // There can only be one type with a given name,
131                                         // so we've just found duplicate definitions, and this
132                                         // one will do as well as any other.
133                                         // We don't consider something to have a dynamic type if
134                                         // it is the same as the static type.  So compare against
135                                         // the value we were handed:
136 
137                                         clang::ASTContext *in_ast_ctx = in_value.GetClangAST ();
138                                         clang::ASTContext *this_ast_ctx = this_type->GetClangAST ();
139                                         if (in_ast_ctx != this_ast_ctx
140                                             || !ClangASTContext::AreTypesSame (in_ast_ctx,
141                                                                                in_value.GetClangType(),
142                                                                                this_type->GetClangFullType()))
143                                         {
144                                             class_type_or_name.SetTypeSP (this_type);
145                                             return true;
146                                         }
147                                         return false;
148                                     }
149                                 }
150                             }
151                         }
152                         else
153                             return false;
154 
155                         // The offset_to_top is two pointers above the address.
156                         Address offset_to_top_address = address_point_address;
157                         int64_t slide = -2 * ((int64_t) target->GetArchitecture().GetAddressByteSize());
158                         offset_to_top_address.Slide (slide);
159 
160                         Error error;
161                         lldb::addr_t offset_to_top_location = offset_to_top_address.GetLoadAddress(target);
162 
163                         size_t bytes_read = process->ReadMemory (offset_to_top_location,
164                                                                  memory_buffer,
165                                                                  address_byte_size,
166                                                                  error);
167 
168                         if (!error.Success() || (bytes_read != address_byte_size))
169                         {
170                             return false;
171                         }
172 
173                         offset_ptr = 0;
174                         int64_t offset_to_top = data.GetMaxS64(&offset_ptr, process->GetAddressByteSize());
175 
176                         // So the dynamic type is a value that starts at offset_to_top
177                         // above the original address.
178                         lldb::addr_t dynamic_addr = original_ptr + offset_to_top;
179                         if (!target->GetSectionLoadList().ResolveLoadAddress (dynamic_addr, dynamic_address))
180                         {
181                             dynamic_address.SetOffset(dynamic_addr);
182                             dynamic_address.SetSection(NULL);
183                         }
184                         return true;
185                     }
186                 }
187             }
188         }
189 
190     }
191 
192     return false;
193 }
194 
195 bool
196 ItaniumABILanguageRuntime::IsVTableName (const char *name)
197 {
198     if (name == NULL)
199         return false;
200 
201     // Can we maybe ask Clang about this?
202     if (strstr (name, "_vptr$") == name)
203         return true;
204     else
205         return false;
206 }
207 
208 //------------------------------------------------------------------
209 // Static Functions
210 //------------------------------------------------------------------
211 lldb_private::LanguageRuntime *
212 ItaniumABILanguageRuntime::CreateInstance (Process *process, lldb::LanguageType language)
213 {
214     // FIXME: We have to check the process and make sure we actually know that this process supports
215     // the Itanium ABI.
216     if (language == eLanguageTypeC_plus_plus)
217         return new ItaniumABILanguageRuntime (process);
218     else
219         return NULL;
220 }
221 
222 void
223 ItaniumABILanguageRuntime::Initialize()
224 {
225     PluginManager::RegisterPlugin (pluginName,
226                                    pluginDesc,
227                                    CreateInstance);
228 }
229 
230 void
231 ItaniumABILanguageRuntime::Terminate()
232 {
233     PluginManager::UnregisterPlugin (CreateInstance);
234 }
235 
236 //------------------------------------------------------------------
237 // PluginInterface protocol
238 //------------------------------------------------------------------
239 const char *
240 ItaniumABILanguageRuntime::GetPluginName()
241 {
242     return pluginName;
243 }
244 
245 const char *
246 ItaniumABILanguageRuntime::GetShortPluginName()
247 {
248     return pluginShort;
249 }
250 
251 uint32_t
252 ItaniumABILanguageRuntime::GetPluginVersion()
253 {
254     return 1;
255 }
256 
257 void
258 ItaniumABILanguageRuntime::SetExceptionBreakpoints ()
259 {
260     if (!m_process)
261         return;
262 
263     if (!m_cxx_exception_bp_sp)
264         m_cxx_exception_bp_sp = m_process->GetTarget().CreateBreakpoint (NULL,
265                                                                          NULL,
266                                                                          "__cxa_throw",
267                                                                          eFunctionNameTypeBase,
268                                                                          true);
269     else
270         m_cxx_exception_bp_sp->SetEnabled (true);
271 
272     if (!m_cxx_exception_alloc_bp_sp)
273         m_cxx_exception_alloc_bp_sp = m_process->GetTarget().CreateBreakpoint (NULL,
274                                                                                NULL,
275                                                                                "__cxa_allocate",
276                                                                                eFunctionNameTypeBase,
277                                                                                true);
278     else
279         m_cxx_exception_alloc_bp_sp->SetEnabled (true);
280 }
281 
282 void
283 ItaniumABILanguageRuntime::ClearExceptionBreakpoints ()
284 {
285     if (!m_process)
286         return;
287 
288     if (m_cxx_exception_bp_sp.get())
289     {
290         m_cxx_exception_bp_sp->SetEnabled (false);
291     }
292 
293     if (m_cxx_exception_alloc_bp_sp.get())
294     {
295         m_cxx_exception_bp_sp->SetEnabled (false);
296     }
297 }
298 
299 bool
300 ItaniumABILanguageRuntime::ExceptionBreakpointsExplainStop (lldb::StopInfoSP stop_reason)
301 {
302     if (!m_process)
303         return false;
304 
305     if (!stop_reason ||
306         stop_reason->GetStopReason() != eStopReasonBreakpoint)
307         return false;
308 
309     uint64_t break_site_id = stop_reason->GetValue();
310     lldb::BreakpointSiteSP bp_site_sp = m_process->GetBreakpointSiteList().FindByID(break_site_id);
311 
312     if (!bp_site_sp)
313         return false;
314 
315     uint32_t num_owners = bp_site_sp->GetNumberOfOwners();
316 
317     bool        check_cxx_exception = false;
318     break_id_t  cxx_exception_bid;
319 
320     bool        check_cxx_exception_alloc = false;
321     break_id_t  cxx_exception_alloc_bid;
322 
323     if (m_cxx_exception_bp_sp)
324     {
325         check_cxx_exception = true;
326         cxx_exception_bid = m_cxx_exception_bp_sp->GetID();
327     }
328 
329     if (m_cxx_exception_alloc_bp_sp)
330     {
331         check_cxx_exception_alloc = true;
332         cxx_exception_alloc_bid = m_cxx_exception_alloc_bp_sp->GetID();
333     }
334 
335     for (uint32_t i = 0; i < num_owners; i++)
336     {
337         break_id_t bid = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().GetID();
338 
339         if ((check_cxx_exception        && (bid == cxx_exception_bid)) ||
340             (check_cxx_exception_alloc  && (bid == cxx_exception_alloc_bid)))
341             return true;
342     }
343 
344     return false;
345 }
346