Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ObjCObjectPrinter.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 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
| 14 | #include "lldb/Core/StreamString.h" |
| 15 | #include "lldb/Expression/ClangFunction.h" |
| 16 | #include "lldb/Target/ExecutionContext.h" |
| 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Target/Target.h" |
| 19 | |
Eli Friedman | b1ed516 | 2010-06-09 09:32:42 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ObjCObjectPrinter.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | //---------------------------------------------------------------------- |
| 26 | // ObjCObjectPrinter constructor |
| 27 | //---------------------------------------------------------------------- |
| 28 | ObjCObjectPrinter::ObjCObjectPrinter (Process &process) : |
| 29 | m_process(process) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | //---------------------------------------------------------------------- |
| 34 | // Destructor |
| 35 | //---------------------------------------------------------------------- |
| 36 | ObjCObjectPrinter::~ObjCObjectPrinter () |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | bool |
| 41 | ObjCObjectPrinter::PrintObject (ConstString &str, Value &object_ptr, ExecutionContext &exe_ctx) |
| 42 | { |
| 43 | if (!exe_ctx.process) |
| 44 | return false; |
| 45 | |
| 46 | const Address *function_address = GetPrintForDebuggerAddr(); |
| 47 | |
| 48 | if (!function_address) |
| 49 | return false; |
| 50 | |
| 51 | const char *target_triple = exe_ctx.process->GetTargetTriple().GetCString(); |
| 52 | ClangASTContext *ast_context = exe_ctx.target->GetScratchClangASTContext(); |
| 53 | |
| 54 | void *return_qualtype = ast_context->GetCStringType(true); |
| 55 | Value ret; |
| 56 | ret.SetContext(Value::eContextTypeOpaqueClangQualType, return_qualtype); |
| 57 | |
| 58 | ValueList arg_value_list; |
| 59 | arg_value_list.PushValue(object_ptr); |
| 60 | |
| 61 | ClangFunction func(target_triple, ast_context, return_qualtype, *function_address, arg_value_list); |
| 62 | StreamString error_stream; |
| 63 | |
| 64 | lldb::addr_t wrapper_struct_addr = LLDB_INVALID_ADDRESS; |
| 65 | func.InsertFunction(exe_ctx, wrapper_struct_addr, error_stream); |
| 66 | // FIXME: Check result of ExecuteFunction. |
| 67 | func.ExecuteFunction(exe_ctx, &wrapper_struct_addr, error_stream, true, 1000, true, ret); |
| 68 | |
| 69 | addr_t result_ptr = ret.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 70 | |
| 71 | // poor man's strcpy |
| 72 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | Error error; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame^] | 74 | std::vector<char> desc; |
| 75 | while (1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame^] | 77 | char byte = '\0'; |
| 78 | if (exe_ctx.process->ReadMemory(result_ptr + desc.size(), &byte, 1, error) != 1) |
| 79 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame^] | 81 | desc.push_back(byte); |
| 82 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | if (byte == '\0') |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame^] | 84 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame^] | 87 | if (!desc.empty()) |
| 88 | { |
| 89 | str.SetCString(desc.data()); |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | Address * |
| 96 | ObjCObjectPrinter::GetPrintForDebuggerAddr() |
| 97 | { |
| 98 | if (!m_PrintForDebugger_addr.get()) |
| 99 | { |
| 100 | ModuleList &modules = m_process.GetTarget().GetImages(); |
| 101 | |
| 102 | SymbolContextList contexts; |
| 103 | SymbolContext context; |
| 104 | |
| 105 | if((!modules.FindSymbolsWithNameAndType(ConstString ("_NSPrintForDebugger"), eSymbolTypeCode, contexts)) && |
| 106 | (!modules.FindSymbolsWithNameAndType(ConstString ("_CFPrintForDebugger"), eSymbolTypeCode, contexts))) |
| 107 | return NULL; |
| 108 | |
| 109 | contexts.GetContextAtIndex(0, context); |
| 110 | |
| 111 | m_PrintForDebugger_addr.reset(new Address(context.symbol->GetValue())); |
| 112 | } |
| 113 | |
| 114 | return m_PrintForDebugger_addr.get(); |
| 115 | } |
| 116 | |