aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
blob: cdb95250b2f4ceaa693b7686a39e2989fc2e17e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//===-- AppleObjCRuntime.cpp -------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "AppleObjCRuntime.h"
#include "AppleObjCTrampolineHandler.h"

#include "clang/AST/Type.h"

#include "lldb/Breakpoint/BreakpointLocation.h"
#include "lldb/Core/ConstString.h"
#include "lldb/Core/Error.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/Section.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"

#include <vector>

using namespace lldb;
using namespace lldb_private;

#define PO_FUNCTION_TIMEOUT_USEC 15*1000*1000

AppleObjCRuntime::~AppleObjCRuntime()
{
}

AppleObjCRuntime::AppleObjCRuntime(Process *process) :
    ObjCLanguageRuntime (process),
    m_read_objc_library (false),
    m_objc_trampoline_handler_ap (),
    m_Foundation_major()
{
    ReadObjCLibraryIfNeeded (process->GetTarget().GetImages());
}

bool
AppleObjCRuntime::GetObjectDescription (Stream &str, ValueObject &valobj)
{
    CompilerType compiler_type(valobj.GetCompilerType());
    bool is_signed;
    // ObjC objects can only be pointers (or numbers that actually represents pointers
    // but haven't been typecast, because reasons..)
    if (!compiler_type.IsIntegerType (is_signed) && !compiler_type.IsPointerType ())
        return false;
    
    // Make the argument list: we pass one arg, the address of our pointer, to the print function.
    Value val;
    
    if (!valobj.ResolveValue(val.GetScalar()))
        return false;
    
    ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
    return GetObjectDescription(str, val, exe_ctx.GetBestExecutionContextScope());
                   
}
bool
AppleObjCRuntime::GetObjectDescription (Stream &strm, Value &value, ExecutionContextScope *exe_scope)
{
    if (!m_read_objc_library)
        return false;
        
    ExecutionContext exe_ctx;
    exe_scope->CalculateExecutionContext(exe_ctx);
    Process *process = exe_ctx.GetProcessPtr();
    if (!process)
        return false;
    
    // We need other parts of the exe_ctx, but the processes have to match.
    assert (m_process == process);
    
    // Get the function address for the print function.
    const Address *function_address = GetPrintForDebuggerAddr();
    if (!function_address)
        return false;
    
    Target *target = exe_ctx.GetTargetPtr();
    CompilerType compiler_type = value.GetCompilerType();
    if (compiler_type)
    {
        if (!ClangASTContext::IsObjCObjectPointerType(compiler_type))
        {
            strm.Printf ("Value doesn't point to an ObjC object.\n");
            return false;
        }
    }
    else 
    {
        // If it is not a pointer, see if we can make it into a pointer.
        ClangASTContext *ast_context = target->GetScratchClangASTContext();
        CompilerType opaque_type = ast_context->GetBasicType(eBasicTypeObjCID);
        if (!opaque_type)
            opaque_type = ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
        //value.SetContext(Value::eContextTypeClangType, opaque_type_ptr);
        value.SetCompilerType (opaque_type);
    }

    ValueList arg_value_list;
    arg_value_list.PushValue(value);
    
    // This is the return value:
    ClangASTContext *ast_context = target->GetScratchClangASTContext();
    
    CompilerType return_compiler_type = ast_context->GetCStringType(true);
    Value ret;
//    ret.SetContext(Value::eContextTypeClangType, return_compiler_type);
    ret.SetCompilerType (return_compiler_type);
    
    if (exe_ctx.GetFramePtr() == NULL)
    {
        Thread *thread = exe_ctx.GetThreadPtr();
        if (thread == NULL)
        {
            exe_ctx.SetThreadSP(process->GetThreadList().GetSelectedThread());
            thread = exe_ctx.GetThreadPtr();
        }
        if (thread)
        {
            exe_ctx.SetFrameSP(thread->GetSelectedFrame());
        }
    }
    
    // Now we're ready to call the function:
    
    StreamString error_stream;
    lldb::addr_t wrapper_struct_addr = LLDB_INVALID_ADDRESS;

    if (!m_print_object_caller_up)
    {
        Error error;
         m_print_object_caller_up.reset(exe_scope->CalculateTarget()->GetFunctionCallerForLanguage (eLanguageTypeObjC,
                                                                                                    return_compiler_type,
                                                                                                    *function_address,
                                                                                                    arg_value_list,
                                                                                                    "objc-object-description",
                                                                                                    error));
        if (error.Fail())
        {
            m_print_object_caller_up.reset();
            strm.Printf("Could not get function runner to call print for debugger function: %s.", error.AsCString());
            return false;
        }
        m_print_object_caller_up->InsertFunction(exe_ctx, wrapper_struct_addr, error_stream);
    }
    else
    {
        m_print_object_caller_up->WriteFunctionArguments(exe_ctx,
                                                         wrapper_struct_addr,
                                                         arg_value_list,
                                                         error_stream);
    }

    

    EvaluateExpressionOptions options;
    options.SetUnwindOnError(true);
    options.SetTryAllThreads(true);
    options.SetStopOthers(true);
    options.SetIgnoreBreakpoints(true);
    options.SetTimeoutUsec(PO_FUNCTION_TIMEOUT_USEC);
    
    ExpressionResults results = m_print_object_caller_up->ExecuteFunction (exe_ctx,
                                                                           &wrapper_struct_addr,
                                                                           options,
                                                                           error_stream,
                                                                           ret);
    if (results != eExpressionCompleted)
    {
        strm.Printf("Error evaluating Print Object function: %d.\n", results);
        return false;
    }
       
    addr_t result_ptr = ret.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
    
    char buf[512];
    size_t cstr_len = 0;    
    size_t full_buffer_len = sizeof (buf) - 1;
    size_t curr_len = full_buffer_len;
    while (curr_len == full_buffer_len)
    {
        Error error;
        curr_len = process->ReadCStringFromMemory(result_ptr + cstr_len, buf, sizeof(buf), error);
        strm.Write (buf, curr_len);
        cstr_len += curr_len;
    }
    return cstr_len > 0;
}

