1 /* Copyright (C) 2009-2019 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
;
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
)
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
;
51 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
53 /* Record the function's range, for the unwinder. */
54 state
->code_begin
= begin
;
55 state
->code_end
= end
;
57 cbs
->block_open (cbs
, symtab
, NULL
, begin
, end
, "jit_function_00");
59 cbs
->symtab_close (cbs
, symtab
);
60 cbs
->object_close (cbs
, object
);
65 free_reg_value (struct gdb_reg_value
*value
)
71 write_register (struct gdb_unwind_callbacks
*callbacks
, int dw_reg
,
74 const int size
= sizeof (uintptr_t);
75 struct gdb_reg_value
*reg_val
=
76 malloc (sizeof (struct gdb_reg_value
) + size
- 1);
78 reg_val
->free
= free_reg_value
;
80 memcpy (reg_val
->value
, &value
, size
);
81 callbacks
->reg_set (callbacks
, dw_reg
, reg_val
);
85 read_register (struct gdb_unwind_callbacks
*callbacks
, int dw_reg
,
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
)
92 reg_val
->free (reg_val
);
95 memcpy (value
, reg_val
->value
, size
);
96 reg_val
->free (reg_val
);
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. */
105 read_sp (struct gdb_reader_funcs
*self
, struct gdb_unwind_callbacks
*cbs
,
106 uintptr_t ip
, uintptr_t *value
)
108 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
111 if (!read_register (cbs
, AMD64_RSP
, &sp
))
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;
123 static enum gdb_status
124 unwind_frame (struct gdb_reader_funcs
*self
, struct gdb_unwind_callbacks
*cbs
)
126 const int word_size
= sizeof (uintptr_t);
127 uintptr_t prev_sp
, this_sp
;
128 uintptr_t prev_ip
, this_ip
;
129 uintptr_t prev_bp
, this_bp
;
130 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
132 if (!read_register (cbs
, AMD64_RA
, &this_ip
))
135 if (this_ip
>= state
->code_end
|| this_ip
< state
->code_begin
)
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
))
142 /* RBP is unmodified. */
145 /* Fetch the demangled stack pointer. */
146 if (!read_sp (self
, cbs
, this_ip
, &this_sp
))
149 /* The return address is saved on the stack. */
150 if (cbs
->target_read (this_sp
, &prev_ip
, word_size
) == GDB_FAIL
)
152 prev_sp
= this_sp
+ word_size
;
154 write_register (cbs
, AMD64_RA
, prev_ip
);
155 write_register (cbs
, AMD64_RSP
, prev_sp
);
156 write_register (cbs
, AMD64_RBP
, prev_bp
);
160 static struct gdb_frame_id
161 get_frame_id (struct gdb_reader_funcs
*self
, struct gdb_unwind_callbacks
*cbs
)
163 struct reader_state
*state
= (struct reader_state
*) self
->priv_data
;
164 struct gdb_frame_id frame_id
;
168 read_register (cbs
, AMD64_RA
, &ip
);
169 read_sp (self
, cbs
, ip
, &sp
);
171 frame_id
.code_address
= (GDB_CORE_ADDR
) state
->code_begin
;
172 frame_id
.stack_address
= (GDB_CORE_ADDR
) sp
;
178 destroy_reader (struct gdb_reader_funcs
*self
)
180 free (self
->priv_data
);
184 struct gdb_reader_funcs
*
185 gdb_init_reader (void)
187 struct reader_state
*state
= calloc (1, sizeof (struct reader_state
));
188 struct gdb_reader_funcs
*reader_funcs
=
189 malloc (sizeof (struct gdb_reader_funcs
));
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
;