Commit | Line | Data |
---|---|---|
2838cc1d SD |
1 | /* Copyright (C) 2009-2016 Free Software Foundation, Inc. |
2 | ||
3 | This file is part of GDB. | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #include <stdint.h> | |
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | ||
23 | #include JIT_READER_H /* Please see jit-reader.exp for an explanation. */ | |
24 | #include "jithost.h" | |
25 | ||
26 | GDB_DECLARE_GPL_COMPATIBLE_READER; | |
27 | ||
28 | enum register_mapping | |
29 | { | |
30 | AMD64_RA = 16, | |
31 | AMD64_RSP = 7, | |
32 | }; | |
33 | ||
34 | struct reader_state | |
35 | { | |
36 | uintptr_t code_begin; | |
37 | uintptr_t code_end; | |
38 | }; | |
39 | ||
40 | static enum gdb_status | |
41 | read_debug_info (struct gdb_reader_funcs *self, | |
42 | struct gdb_symbol_callbacks *cbs, | |
43 | void *memory, long memory_sz) | |
44 | { | |
45 | struct jithost_abi *symfile = memory; | |
46 | struct gdb_object *object = cbs->object_open (cbs); | |
47 | struct gdb_symtab *symtab = cbs->symtab_open (cbs, object, ""); | |
48 | GDB_CORE_ADDR begin = (GDB_CORE_ADDR) symfile->begin; | |
49 | GDB_CORE_ADDR end = (GDB_CORE_ADDR) symfile->end; | |
50 | ||
51 | cbs->block_open (cbs, symtab, NULL, begin, end, "jit_function_00"); | |
52 | ||
53 | cbs->symtab_close (cbs, symtab); | |
54 | cbs->object_close (cbs, object); | |
55 | return GDB_SUCCESS; | |
56 | } | |
57 | ||
58 | static void | |
59 | free_reg_value (struct gdb_reg_value *value) | |
60 | { | |
61 | free (value); | |
62 | } | |
63 | ||
64 | static void | |
65 | write_register (struct gdb_unwind_callbacks *callbacks, int dw_reg, | |
66 | uintptr_t value) | |
67 | { | |
68 | const int size = sizeof (uintptr_t); | |
69 | struct gdb_reg_value *reg_val = | |
70 | malloc (sizeof (struct gdb_reg_value) + size - 1); | |
71 | reg_val->defined = 1; | |
72 | reg_val->free = free_reg_value; | |
73 | ||
74 | memcpy (reg_val->value, &value, size); | |
75 | callbacks->reg_set (callbacks, dw_reg, reg_val); | |
76 | } | |
77 | ||
78 | static int | |
79 | read_register (struct gdb_unwind_callbacks *callbacks, int dw_reg, | |
80 | uintptr_t *value) | |
81 | { | |
82 | const int size = sizeof (uintptr_t); | |
83 | struct gdb_reg_value *reg_val = callbacks->reg_get (callbacks, dw_reg); | |
84 | if (reg_val->size != size || !reg_val->defined) | |
85 | { | |
86 | reg_val->free (reg_val); | |
87 | return 0; | |
88 | } | |
89 | memcpy (value, reg_val->value, size); | |
90 | reg_val->free (reg_val); | |
91 | return 1; | |
92 | } | |
93 | ||
94 | static enum gdb_status | |
95 | unwind_frame (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs) | |
96 | { | |
97 | const int word_size = sizeof (uintptr_t); | |
98 | uintptr_t this_sp, this_ip, prev_ip, prev_sp; | |
99 | struct reader_state *state = (struct reader_state *) self->priv_data; | |
100 | ||
101 | if (!read_register (cbs, AMD64_RA, &this_ip)) | |
102 | return GDB_FAIL; | |
103 | ||
104 | if (this_ip >= state->code_end || this_ip < state->code_begin) | |
105 | return GDB_FAIL; | |
106 | ||
107 | if (!read_register (cbs, AMD64_RSP, &this_sp)) | |
108 | return GDB_FAIL; | |
109 | ||
110 | if (cbs->target_read (this_sp, &prev_ip, word_size) == GDB_FAIL) | |
111 | return GDB_FAIL; | |
112 | ||
113 | prev_sp = this_sp + word_size; | |
114 | write_register (cbs, AMD64_RA, prev_ip); | |
115 | write_register (cbs, AMD64_RSP, prev_sp); | |
116 | return GDB_SUCCESS; | |
117 | } | |
118 | ||
119 | static struct gdb_frame_id | |
120 | get_frame_id (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs) | |
121 | { | |
122 | struct reader_state *state = (struct reader_state *) self->priv_data; | |
123 | struct gdb_frame_id frame_id; | |
124 | ||
125 | uintptr_t sp; | |
126 | read_register (cbs, AMD64_RSP, &sp); | |
127 | ||
128 | frame_id.code_address = (GDB_CORE_ADDR) state->code_begin; | |
129 | frame_id.stack_address = (GDB_CORE_ADDR) sp; | |
130 | ||
131 | return frame_id; | |
132 | } | |
133 | ||
134 | static void | |
135 | destroy_reader (struct gdb_reader_funcs *self) | |
136 | { | |
137 | free (self->priv_data); | |
138 | free (self); | |
139 | } | |
140 | ||
141 | struct gdb_reader_funcs * | |
142 | gdb_init_reader (void) | |
143 | { | |
144 | struct reader_state *state = malloc (sizeof (struct reader_state)); | |
145 | struct gdb_reader_funcs *reader_funcs = | |
146 | malloc (sizeof (struct gdb_reader_funcs)); | |
147 | ||
148 | reader_funcs->reader_version = GDB_READER_INTERFACE_VERSION; | |
149 | reader_funcs->priv_data = state; | |
150 | reader_funcs->read = read_debug_info; | |
151 | reader_funcs->unwind = unwind_frame; | |
152 | reader_funcs->get_frame_id = get_frame_id; | |
153 | reader_funcs->destroy = destroy_reader; | |
154 | ||
155 | return reader_funcs; | |
156 | } |