summaryrefslogtreecommitdiffstats
path: root/lldb/source/Plugins/Process/Utility/MacOSXLibunwindCallbacks.cpp
blob: e6c7b354abcad8878bfc579f27ed1689fdacf845 (plain)
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
//===-- MacOSXLibunwindCallbacks.cpp ----------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_MacOSXLibunwindCallbacks_cpp_
#define liblldb_MacOSXLibunwindCallbacks_cpp_
#if defined(__cplusplus)

#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/FileSpec.h"
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"

#include "lldb-enumerations.h"
#include "libunwind.h"
#include "llvm-c/EnhancedDisassembly.h"

using namespace lldb;

namespace lldb_private {

/* Don't implement (libunwind does not use)
      find_proc_info
      put_unwind_info
      get_dyn_info_list_addr
      access_mem
      resume
*/
/*
  Should implement (not needed yet)
      access_fpreg
      access_vecreg
      proc_is_sigtramp
      proc_is_inferior_function_call
      access_reg_inf_func_call
*/
  
static int 
access_reg (lldb_private::unw_addr_space_t as, lldb_private::unw_regnum_t regnum, lldb_private::unw_word_t *valp, int write, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    /* FIXME Only support reading for now.  */
    if (write == 1)
        return -1;
    if (th->GetRegisterContext()->GetRegisterInfoAtIndex(regnum) == NULL)
        return -1;
    DataExtractor de;
    if (!th->GetRegisterContext()->ReadRegisterBytes (regnum, de))
        return -1;
    memcpy (valp, de.GetDataStart(), de.GetByteSize());
    return UNW_ESUCCESS;
}

static int 
get_proc_name (lldb_private::unw_addr_space_t as, lldb_private::unw_word_t ip, char *bufp, size_t buf_len, lldb_private::unw_word_t *offp, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    Address addr;
    if (!th->GetProcess().ResolveLoadAddress(ip, addr))
        return -1;
    
    SymbolContext sc;
    if (!th->GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (addr, eSymbolContextFunction, sc))
        return -1;
    if (!sc.symbol)
        return -1;
    strlcpy (bufp, sc.symbol->GetMangled().GetMangledName().AsCString(""), buf_len);
    if (offp)
        *offp = addr.GetLoadAddress(&th->GetProcess()) - sc.symbol->GetValue().GetLoadAddress(&th->GetProcess());
    return UNW_ESUCCESS;
}

static int 
find_image_info (lldb_private::unw_addr_space_t as, lldb_private::unw_word_t load_addr, lldb_private::unw_word_t *mh, 
                 lldb_private::unw_word_t *text_start, lldb_private::unw_word_t *text_end, 
                 lldb_private::unw_word_t *eh_frame, lldb_private::unw_word_t *eh_frame_len, 
                 lldb_private::unw_word_t *compact_unwind_start, lldb_private::unw_word_t *compact_unwind_len, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    Address addr;
    if (!th->GetProcess().ResolveLoadAddress(load_addr, addr))
        return -1;
    
    SymbolContext sc;
    if (!th->GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (addr, eSymbolContextModule, sc))
        return -1;
    
    SectionList *sl = sc.module_sp->GetObjectFile()->GetSectionList();
    static ConstString g_segment_name_TEXT("__TEXT");
    SectionSP text_segment_sp(sl->FindSectionByName(g_segment_name_TEXT));
    if (!text_segment_sp)
        return -1;
    
    *mh = text_segment_sp->GetLoadBaseAddress (&th->GetProcess());
    *text_start = text_segment_sp->GetLoadBaseAddress (&th->GetProcess());
    *text_end = *text_start + text_segment_sp->GetByteSize();
    
    static ConstString g_section_name_eh_frame ("__eh_frame");
    SectionSP eh_frame_section_sp = text_segment_sp->GetChildren().FindSectionByName(g_section_name_eh_frame);
    if (eh_frame_section_sp.get()) {
        *eh_frame = eh_frame_section_sp->GetLoadBaseAddress (&th->GetProcess());
        *eh_frame_len = eh_frame_section_sp->GetByteSize();
    } else {
        *eh_frame = 0;
        *eh_frame_len = 0;
    }
    
    static ConstString g_section_name_unwind_info ("__unwind_info");
    SectionSP unwind_info_section_sp = text_segment_sp->GetChildren().FindSectionByName(g_section_name_unwind_info);
    if (unwind_info_section_sp.get()) {
        *compact_unwind_start = unwind_info_section_sp->GetLoadBaseAddress (&th->GetProcess());
        *compact_unwind_len = unwind_info_section_sp->GetByteSize();
    } else {
        *compact_unwind_start = 0;
        *compact_unwind_len = 0;
    }
    return UNW_ESUCCESS;
}

static int 
get_proc_bounds (lldb_private::unw_addr_space_t as, lldb_private::unw_word_t ip, lldb_private::unw_word_t *low, lldb_private::unw_word_t *high, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    Address addr;
    if (!th->GetProcess().ResolveLoadAddress(ip, addr))
        return -1;
    SymbolContext sc;
    if (!th->GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (addr, eSymbolContextFunction | eSymbolContextSymbol, sc))
        return -1;
    if (sc.function)
    {
        lldb::addr_t start, len;
        start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(&th->GetProcess());
        len = sc.function->GetAddressRange().GetByteSize();
        if (start == LLDB_INVALID_ADDRESS || len == LLDB_INVALID_ADDRESS)
            return -1;
        *low = start;
        *high = start + len;
        return UNW_ESUCCESS;
    }
    if (sc.symbol)
    {
        lldb::addr_t start, len;
        start = sc.symbol->GetAddressRangeRef().GetBaseAddress().GetLoadAddress(&th->GetProcess());
        len = sc.symbol->GetAddressRangeRef().GetByteSize();
        if (start == LLDB_INVALID_ADDRESS)
            return -1;
        *low = start;
        if (len != LLDB_INVALID_ADDRESS)
            *high = start + len;
        else
            *high = 0;
        return UNW_ESUCCESS;
    }
    return -1;
}

static int 
access_raw (lldb_private::unw_addr_space_t as, lldb_private::unw_word_t addr, lldb_private::unw_word_t extent, uint8_t *valp, int write, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    /* FIXME Only support reading for now.  */
    if (write == 1)
        return -1;
    
    Error error;
    if (th->GetProcess().ReadMemory (addr, valp, extent, error) != extent)
        return -1;
    return UNW_ESUCCESS;
}


static int 
reg_info (lldb_private::unw_addr_space_t as, lldb_private::unw_regnum_t regnum, lldb_private::unw_regtype_t *type, char *buf, size_t buflen, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    RegisterContext *regc = th->GetRegisterContext();
    if (regnum > regc->GetRegisterCount())
    {
        *type = UNW_NOT_A_REG;
        return UNW_ESUCCESS;
    }
    
    const char *name = regc->GetRegisterName (regnum);
    if (name == NULL)
    {
        *type = UNW_NOT_A_REG;
        return UNW_ESUCCESS;
    }
    strlcpy (buf, name, buflen);
    
    const lldb::RegisterInfo *reginfo = regc->GetRegisterInfoAtIndex (regnum);
    if (reginfo == NULL || reginfo->encoding == eEncodingInvalid)
    {
        *type = UNW_NOT_A_REG;
        return UNW_ESUCCESS;
    }
    if (reginfo->encoding == eEncodingUint || reginfo->encoding == eEncodingSint)
        *type = UNW_INTEGER_REG;
    if (reginfo->encoding == eEncodingIEEE754)
        *type = UNW_FLOATING_POINT_REG;
    if (reginfo->encoding == eEncodingVector)
        *type = UNW_VECTOR_REG;
    
    return UNW_ESUCCESS;
}


static int
read_byte_for_edis (uint8_t *buf, uint64_t addr, void *arg)
{
    if (arg == 0)
        return -1;
    Thread *th = (Thread *) arg;
    DataBufferHeap onebyte(1, 0);
    Error error;
    if (th->GetProcess().ReadMemory (addr, onebyte.GetBytes(), onebyte.GetByteSize(), error) != 1)
        return -1;
    *buf = onebyte.GetBytes()[0];
    return UNW_ESUCCESS;
}

static int 
instruction_length (lldb_private::unw_addr_space_t as, lldb_private::unw_word_t addr, int *length, void *arg)
{
    EDDisassemblerRef disasm;
    EDInstRef         cur_insn;
    
    if (arg == 0)
        return -1;
    Thread *thread = (Thread *) arg;
    
    const ArchSpec::CPU arch_cpu = thread->GetProcess().GetTarget().GetArchitecture ().GetGenericCPUType();

    if (arch_cpu == ArchSpec::eCPU_i386)
    {
        if (EDGetDisassembler (&disasm, "i386-apple-darwin", kEDAssemblySyntaxX86ATT) != 0)
            return -1;
    }
    else if (arch_cpu == ArchSpec::eCPU_x86_64)
    {
        if (EDGetDisassembler (&disasm, "x86_64-apple-darwin", kEDAssemblySyntaxX86ATT) != 0)
            return -1;
    }
    else
    {
        return -1;
    }
    
    if (EDCreateInsts (&cur_insn, 1, disasm, read_byte_for_edis, addr, arg) != 1)
        return -1;
    *length = EDInstByteSize (cur_insn);
    EDReleaseInst (cur_insn);
    return UNW_ESUCCESS;
}

lldb_private::unw_accessors_t 
get_macosx_libunwind_callbacks () {
  lldb_private::unw_accessors_t ap;
  bzero (&ap, sizeof (lldb_private::unw_accessors_t));
  ap.find_proc_info = NULL;
  ap.put_unwind_info = NULL;
  ap.get_dyn_info_list_addr = NULL;
  ap.find_image_info = find_image_info;
  ap.access_mem = NULL;
  ap.access_reg = access_reg;
  ap.access_fpreg = NULL;
  ap.access_vecreg = NULL;
  ap.resume = NULL;
  ap.get_proc_name = get_proc_name;
  ap.get_proc_bounds = get_proc_bounds;
  ap.access_raw = access_raw;
  ap.reg_info = reg_info;
  ap.proc_is_sigtramp = NULL;
  ap.proc_is_inferior_function_call = NULL;
  ap.access_reg_inf_func_call = NULL;
  ap.instruction_length = instruction_length;
  return ap;
}


} // namespace lldb_private

#endif  // #if defined(__cplusplus)
#endif // #ifndef liblldb_MacOSXLibunwindCallbacks_cpp_
OpenPOWER on IntegriCloud