lldb::ModuleSP
AppleObjCRuntime::GetObjCModule ()
{
    ModuleSP module_sp (m_objc_module_wp.lock());
    if (module_sp)
        return module_sp;

    Process *process = GetProcess();
    if (process)
    {
        const ModuleList& modules = process->GetTarget().GetImages();
        for (uint32_t idx = 0; idx < modules.GetSize(); idx++)
        {
            module_sp = modules.GetModuleAtIndex(idx);
            if (AppleObjCRuntime::AppleIsModuleObjCLibrary(module_sp))
            {
                m_objc_module_wp = module_sp;
                return module_sp;
            }
        }
    }
    return ModuleSP();
}

Address *
AppleObjCRuntime::GetPrintForDebuggerAddr()
{
    if (!m_PrintForDebugger_addr.get())
    {
        const ModuleList &modules = m_process->GetTarget().GetImages();
        
        SymbolContextList contexts;
        SymbolContext context;
        
        if ((!modules.FindSymbolsWithNameAndType(ConstString ("_NSPrintForDebugger"), eSymbolTypeCode, contexts)) &&
           (!modules.FindSymbolsWithNameAndType(ConstString ("_CFPrintForDebugger"), eSymbolTypeCode, contexts)))
            return NULL;
        
        contexts.GetContextAtIndex(0, context);
        
        m_PrintForDebugger_addr.reset(new Address(context.symbol->GetAddress()));
    }
    
    return m_PrintForDebugger_addr.get();
}

bool
AppleObjCRuntime::CouldHaveDynamicValue (ValueObject &in_value)
{
    return in_value.GetCompilerType().IsPossibleDynamicType (NULL,
                                                          false, // do not check C++
                                                          true); // check ObjC
}

bool
AppleObjCRuntime::GetDynamicTypeAndAddress (ValueObject &in_value, 
                                            lldb::DynamicValueType use_dynamic, 
                                            TypeAndOrName &class_type_or_name, 
                                            Address &address,
                                            Value::ValueType &value_type)
{
    return false;
}

