Greg Clayton | 07a1d37 | 2013-06-13 18:03:11 +0000 | [diff] [blame^] | 1 | //===-- main.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 <stdint.h> |
| 11 | #include <stdlib.h> |
| 12 | |
| 13 | #include <LLDB/LLDB.h> |
| 14 | |
| 15 | using namespace lldb; |
| 16 | |
| 17 | //---------------------------------------------------------------------- |
| 18 | // This quick sample code shows how to create a debugger instance and |
| 19 | // create an executable target without adding dependent shared |
| 20 | // libraries. It will then set a regular expression breakpoint to get |
| 21 | // breakpoint locations for all functions in the module, and use the |
| 22 | // locations to extract the symbol context for each location. Then it |
| 23 | // dumps all // information about the function: its name, file address |
| 24 | // range, the return type (if any), and all argument types. |
| 25 | // |
| 26 | // To build the program, type (while in this directory): |
| 27 | // |
| 28 | // $ make |
| 29 | // |
| 30 | // then to run this on MacOSX, specify the path to your LLDB.framework |
| 31 | // library using the DYLD_FRAMEWORK_PATH option and run the executable |
| 32 | // |
| 33 | // $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/tot/build/Debug ./a.out executable_path1 [executable_path2 ...] |
| 34 | //---------------------------------------------------------------------- |
| 35 | class LLDBSentry |
| 36 | { |
| 37 | public: |
| 38 | LLDBSentry() { |
| 39 | // Initialize LLDB |
| 40 | SBDebugger::Initialize(); |
| 41 | } |
| 42 | ~LLDBSentry() { |
| 43 | // Terminate LLDB |
| 44 | SBDebugger::Terminate(); |
| 45 | } |
| 46 | }; |
| 47 | int |
| 48 | main (int argc, char const *argv[]) |
| 49 | { |
| 50 | // Use a sentry object to properly initialize/terminate LLDB. |
| 51 | LLDBSentry sentry; |
| 52 | |
| 53 | if (argc < 2) |
| 54 | exit (1); |
| 55 | |
| 56 | const char *arch = NULL; // Fill this in with "x86_64" or "i386" as needed |
| 57 | const char *platform = NULL; // Leave NULL for native platform, set to a valid other platform name if required |
| 58 | const bool add_dependent_libs = false; |
| 59 | SBError error; |
| 60 | for (int arg_idx = 1; arg_idx < argc; ++arg_idx) |
| 61 | { |
| 62 | // The first argument is the file path we want to look something up in |
| 63 | const char *exe_file_path = argv[arg_idx]; |
| 64 | |
| 65 | // Create a debugger instance so we can create a target |
| 66 | SBDebugger debugger (SBDebugger::Create()); |
| 67 | |
| 68 | if (debugger.IsValid()) |
| 69 | { |
| 70 | // Create a target using the executable. |
| 71 | SBTarget target = debugger.CreateTarget (exe_file_path, |
| 72 | arch, |
| 73 | platform, |
| 74 | add_dependent_libs, |
| 75 | error); |
| 76 | |
| 77 | if (error.Success()) |
| 78 | { |
| 79 | if (target.IsValid()) |
| 80 | { |
| 81 | SBFileSpec exe_file_spec (exe_file_path, true); |
| 82 | SBModule module (target.FindModule (exe_file_spec)); |
| 83 | SBFileSpecList comp_unit_list; |
| 84 | |
| 85 | if (module.IsValid()) |
| 86 | { |
| 87 | char command[1024]; |
| 88 | lldb::SBCommandReturnObject command_result; |
| 89 | snprintf (command, sizeof(command), "add-dsym --uuid %s", module.GetUUIDString()); |
| 90 | debugger.GetCommandInterpreter().HandleCommand (command, command_result); |
| 91 | if (!command_result.Succeeded()) |
| 92 | { |
| 93 | fprintf (stderr, "error: couldn't locate debug symbols for '%s'\n", exe_file_path); |
| 94 | exit(1); |
| 95 | } |
| 96 | |
| 97 | SBFileSpecList module_list; |
| 98 | module_list.Append(exe_file_spec); |
| 99 | SBBreakpoint bp = target.BreakpointCreateByRegex (".", module_list, comp_unit_list); |
| 100 | |
| 101 | const size_t num_locations = bp.GetNumLocations(); |
| 102 | for (uint32_t bp_loc_idx=0; bp_loc_idx<num_locations; ++bp_loc_idx) |
| 103 | { |
| 104 | SBBreakpointLocation bp_loc = bp.GetLocationAtIndex(bp_loc_idx); |
| 105 | SBSymbolContext sc (bp_loc.GetAddress().GetSymbolContext(eSymbolContextEverything)); |
| 106 | if (sc.IsValid()) |
| 107 | { |
| 108 | if (sc.GetBlock().GetContainingInlinedBlock().IsValid()) |
| 109 | { |
| 110 | // Skip inlined functions |
| 111 | continue; |
| 112 | } |
| 113 | SBFunction function (sc.GetFunction()); |
| 114 | if (function.IsValid()) |
| 115 | { |
| 116 | addr_t lo_pc = function.GetStartAddress().GetFileAddress(); |
| 117 | if (lo_pc == LLDB_INVALID_ADDRESS) |
| 118 | { |
| 119 | // Skip functions that don't have concrete instances in the binary |
| 120 | continue; |
| 121 | } |
| 122 | addr_t hi_pc = function.GetEndAddress().GetFileAddress(); |
| 123 | |
| 124 | printf ("\nfunction name: %s\n", function.GetName()); |
| 125 | printf ("function range:[0x%llx - 0x%llx)\n", lo_pc, hi_pc); |
| 126 | SBType function_type = function.GetType(); |
| 127 | SBType return_type = function_type.GetFunctionReturnType(); |
| 128 | if (return_type.IsValid()) |
| 129 | { |
| 130 | printf ("return type: %s\n", return_type.GetName()); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | printf ("return type: <NONE>\n"); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | SBTypeList function_args = function_type.GetFunctionArgumentTypes(); |
| 139 | const size_t num_function_args = function_args.GetSize(); |
| 140 | for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args; ++function_arg_idx) |
| 141 | { |
| 142 | SBType function_arg_type = function_args.GetTypeAtIndex(function_arg_idx); |
| 143 | if (function_arg_type.IsValid()) |
| 144 | { |
| 145 | printf ("arg[%u] type: %s\n", function_arg_idx, function_arg_type.GetName()); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | printf ("arg[%u] type: <invalid>\n", function_arg_idx); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | fprintf (stderr, "error: %s\n", error.GetCString()); |
| 162 | exit(1); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |