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
use libc::{c_char, c_int, c_uchar, c_uint, c_void, pid_t, ptrdiff_t, size_t, FILE};

use crate::*;

pub enum Dwfl {}
pub enum Dwfl_Module {}
pub enum Dwfl_Thread {}
pub enum Dwfl_Frame {}
pub enum Dwfl_Line {}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Dwfl_Callbacks {
    pub find_elf: Option<
        unsafe extern "C" fn(
            mod_: *mut Dwfl_Module,
            userdata: *mut *mut c_void,
            modname: *const c_char,
            base: Dwarf_Addr,
            file_name: *mut *mut c_char,
            elfp: *mut *mut Elf,
        ) -> c_int,
    >,
    pub find_debuginfo: Option<
        unsafe extern "C" fn(
            mod_: *mut Dwfl_Module,
            userdata: *mut *mut c_void,
            modname: *const c_char,
            base: Dwarf_Addr,
            file_name: *const c_char,
            debuglink_file: *const c_char,
            debuglink_crc: GElf_Word,
            debuginfo_file_name: *mut *mut c_char,
        ) -> c_int,
    >,
    pub section_address: Option<
        unsafe extern "C" fn(
            mod_: *mut Dwfl_Module,
            userdata: *mut *mut c_void,
            modname: *const c_char,
            base: Dwarf_Addr,
            secname: *const c_char,
            shndx: GElf_Word,
            shdr: *const GElf_Shdr,
            addr: *mut Dwarf_Addr,
        ) -> c_int,
    >,
    pub debuginfo_path: *mut *mut c_char,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Dwfl_Thread_Callbacks {
    pub next_thread: Option<
        unsafe extern "C" fn(dwfl: *mut Dwfl, dwfl_arg: *mut c_void, thread_argp: *mut *mut c_void)
            -> pid_t,
    >,
    pub get_thread: Option<
        unsafe extern "C" fn(
            dwfl: *mut Dwfl,
            tid: pid_t,
            dwfl_arg: *mut c_void,
            thread_argp: *mut *mut c_void,
        ) -> bool,
    >,
    pub memory_read: Option<
        unsafe extern "C" fn(
            dwfl: *mut Dwfl,
            addr: Dwarf_Addr,
            result: *mut Dwarf_Word,
            dwfl_arg: *mut c_void,
        ) -> bool,
    >,
    pub set_initial_registers:
        Option<unsafe extern "C" fn(thread: *mut Dwfl_Thread, thread_arg: *mut c_void) -> bool>,
    pub detach: Option<unsafe extern "C" fn(dwfl: *mut Dwfl, dwfl_arg: *mut c_void)>,
    pub thread_detach:
        Option<unsafe extern "C" fn(thread: *mut Dwfl_Thread, thread_arg: *mut c_void)>,
}

extern "C" {
    pub fn dwfl_begin(callbacks: *const Dwfl_Callbacks) -> *mut Dwfl;

    pub fn dwfl_end(dwfl: *mut Dwfl);

    pub fn dwfl_version(dwfl: *mut Dwfl) -> *const c_char;

    pub fn dwfl_errno() -> c_int;

    pub fn dwfl_errmsg(err: c_int) -> *const c_char;

    pub fn dwfl_report_begin(dwfl: *mut Dwfl);

    pub fn dwfl_report_segment(
        dwfl: *mut Dwfl,
        ndx: c_int,
        phdr: *const GElf_Phdr,
        bias: GElf_Addr,
        ident: *const c_void,
    ) -> c_int;

    pub fn dwfl_report_module(
        dwfl: *mut Dwfl,
        name: *const c_char,
        start: Dwarf_Addr,
        end: Dwarf_Addr,
    ) -> *mut Dwfl_Module;

    pub fn dwfl_report_elf(
        dwfl: *mut Dwfl,
        name: *const c_char,
        file_name: *const c_char,
        fd: c_int,
        base: GElf_Addr,
        add_p_vaddr: bool,
    ) -> *mut Dwfl_Module;

    pub fn dwfl_report_offline(
        dwfl: *mut Dwfl,
        name: *const c_char,
        file_name: *const c_char,
        fd: c_int,
    ) -> *mut Dwfl_Module;

    pub fn dwfl_report_end(
        dwfl: *mut Dwfl,
        removed: Option<
            unsafe extern "C" fn(
                _: *mut Dwfl_Module,
                _: *mut c_void,
                _: *const c_char,
                _: Dwarf_Addr,
                arg: *mut c_void,
            ) -> c_int,
        >,
        arg: *mut c_void,
    ) -> c_int;

    pub fn dwfl_report_begin_add(dwfl: *mut Dwfl);

    pub fn dwfl_module_info(
        mod_: *mut Dwfl_Module,
        userdata: *mut *mut *mut c_void,
        start: *mut Dwarf_Addr,
        end: *mut Dwarf_Addr,
        dwbias: *mut Dwarf_Addr,
        symbias: *mut Dwarf_Addr,
        mainfile: *mut *const c_char,
        debugfile: *mut *const c_char,
    ) -> *const c_char;

    pub fn dwfl_getmodules(
        dwfl: *mut Dwfl,
        callback: Option<
            unsafe extern "C" fn(
                _: *mut Dwfl_Module,
                _: *mut *mut c_void,
                _: *const c_char,
                _: Dwarf_Addr,
                arg: *mut c_void,
            ) -> c_int,
        >,
        arg: *mut c_void,
        offset: ptrdiff_t,
    ) -> ptrdiff_t;

    pub fn dwfl_addrmodule(dwfl: *mut Dwfl, address: Dwarf_Addr) -> *mut Dwfl_Module;

    pub fn dwfl_addrsegment(
        dwfl: *mut Dwfl,
        address: Dwarf_Addr,
        mod_: *mut *mut Dwfl_Module,
    ) -> c_int;

    pub fn dwfl_module_report_build_id(
        mod_: *mut Dwfl_Module,
        bits: *const c_uchar,
        len: size_t,
        vaddr: GElf_Addr,
    ) -> c_int;

    pub fn dwfl_module_build_id(
        mod_: *mut Dwfl_Module,
        bits: *mut *const c_uchar,
        vaddr: *mut GElf_Addr,
    ) -> c_int;

    pub fn dwfl_build_id_find_elf(
        _: *mut Dwfl_Module,
        _: *mut *mut c_void,
        _: *const c_char,
        _: Dwarf_Addr,
        _: *mut *mut c_char,
        _: *mut *mut Elf,
    ) -> c_int;

    pub fn dwfl_build_id_find_debuginfo(
        _: *mut Dwfl_Module,
        _: *mut *mut c_void,
        _: *const c_char,
        _: Dwarf_Addr,
        _: *const c_char,
        _: *const c_char,
        _: GElf_Word,
        _: *mut *mut c_char,
    ) -> c_int;

    pub fn dwfl_standard_find_debuginfo(
        _: *mut Dwfl_Module,
        _: *mut *mut c_void,
        _: *const c_char,
        _: Dwarf_Addr,
        _: *const c_char,
        _: *const c_char,
        _: GElf_Word,
        _: *mut *mut c_char,
    ) -> c_int;

    pub fn dwfl_linux_kernel_find_elf(
        _: *mut Dwfl_Module,
        _: *mut *mut c_void,
        _: *const c_char,
        _: Dwarf_Addr,
        _: *mut *mut c_char,
        _: *mut *mut Elf,
    ) -> c_int;

    pub fn dwfl_linux_kernel_module_section_address(
        _: *mut Dwfl_Module,
        _: *mut *mut c_void,
        _: *const c_char,
        _: Dwarf_Addr,
        _: *const c_char,
        _: GElf_Word,
        _: *const GElf_Shdr,
        addr: *mut Dwarf_Addr,
    ) -> c_int;

    pub fn dwfl_linux_kernel_report_kernel(dwfl: *mut Dwfl) -> c_int;

    pub fn dwfl_linux_kernel_report_modules(dwfl: *mut Dwfl) -> c_int;

    pub fn dwfl_linux_kernel_report_offline(
        dwfl: *mut Dwfl,
        release: *const c_char,
        predicate: Option<unsafe extern "C" fn(_: *const c_char, *const c_char) -> c_int>,
    ) -> c_int;

    pub fn dwfl_core_file_report(
        dwfl: *mut Dwfl,
        elf: *mut Elf,
        executable: *const c_char,
    ) -> c_int;

    pub fn dwfl_linux_proc_report(dwfl: *mut Dwfl, pid: pid_t) -> c_int;

    pub fn dwfl_linux_proc_maps_report(dwfl: *mut Dwfl, file: *mut FILE) -> c_int;

    pub fn dwfl_linux_proc_find_elf(
        mod_: *mut Dwfl_Module,
        userdata: *mut *mut c_void,
        module_name: *const c_char,
        base: Dwarf_Addr,
        file_name: *mut *mut c_char,
        _: *mut *mut Elf,
    ) -> c_int;

    pub fn dwfl_module_relocations(mod_: *mut Dwfl_Module) -> c_int;

    pub fn dwfl_module_relocate_address(mod_: *mut Dwfl_Module, address: *mut Dwarf_Addr) -> c_int;

    pub fn dwfl_module_relocation_info(
        mod_: *mut Dwfl_Module,
        idx: c_uint,
        shndxp: *mut GElf_Word,
    ) -> *const c_char;

    pub fn dwfl_module_getelf(_: *mut Dwfl_Module, bias: *mut GElf_Addr) -> *mut Elf;

    pub fn dwfl_module_getsymtab(mod_: *mut Dwfl_Module) -> c_int;

    pub fn dwfl_module_getsymtab_first_global(mod_: *mut Dwfl_Module) -> c_int;

    pub fn dwfl_module_getsym(
        mod_: *mut Dwfl_Module,
        ndx: c_int,
        sym: *mut GElf_Sym,
        shndxp: *mut GElf_Word,
    ) -> *const c_char;

    pub fn dwfl_module_getsym_info(
        mod_: *mut Dwfl_Module,
        ndx: c_int,
        sym: *mut GElf_Sym,
        addr: *mut GElf_Addr,
        shndxp: *mut GElf_Word,
        elfp: *mut *mut Elf,
        bias: *mut Dwarf_Addr,
    ) -> *const c_char;

    pub fn dwfl_module_addrname(mod_: *mut Dwfl_Module, address: GElf_Addr) -> *const c_char;

    pub fn dwfl_module_addrinfo(
        mod_: *mut Dwfl_Module,
        address: GElf_Addr,
        offset: *mut GElf_Off,
        sym: *mut GElf_Sym,
        shndxp: *mut GElf_Word,
        elfp: *mut *mut Elf,
        bias: *mut Dwarf_Addr,
    ) -> *const c_char;

    pub fn dwfl_module_addrsym(
        mod_: *mut Dwfl_Module,
        address: GElf_Addr,
        sym: *mut GElf_Sym,
        shndxp: *mut GElf_Word,
    ) -> *const c_char;

    pub fn dwfl_module_address_section(
        mod_: *mut Dwfl_Module,
        address: *mut Dwarf_Addr,
        bias: *mut Dwarf_Addr,
    ) -> *mut Elf_Scn;

    pub fn dwfl_module_getdwarf(_: *mut Dwfl_Module, bias: *mut Dwarf_Addr) -> *mut Dwarf;

    pub fn dwfl_getdwarf(
        _: *mut Dwfl,
        callback: Option<
            unsafe extern "C" fn(
                _: *mut Dwfl_Module,
                _: *mut *mut c_void,
                _: *const c_char,
                _: Dwarf_Addr,
                _: *mut Dwarf,
                _: Dwarf_Addr,
                _: *mut c_void,
            ) -> c_int,
        >,
        arg: *mut c_void,
        offset: ptrdiff_t,
    ) -> ptrdiff_t;

    pub fn dwfl_addrdwarf(dwfl: *mut Dwfl, addr: Dwarf_Addr, bias: *mut Dwarf_Addr) -> *mut Dwarf;

    pub fn dwfl_addrdie(dwfl: *mut Dwfl, addr: Dwarf_Addr, bias: *mut Dwarf_Addr)
        -> *mut Dwarf_Die;

    pub fn dwfl_module_addrdie(
        mod_: *mut Dwfl_Module,
        addr: Dwarf_Addr,
        bias: *mut Dwarf_Addr,
    ) -> *mut Dwarf_Die;

    pub fn dwfl_nextcu(
        dwfl: *mut Dwfl,
        lastcu: *mut Dwarf_Die,
        bias: *mut Dwarf_Addr,
    ) -> *mut Dwarf_Die;

    pub fn dwfl_module_nextcu(
        mod_: *mut Dwfl_Module,
        lastcu: *mut Dwarf_Die,
        bias: *mut Dwarf_Addr,
    ) -> *mut Dwarf_Die;

    pub fn dwfl_cumodule(cudie: *mut Dwarf_Die) -> *mut Dwfl_Module;

    pub fn dwfl_getsrclines(cudie: *mut Dwarf_Die, nlines: *mut size_t) -> c_int;

    pub fn dwfl_module_getsrc(mod_: *mut Dwfl_Module, addr: Dwarf_Addr) -> *mut Dwfl_Line;

    pub fn dwfl_getsrc(dwfl: *mut Dwfl, addr: Dwarf_Addr) -> *mut Dwfl_Line;

    pub fn dwfl_module_getsrc_file(
        mod_: *mut Dwfl_Module,
        fname: *const c_char,
        lineno: c_int,
        column: c_int,
        srcp: *mut *mut *mut Dwfl_Line,
        nsrcs: *mut size_t,
    ) -> c_int;

    pub fn dwfl_linemodule(line: *mut Dwfl_Line) -> *mut Dwfl_Module;

    pub fn dwfl_linecu(line: *mut Dwfl_Line) -> *mut Dwarf_Die;

    pub fn dwfl_lineinfo(
        line: *mut Dwfl_Line,
        addr: *mut Dwarf_Addr,
        linep: *mut c_int,
        colp: *mut c_int,
        mtime: *mut Dwarf_Word,
        length: *mut Dwarf_Word,
    ) -> *const c_char;

    pub fn dwfl_dwarf_line(line: *mut Dwfl_Line, bias: *mut Dwarf_Addr) -> *mut Dwarf_Line;

    pub fn dwfl_line_comp_dir(line: *mut Dwfl_Line) -> *const c_char;

    pub fn dwfl_module_return_value_location(
        mod_: *mut Dwfl_Module,
        functypedie: *mut Dwarf_Die,
        locops: *mut *const Dwarf_Op,
    ) -> c_int;

    pub fn dwfl_module_register_names(
        mod_: *mut Dwfl_Module,
        callback: Option<
            unsafe extern "C" fn(
                arg: *mut c_void,
                regno: c_int,
                setname: *const c_char,
                prefix: *const c_char,
                regname: *const c_char,
                bits: c_int,
                type_: c_int,
            ) -> c_int,
        >,
        arg: *mut c_void,
    ) -> c_int;

    pub fn dwfl_module_dwarf_cfi(mod_: *mut Dwfl_Module, bias: *mut Dwarf_Addr) -> *mut Dwarf_CFI;

    pub fn dwfl_module_eh_cfi(mod_: *mut Dwfl_Module, bias: *mut Dwarf_Addr) -> *mut Dwarf_CFI;

    pub fn dwfl_attach_state(
        dwfl: *mut Dwfl,
        elf: *mut Elf,
        pid: pid_t,
        thread_callbacks: *const Dwfl_Thread_Callbacks,
        dwfl_arg: *mut c_void,
    ) -> bool;

    pub fn dwfl_core_file_attach(dwfl: *mut Dwfl, elf: *mut Elf) -> c_int;

    pub fn dwfl_linux_proc_attach(
        dwfl: *mut Dwfl,
        pid: pid_t,
        assume_ptrace_stopped: bool,
    ) -> c_int;

    pub fn dwfl_pid(dwfl: *mut Dwfl) -> pid_t;

    pub fn dwfl_thread_dwfl(thread: *mut Dwfl_Thread) -> *mut Dwfl;

    pub fn dwfl_thread_tid(thread: *mut Dwfl_Thread) -> pid_t;

    pub fn dwfl_frame_thread(state: *mut Dwfl_Frame) -> *mut Dwfl_Thread;

    pub fn dwfl_thread_state_registers(
        thread: *mut Dwfl_Thread,
        firstreg: c_int,
        nregs: c_uint,
        regs: *const Dwarf_Word,
    ) -> bool;

    pub fn dwfl_thread_state_register_pc(thread: *mut Dwfl_Thread, pc: Dwarf_Word);

    pub fn dwfl_getthreads(
        dwfl: *mut Dwfl,
        callback: Option<unsafe extern "C" fn(thread: *mut Dwfl_Thread, arg: *mut c_void) -> c_int>,
        arg: *mut c_void,
    ) -> c_int;

    pub fn dwfl_thread_getframes(
        thread: *mut Dwfl_Thread,
        callback: Option<unsafe extern "C" fn(state: *mut Dwfl_Frame, arg: *mut c_void) -> c_int>,
        arg: *mut c_void,
    ) -> c_int;

    pub fn dwfl_getthread_frames(
        dwl: *mut Dwfl,
        tid: pid_t,
        callback: Option<unsafe extern "C" fn(state: *mut Dwfl_Frame, arg: *mut c_void) -> c_int>,
        arg: *mut c_void,
    ) -> c_int;

    pub fn dwfl_frame_pc(
        state: *mut Dwfl_Frame,
        pc: *mut Dwarf_Addr,
        isactivation: *mut bool,
    ) -> bool;
}