TypeAndOrName
AppleObjCRuntime::FixUpDynamicType (const TypeAndOrName& type_and_or_name,
                                    ValueObject& static_value)
{
    CompilerType static_type(static_value.GetCompilerType());
    Flags static_type_flags(static_type.GetTypeInfo());
    
    TypeAndOrName ret(type_and_or_name);
    if (type_and_or_name.HasType())
    {
        // The type will always be the type of the dynamic object.  If our parent's type was a pointer,
        // then our type should be a pointer to the type of the dynamic object.  If a reference, then the original type
        // should be okay...
        CompilerType orig_type = type_and_or_name.GetCompilerType();
        CompilerType corrected_type = orig_type;
        if (static_type_flags.AllSet(eTypeIsPointer))
            corrected_type = orig_type.GetPointerType ();
        ret.SetCompilerType(corrected_type);
    }
    else
    {
        // If we are here we need to adjust our dynamic type name to include the correct & or * symbol
        std::string corrected_name (type_and_or_name.GetName().GetCString());
        if (static_type_flags.AllSet(eTypeIsPointer))
            corrected_name.append(" *");
        // the parent type should be a correctly pointer'ed or referenc'ed type
        ret.SetCompilerType(static_type);
        ret.SetName(corrected_name.c_str());
    }
    return ret;
}

bool
AppleObjCRuntime::AppleIsModuleObjCLibrary (const ModuleSP &module_sp)
{
    if (module_sp)
    {
        const FileSpec &module_file_spec = module_sp->GetFileSpec();
        static ConstString ObjCName ("libobjc.A.dylib");
        
        if (module_file_spec)
        {
            if (module_file_spec.GetFilename() == ObjCName)
                return true;
        }
    }
    return false;
}

// we use the version of Foundation to make assumptions about the ObjC runtime on a target
uint32_t
AppleObjCRuntime::GetFoundationVersion ()
{
    if (!m_Foundation_major.hasValue())
    {
        const ModuleList& modules = m_process->GetTarget().GetImages();
        uint32_t major = UINT32_MAX;
        for (uint32_t idx = 0; idx < modules.GetSize(); idx++)
        {
            lldb::ModuleSP module_sp = modules.GetModuleAtIndex(idx);
            if (!module_sp)
                continue;
            if (strcmp(module_sp->GetFileSpec().GetFilename().AsCString(""),"Foundation") == 0)
            {
                module_sp->GetVersion(&major,1);
                m_Foundation_major = major;
                return major;
            }
        }
        return LLDB_INVALID_MODULE_VERSION;
    }
    else
        return m_Foundation_major.getValue();
}

bool
AppleObjCRuntime::IsModuleObjCLibrary (const ModuleSP &module_sp)
{
    return AppleIsModuleObjCLibrary(module_sp);
}

bool
AppleObjCRuntime::ReadObjCLibrary (const ModuleSP &module_sp)
{
    // Maybe check here and if we have a handler already, and the UUID of this module is the same as the one in the
    // current module, then we don't have to reread it?
    m_objc_trampoline_handler_ap.reset(new AppleObjCTrampolineHandler (m_process->shared_from_this(), module_sp));
    if (m_objc_trampoline_handler_ap.get() != NULL)
    {
        m_read_objc_library = true;
        return true;
    }
    else
        return false;
}

ThreadPlanSP
AppleObjCRuntime::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
{
    ThreadPlanSP thread_plan_sp;
    if (m_objc_trampoline_handler_ap.get())
        thread_plan_sp = m_objc_trampoline_handler_ap->GetStepThroughDispatchPlan (thread, stop_others);
    return thread_plan_sp;
}

//------------------------------------------------------------------
// Static Functions
//------------------------------------------------------------------
ObjCLanguageRuntime::ObjCRuntimeVersions
AppleObjCRuntime::GetObjCVersion (Process *process, ModuleSP &objc_module_sp)
{
    if (!process)
        return ObjCRuntimeVersions::eObjC_VersionUnknown;

    Target &target = process->GetTarget();
    if (target.GetArchitecture().GetTriple().getVendor() != llvm::Triple::VendorType::Apple)
        return ObjCRuntimeVersions::eObjC_VersionUnknown;

    const ModuleList &target_modules = target.GetImages();
    Mutex::Locker modules_locker(target_modules.GetMutex());
    
    size_t num_images = target_modules.GetSize();
    for (size_t i = 0; i < num_images; i++)
    {
        ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i);
        // One tricky bit here is that we might get called as part of the initial module loading, but
        // before all the pre-run libraries get winnowed from the module list.  So there might actually
        // be an old and incorrect ObjC library sitting around in the list, and we don't want to look at that.
        // That's why we call IsLoadedInTarget.
        
        if (AppleIsModuleObjCLibrary (module_sp) && module_sp->IsLoadedInTarget(&target))
        {
            objc_module_sp = module_sp;
            ObjectFile *ofile = module_sp->GetObjectFile();
            if (!ofile)
                return ObjCRuntimeVersions::eObjC_VersionUnknown;
            
            SectionList *sections = module_sp->GetSectionList();
            if (!sections)
                return ObjCRuntimeVersions::eObjC_VersionUnknown;
            SectionSP v1_telltale_section_sp = sections->FindSectionByName(ConstString ("__OBJC"));
            if (v1_telltale_section_sp)
            {
                return ObjCRuntimeVersions::eAppleObjC_V1;
            }
            return ObjCRuntimeVersions::eAppleObjC_V2;
        }
    }
            
    return ObjCRuntimeVersions::eObjC_VersionUnknown;
}

