Commit | Line | Data |
---|---|---|
e2882c85 | 1 | /* Copyright (C) 2009-2018 Free Software Foundation, Inc. |
2838cc1d SD |
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, | |
20aa2c60 | 31 | AMD64_RBP = 6, |
2838cc1d SD |
32 | AMD64_RSP = 7, |
33 | }; | |
34 | ||
35 | struct reader_state | |
36 | { | |
37 | uintptr_t code_begin; | |
38 | uintptr_t code_end; | |
39 | }; | |
40 | ||
41 | static enum gdb_status | |
42 | read_debug_info (struct gdb_reader_funcs *self, | |
43 | struct gdb_symbol_callbacks *cbs, | |
44 | void *memory, long memory_sz) | |
45 | { | |
46 | struct jithost_abi *symfile = memory; | |
47 | struct gdb_object *object = cbs->object_open (cbs); | |
48 | struct gdb_symtab *symtab = cbs->symtab_open (cbs, object, ""); | |
49 | GDB_CORE_ADDR begin = (GDB_CORE_ADDR) symfile->begin; | |
50 | GDB_CORE_ADDR end = (GDB_CORE_ADDR) symfile->end; | |
20aa2c60 PA |
51 | struct reader_state *state = (struct reader_state *) self->priv_data; |
52 | ||
53 | /* Record the function's range, for the unwinder. */ | |
54 | state->code_begin = begin; | |
55 | state->code_end = end; | |
2838cc1d SD |
56 | |
57 | cbs->block_open (cbs, symtab, NULL, begin, end, "jit_function_00"); | |
58 | ||
59 | cbs->symtab_close (cbs, symtab); | |
60 | cbs->object_close (cbs, object); | |
61 | return GDB_SUCCESS; | |
62 | } | |
63 | ||
64 | static void | |
65 | free_reg_value (struct gdb_reg_value *value) | |
66 | { | |
67 | free (value); | |
68 | } | |
69 | ||
70 | static void | |
71 | write_register (struct gdb_unwind_callbacks *callbacks, int dw_reg, | |
72 | uintptr_t value) | |
73 | { | |
74 | const int size = sizeof (uintptr_t); | |
75 | struct gdb_reg_value *reg_val = | |
76 | malloc (sizeof (struct gdb_reg_value) + size - 1); | |
77 | reg_val->defined = 1; | |
78 | reg_val->free = free_reg_value; | |
79 | ||
80 | memcpy (reg_val->value, &value, size); | |
81 | callbacks->reg_set (callbacks, dw_reg, reg_val); | |
82 | } | |
83 | ||
84 | static int | |
85 | read_register (struct gdb_unwind_callbacks *callbacks, int dw_reg, | |
86 | uintptr_t *value) | |
87 | { | |
88 | const int size = sizeof (uintptr_t); | |
89 | struct gdb_reg_value *reg_val = callbacks->reg_get (callbacks, dw_reg); | |
90 | if (reg_val->size != size || !reg_val->defined) | |
91 | { | |
92 | reg_val->free (reg_val); | |
93 | return 0; | |
94 | } | |
95 | memcpy (value, reg_val->value, size); | |
96 | reg_val->free (reg_val); | |
97 | return 1; | |
98 | } | |
99 | ||
20aa2c60 PA |
100 | /* Read the stack pointer into *VALUE. IP is the address the inferior |
101 | is currently stopped at. Takes care of demangling the stack | |
102 | pointer if necessary. */ | |
103 | ||
104 | static int | |
105 | read_sp (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs, | |
106 | uintptr_t ip, uintptr_t *value) | |
107 | { | |
108 | struct reader_state *state = (struct reader_state *) self->priv_data; | |
109 | uintptr_t sp; | |
110 | ||
111 | if (!read_register (cbs, AMD64_RSP, &sp)) | |
112 | return GDB_FAIL; | |
113 | ||
114 | /* If stopped at the instruction after the "xor $-1, %rsp", demangle | |
115 | the stack pointer back. */ | |
116 | if (ip == state->code_begin + 5) | |
117 | sp ^= (uintptr_t) -1; | |
118 | ||
119 | *value = sp; | |
120 | return GDB_SUCCESS; | |
121 | } | |
122 | ||
2838cc1d SD |
123 | static enum gdb_status |
124 | unwind_frame (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs) | |
125 | { | |
126 | const int word_size = sizeof (uintptr_t); | |
20aa2c60 PA |
127 | uintptr_t prev_sp, this_sp; |
128 | uintptr_t prev_ip, this_ip; | |
129 | uintptr_t prev_bp, this_bp; | |
2838cc1d SD |
130 | struct reader_state *state = (struct reader_state *) self->priv_data; |
131 | ||
132 | if (!read_register (cbs, AMD64_RA, &this_ip)) | |
133 | return GDB_FAIL; | |
134 | ||
135 | if (this_ip >= state->code_end || this_ip < state->code_begin) | |
136 | return GDB_FAIL; | |
137 | ||
20aa2c60 PA |
138 | /* Unwind RBP in order to make the unwinder that tries to unwind |
139 | from the just-unwound frame happy. */ | |
140 | if (!read_register (cbs, AMD64_RBP, &this_bp)) | |
2838cc1d | 141 | return GDB_FAIL; |
20aa2c60 PA |
142 | /* RBP is unmodified. */ |
143 | prev_bp = this_bp; | |
2838cc1d | 144 | |
20aa2c60 PA |
145 | /* Fetch the demangled stack pointer. */ |
146 | if (!read_sp (self, cbs, this_ip, &this_sp)) | |
2838cc1d SD |
147 | return GDB_FAIL; |
148 | ||
20aa2c60 PA |
149 | /* The return address is saved on the stack. */ |
150 | if (cbs->target_read (this_sp, &prev_ip, word_size) == GDB_FAIL) | |
151 | return GDB_FAIL; | |
2838cc1d | 152 | prev_sp = this_sp + word_size; |
20aa2c60 | 153 | |
2838cc1d SD |
154 | write_register (cbs, AMD64_RA, prev_ip); |
155 | write_register (cbs, AMD64_RSP, prev_sp); | |
20aa2c60 | 156 | write_register (cbs, AMD64_RBP, prev_bp); |
2838cc1d SD |
157 | return GDB_SUCCESS; |
158 | } | |
159 | ||
160 | static struct gdb_frame_id | |
161 | get_frame_id (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs) | |
162 | { | |
163 | struct reader_state *state = (struct reader_state *) self->priv_data; | |
164 | struct gdb_frame_id frame_id; | |
20aa2c60 | 165 | uintptr_t ip; |
2838cc1d | 166 | uintptr_t sp; |
20aa2c60 PA |
167 | |
168 | read_register (cbs, AMD64_RA, &ip); | |
169 | read_sp (self, cbs, ip, &sp); | |
2838cc1d SD |
170 | |
171 | frame_id.code_address = (GDB_CORE_ADDR) state->code_begin; | |
172 | frame_id.stack_address = (GDB_CORE_ADDR) sp; | |
173 | ||
174 | return frame_id; | |
175 | } | |
176 | ||
177 | static void | |
178 | destroy_reader (struct gdb_reader_funcs *self) | |
179 | { | |
180 | free (self->priv_data); | |
181 | free (self); | |
182 | } | |
183 | ||
184 | struct gdb_reader_funcs * | |
185 | gdb_init_reader (void) | |
186 | { | |
20aa2c60 | 187 | struct reader_state *state = calloc (1, sizeof (struct reader_state)); |
2838cc1d SD |
188 | struct gdb_reader_funcs *reader_funcs = |
189 | malloc (sizeof (struct gdb_reader_funcs)); | |
190 | ||
191 | reader_funcs->reader_version = GDB_READER_INTERFACE_VERSION; | |
192 | reader_funcs->priv_data = state; | |
193 | reader_funcs->read = read_debug_info; | |
194 | reader_funcs->unwind = unwind_frame; | |
195 | reader_funcs->get_frame_id = get_frame_id; | |
196 | reader_funcs->destroy = destroy_reader; | |
197 | ||
198 | return reader_funcs; | |
199 | } |