1 /* Copyright (C) 2009-2020 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
23 #include JIT_READER_H /* Please see jit-reader.exp for an explanation. */
26 GDB_DECLARE_GPL_COMPATIBLE_READER
;
43 static enum gdb_status
44 read_debug_info (struct gdb_reader_funcs
*self
,
45 struct gdb_symbol_callbacks
*cbs
,
46 void *memory
, long memory_sz
)
48 struct jithost_abi
*symfile
= memory
;
49 struct gdb_object
*object
= cbs
->object_open (cbs
);
50 struct gdb_symtab
*symtab
= cbs
->symtab_open (cbs
, object
, "");
52 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
54 /* Record the stack mangle function's range, for the unwinder. */
55 state
->func_stack_mangle
.begin
56 = (uintptr_t) symfile
->function_stack_mangle
.begin
;
57 state
->func_stack_mangle
.end
58 = (uintptr_t) symfile
->function_stack_mangle
.end
;
60 cbs
->block_open (cbs
, symtab
, NULL
,
61 (GDB_CORE_ADDR
) symfile
->function_stack_mangle
.begin
,
62 (GDB_CORE_ADDR
) symfile
->function_stack_mangle
.end
,
63 "jit_function_stack_mangle");
65 cbs
->block_open (cbs
, symtab
, NULL
,
66 (GDB_CORE_ADDR
) symfile
->function_add
.begin
,
67 (GDB_CORE_ADDR
) symfile
->function_add
.end
,
70 cbs
->symtab_close (cbs
, symtab
);
71 cbs
->object_close (cbs
, object
);
76 free_reg_value (struct gdb_reg_value
*value
)
82 write_register (struct gdb_unwind_callbacks
*callbacks
, int dw_reg
,
85 const int size
= sizeof (uintptr_t);
86 struct gdb_reg_value
*reg_val
=
87 malloc (sizeof (struct gdb_reg_value
) + size
- 1);
89 reg_val
->free
= free_reg_value
;
91 memcpy (reg_val
->value
, &value
, size
);
92 callbacks
->reg_set (callbacks
, dw_reg
, reg_val
);
96 read_register (struct gdb_unwind_callbacks
*callbacks
, int dw_reg
,
99 const int size
= sizeof (uintptr_t);
100 struct gdb_reg_value
*reg_val
= callbacks
->reg_get (callbacks
, dw_reg
);
101 if (reg_val
->size
!= size
|| !reg_val
->defined
)
103 reg_val
->free (reg_val
);
106 memcpy (value
, reg_val
->value
, size
);
107 reg_val
->free (reg_val
);
111 /* Read the stack pointer into *VALUE. IP is the address the inferior
112 is currently stopped at. Takes care of demangling the stack
113 pointer if necessary. */
116 read_sp (struct gdb_reader_funcs
*self
, struct gdb_unwind_callbacks
*cbs
,
117 uintptr_t ip
, uintptr_t *value
)
119 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
122 if (!read_register (cbs
, AMD64_RSP
, &sp
))
125 /* If stopped at the instruction after the "xor $-1, %rsp", demangle
126 the stack pointer back. */
127 if (ip
== state
->func_stack_mangle
.begin
+ 5)
128 sp
^= (uintptr_t) -1;
134 static enum gdb_status
135 unwind_frame (struct gdb_reader_funcs
*self
, struct gdb_unwind_callbacks
*cbs
)
137 const int word_size
= sizeof (uintptr_t);
138 uintptr_t prev_sp
, this_sp
;
139 uintptr_t prev_ip
, this_ip
;
140 uintptr_t prev_bp
, this_bp
;
141 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
143 if (!read_register (cbs
, AMD64_RA
, &this_ip
))
146 if (this_ip
>= state
->func_stack_mangle
.end
147 || this_ip
< state
->func_stack_mangle
.begin
)
150 /* Unwind RBP in order to make the unwinder that tries to unwind
151 from the just-unwound frame happy. */
152 if (!read_register (cbs
, AMD64_RBP
, &this_bp
))
154 /* RBP is unmodified. */
157 /* Fetch the demangled stack pointer. */
158 if (!read_sp (self
, cbs
, this_ip
, &this_sp
))
161 /* The return address is saved on the stack. */
162 if (cbs
->target_read (this_sp
, &prev_ip
, word_size
) == GDB_FAIL
)
164 prev_sp
= this_sp
+ word_size
;
166 write_register (cbs
, AMD64_RA
, prev_ip
);
167 write_register (cbs
, AMD64_RSP
, prev_sp
);
168 write_register (cbs
, AMD64_RBP
, prev_bp
);
172 static struct gdb_frame_id
173 get_frame_id (struct gdb_reader_funcs
*self
, struct gdb_unwind_callbacks
*cbs
)
175 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
176 struct gdb_frame_id frame_id
;
180 read_register (cbs
, AMD64_RA
, &ip
);
181 read_sp (self
, cbs
, ip
, &sp
);
183 frame_id
.code_address
= (GDB_CORE_ADDR
) state
->func_stack_mangle
.begin
;
184 frame_id
.stack_address
= (GDB_CORE_ADDR
) sp
;
190 destroy_reader (struct gdb_reader_funcs
*self
)
192 free (self
->priv_data
);
196 struct gdb_reader_funcs
*
197 gdb_init_reader (void)
199 struct reader_state
*state
= calloc (1, sizeof (struct reader_state
));
200 struct gdb_reader_funcs
*reader_funcs
=
201 malloc (sizeof (struct gdb_reader_funcs
));
203 reader_funcs
->reader_version
= GDB_READER_INTERFACE_VERSION
;
204 reader_funcs
->priv_data
= state
;
205 reader_funcs
->read
= read_debug_info
;
206 reader_funcs
->unwind
= unwind_frame
;
207 reader_funcs
->get_frame_id
= get_frame_id
;
208 reader_funcs
->destroy
= destroy_reader
;