void
AppleObjCRuntime::SetExceptionBreakpoints ()
{
    const bool catch_bp = false;
    const bool throw_bp = true;
    const bool is_internal = true;
    
    if (!m_objc_exception_bp_sp)
    {
        m_objc_exception_bp_sp = LanguageRuntime::CreateExceptionBreakpoint (m_process->GetTarget(),
                                                                            GetLanguageType(),
                                                                            catch_bp, 
                                                                            throw_bp, 
                                                                            is_internal);
        if (m_objc_exception_bp_sp)
            m_objc_exception_bp_sp->SetBreakpointKind("ObjC exception");
    }
    else
        m_objc_exception_bp_sp->SetEnabled(true);
}


void
AppleObjCRuntime::ClearExceptionBreakpoints ()
{
    if (!m_process)
        return;
    
    if (m_objc_exception_bp_sp.get())
    {
        m_objc_exception_bp_sp->SetEnabled (false);
    }
}

bool
AppleObjCRuntime::ExceptionBreakpointsAreSet ()
{
    return m_objc_exception_bp_sp && m_objc_exception_bp_sp->IsEnabled();
}

bool
AppleObjCRuntime::ExceptionBreakpointsExplainStop (lldb::StopInfoSP stop_reason)
{
    if (!m_process)
        return false;
    
    if (!stop_reason || 
        stop_reason->GetStopReason() != eStopReasonBreakpoint)
        return false;
    
    uint64_t break_site_id = stop_reason->GetValue();
    return m_process->GetBreakpointSiteList().BreakpointSiteContainsBreakpoint (break_site_id,
                                                                                m_objc_exception_bp_sp->GetID());
}

bool
AppleObjCRuntime::CalculateHasNewLiteralsAndIndexing()
{
    if (!m_process)
        return false;
    
    Target &target(m_process->GetTarget());
    
    static ConstString s_method_signature("-[NSDictionary objectForKeyedSubscript:]");
    static ConstString s_arclite_method_signature("__arclite_objectForKeyedSubscript");
    
    SymbolContextList sc_list;
    
    if (target.GetImages().FindSymbolsWithNameAndType(s_method_signature, eSymbolTypeCode, sc_list) ||
        target.GetImages().FindSymbolsWithNameAndType(s_arclite_method_signature, eSymbolTypeCode, sc_list))
        return true;
    else
        return false;
}

lldb::SearchFilterSP
AppleObjCRuntime::CreateExceptionSearchFilter ()
{
    Target &target = m_process->GetTarget();
    
    if (target.GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple)
    {
        FileSpecList filter_modules;
        filter_modules.Append(FileSpec("libobjc.A.dylib", false));
        return target.GetSearchFilterForModuleList(&filter_modules);
    }
    else
    {
        return LanguageRuntime::CreateExceptionSearchFilter();
    }
}

void
AppleObjCRuntime::ReadObjCLibraryIfNeeded (const ModuleList &module_list)
{
    if (!HasReadObjCLibrary ())
    {
        Mutex::Locker locker (module_list.GetMutex ());

        size_t num_modules = module_list.GetSize();
        for (size_t i = 0; i < num_modules; i++)
        {
            auto mod = module_list.GetModuleAtIndex (i);
            if (IsModuleObjCLibrary (mod))
            {
                ReadObjCLibrary (mod);
                break;
            }
        }
    }
}

void
AppleObjCRuntime::ModulesDidLoad (const ModuleList &module_list)
{
    ReadObjCLibraryIfNeeded (module_list);
}