GDB (xrefs)
Loading...
Searching...
No Matches
bpf-tdep.c
Go to the documentation of this file.
1/* Target-dependent code for BPF.
2
3 Copyright (C) 2020-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "arch-utils.h"
22#include "dis-asm.h"
23#include "frame.h"
24#include "frame-unwind.h"
25#include "trad-frame.h"
26#include "symtab.h"
27#include "value.h"
28#include "gdbcmd.h"
29#include "breakpoint.h"
30#include "inferior.h"
31#include "regcache.h"
32#include "target.h"
33#include "dwarf2/frame.h"
34#include "osabi.h"
35#include "target-descriptions.h"
36#include "remote.h"
37#include "gdbarch.h"
38
39
40/* eBPF registers. */
41
57
58#define BPF_NUM_REGS (BPF_PC_REGNUM + 1)
59
60/* Target-dependent structure in gdbarch. */
64
65
66/* Internal debugging facilities. */
67
68/* When this is set to non-zero debugging information will be
69 printed. */
70
71static unsigned int bpf_debug_flag = 0;
72
73/* The show callback for 'show debug bpf'. */
74
75static void
76show_bpf_debug (struct ui_file *file, int from_tty,
77 struct cmd_list_element *c, const char *value)
78{
79 gdb_printf (file, _("Debugging of BPF is %s.\n"), value);
80}
81
82
83/* BPF registers. */
84
85static const char *bpf_register_names[] =
86{
87 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
88 "r8", "r9", "r10", "pc"
89};
90
91/* Return the name of register REGNUM. */
92
93static const char *
95{
97 return bpf_register_names[reg];
98}
99
100/* Return the GDB type of register REGNUM. */
101
102static struct type *
104{
105 if (reg == BPF_R10_REGNUM)
107 else if (reg == BPF_PC_REGNUM)
110}
111
112/* Return the GDB register number corresponding to DWARF's REG. */
113
114static int
116{
117 if (reg >= 0 && reg < BPF_NUM_REGS)
118 return reg;
119 return -1;
120}
121
122/* Implement the "print_insn" gdbarch method. */
123
124static int
125bpf_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
126{
127 info->symbols = NULL;
128 return default_print_insn (memaddr, info);
129}
130
131
132/* Return PC of first real instruction of the function starting at
133 START_PC. */
134
135static CORE_ADDR
136bpf_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
137{
139 "Skipping prologue: start_pc=%s\n",
140 paddress (gdbarch, start_pc));
141 /* XXX: to be completed. */
142 return start_pc + 0;
143}
144
145
146/* Frame unwinder.
147
148 XXX it is not clear how to unwind in eBPF, since the stack is not
149 guaranteed to be contiguous, and therefore no relative stack
150 addressing can be done in the callee in order to access the
151 caller's stack frame. To explore with xBPF, which will relax this
152 restriction. */
153
154/* Given THIS_FRAME, return its ID. */
155
156static void
158 void **this_prologue_cache,
159 struct frame_id *this_id)
160{
161 /* Note that THIS_ID defaults to the outermost frame if we don't set
162 anything here. See frame.c:compute_frame_id. */
163}
164
165/* Return the reason why we can't unwind past THIS_FRAME. */
166
167static enum unwind_stop_reason
169 void **this_cache)
170{
171 return UNWIND_OUTERMOST;
172}
173
174/* Ask THIS_FRAME to unwind its register. */
175
176static struct value *
178 void **this_prologue_cache, int regnum)
179{
180 return frame_unwind_got_register (this_frame, regnum, regnum);
181}
182
183/* Frame unwinder machinery for BPF. */
184
185static const struct frame_unwind bpf_frame_unwind =
186{
187 "bpf prologue",
192 NULL,
194};
195
196
197/* Breakpoints. */
198
199/* Enum describing the different kinds of breakpoints. We currently
200 just support one, implemented by the brkpt xbpf instruction. */
201
206
207/* Implement the breakpoint_kind_from_pc gdbarch method. */
208
209static int
210bpf_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *start_pc)
211{
212 /* We support just one kind of breakpoint. */
213 return BPF_BP_KIND_BRKPT;
214}
215
216/* Implement the sw_breakpoint_from_kind gdbarch method. */
217
218static const gdb_byte *
220{
221 static unsigned char brkpt_insn[]
222 = {0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
223
224 switch (kind)
225 {
227 *size = 8;
228 return brkpt_insn;
229 default:
230 gdb_assert_not_reached ("unexpected BPF breakpoint kind");
231 }
232}
233
234
235/* Assuming THIS_FRAME is a dummy frame, return its frame ID. */
236
237static struct frame_id
239{
240 CORE_ADDR sp = get_frame_register_unsigned (this_frame,
242 return frame_id_build (sp, get_frame_pc (this_frame));
243}
244
245/* Implement the push dummy call gdbarch callback. */
246
247static CORE_ADDR
248bpf_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
249 struct regcache *regcache, CORE_ADDR bp_addr,
250 int nargs, struct value **args, CORE_ADDR sp,
251 function_call_return_method return_method,
252 CORE_ADDR struct_addr)
253{
254 gdb_printf (gdb_stdlog, "Pushing dummy call: sp=%s\n",
255 paddress (gdbarch, sp));
256 /* XXX writeme */
257 return sp;
258}
259
260/* Extract a function return value of TYPE from REGCACHE,
261 and copy it into VALBUF. */
262
263static void
265 gdb_byte *valbuf)
266{
267 int len = type->length ();
268 gdb_byte vbuf[8];
269
270 gdb_assert (len <= 8);
272 memcpy (valbuf, vbuf + 8 - len, len);
273}
274
275/* Store the function return value of type TYPE from VALBUF into REGNAME. */
276
277static void
279 const gdb_byte *valbuf)
280{
281 int len = type->length ();
282 gdb_byte vbuf[8];
283
284 gdb_assert (len <= 8);
285 memset (vbuf, 0, sizeof (vbuf));
286 memcpy (vbuf + 8 - len, valbuf, len);
288}
289
290/* Handle function's return value. */
291
292static enum return_value_convention
293bpf_return_value (struct gdbarch *gdbarch, struct value *function,
294 struct type *type, struct regcache *regcache,
295 gdb_byte *readbuf, const gdb_byte *writebuf)
296{
297 int len = type->length ();
298
299 if (len > 8)
301
302 if (readbuf != NULL)
304 if (writebuf != NULL)
306
308}
309
310
311/* Initialize the current architecture based on INFO. If possible, re-use an
312 architecture from ARCHES, which is a list of architectures already created
313 during this debugging session. */
314
315static struct gdbarch *
317{
318 /* If there is already a candidate, use it. */
320 if (arches != NULL)
321 return arches->gdbarch;
322
323 /* Allocate space for the new architecture. */
326
327 /* Information about registers, etc. */
331
332 /* Register numbers of various important registers. */
335
336 /* Map DWARF2 registers to GDB registers. */
338
339 /* Call dummy code. */
343
344 /* Returning results. */
346
347 /* Advance PC across function entry code. */
349
350 /* Stack grows downward. */
352
353 /* Breakpoint manipulation. */
356
357 /* Frame handling. */
359
360 /* Disassembly. */
362
363 /* Hook in ABI-specific overrides, if they have been registered. */
365
366 /* Install unwinders. */
368
369 return gdbarch;
370}
371
373void
375{
376 gdbarch_register (bfd_arch_bpf, bpf_gdbarch_init);
377
378 /* Add commands 'set/show debug bpf'. */
381 _("Set BPF debugging."),
382 _("Show BPF debugging."),
383 _("Enables BPF specific debugging output."),
384 NULL,
387}
int regnum
gdb_static_assert(sizeof(splay_tree_key) >=sizeof(CORE_ADDR *))
void gdbarch_register(enum bfd_architecture bfd_architecture, gdbarch_init_ftype *init, gdbarch_dump_tdep_ftype *dump_tdep, gdbarch_supports_arch_info_ftype *supports_arch_info)
int default_print_insn(bfd_vma memaddr, disassemble_info *info)
static std::vector< const char * > arches
Definition arch-utils.c:685
int core_addr_lessthan(CORE_ADDR lhs, CORE_ADDR rhs)
Definition arch-utils.c:177
struct gdbarch_list * gdbarch_list_lookup_by_info(struct gdbarch_list *arches, const struct gdbarch_info *info)
static enum return_value_convention bpf_return_value(struct gdbarch *gdbarch, struct value *function, struct type *type, struct regcache *regcache, gdb_byte *readbuf, const gdb_byte *writebuf)
Definition bpf-tdep.c:293
bpf_breakpoint_kinds
Definition bpf-tdep.c:203
@ BPF_BP_KIND_BRKPT
Definition bpf-tdep.c:204
static struct frame_id bpf_dummy_id(struct gdbarch *gdbarch, frame_info_ptr this_frame)
Definition bpf-tdep.c:238
#define BPF_NUM_REGS
Definition bpf-tdep.c:58
static void show_bpf_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition bpf-tdep.c:76
static enum unwind_stop_reason bpf_frame_unwind_stop_reason(frame_info_ptr this_frame, void **this_cache)
Definition bpf-tdep.c:168
static int bpf_breakpoint_kind_from_pc(struct gdbarch *gdbarch, CORE_ADDR *start_pc)
Definition bpf-tdep.c:210
void _initialize_bpf_tdep()
Definition bpf-tdep.c:374
bpf_regnum
Definition bpf-tdep.c:43
@ BPF_R4_REGNUM
Definition bpf-tdep.c:48
@ BPF_R10_REGNUM
Definition bpf-tdep.c:54
@ BPF_R3_REGNUM
Definition bpf-tdep.c:47
@ BPF_R5_REGNUM
Definition bpf-tdep.c:49
@ BPF_R8_REGNUM
Definition bpf-tdep.c:52
@ BPF_R6_REGNUM
Definition bpf-tdep.c:50
@ BPF_R0_REGNUM
Definition bpf-tdep.c:44
@ BPF_R7_REGNUM
Definition bpf-tdep.c:51
@ BPF_R1_REGNUM
Definition bpf-tdep.c:45
@ BPF_PC_REGNUM
Definition bpf-tdep.c:55
@ BPF_R9_REGNUM
Definition bpf-tdep.c:53
@ BPF_R2_REGNUM
Definition bpf-tdep.c:46
static void bpf_frame_this_id(frame_info_ptr this_frame, void **this_prologue_cache, struct frame_id *this_id)
Definition bpf-tdep.c:157
static CORE_ADDR bpf_skip_prologue(struct gdbarch *gdbarch, CORE_ADDR start_pc)
Definition bpf-tdep.c:136
static int bpf_dwarf2_reg_to_regnum(struct gdbarch *gdbarch, int reg)
Definition bpf-tdep.c:115
static const gdb_byte * bpf_sw_breakpoint_from_kind(struct gdbarch *gdbarch, int kind, int *size)
Definition bpf-tdep.c:219
static unsigned int bpf_debug_flag
Definition bpf-tdep.c:71
static const char * bpf_register_name(struct gdbarch *gdbarch, int reg)
Definition bpf-tdep.c:94
static void bpf_extract_return_value(struct type *type, struct regcache *regcache, gdb_byte *valbuf)
Definition bpf-tdep.c:264
static CORE_ADDR bpf_push_dummy_call(struct gdbarch *gdbarch, struct value *function, struct regcache *regcache, CORE_ADDR bp_addr, int nargs, struct value **args, CORE_ADDR sp, function_call_return_method return_method, CORE_ADDR struct_addr)
Definition bpf-tdep.c:248
static struct gdbarch * bpf_gdbarch_init(struct gdbarch_info info, struct gdbarch_list *arches)
Definition bpf-tdep.c:316
static struct type * bpf_register_type(struct gdbarch *gdbarch, int reg)
Definition bpf-tdep.c:103
static int bpf_gdb_print_insn(bfd_vma memaddr, disassemble_info *info)
Definition bpf-tdep.c:125
static const struct frame_unwind bpf_frame_unwind
Definition bpf-tdep.c:185
static void bpf_store_return_value(struct type *type, struct regcache *regcache, const gdb_byte *valbuf)
Definition bpf-tdep.c:278
static struct value * bpf_frame_prev_register(frame_info_ptr this_frame, void **this_prologue_cache, int regnum)
Definition bpf-tdep.c:177
static const char * bpf_register_names[]
Definition bpf-tdep.c:85
enum register_status cooked_read(int regnum, gdb_byte *buf)
Definition regcache.c:698
void cooked_write(int regnum, const gdb_byte *buf)
Definition regcache.c:870
struct cmd_list_element * showdebuglist
Definition cli-cmds.c:167
struct cmd_list_element * setdebuglist
Definition cli-cmds.c:165
set_show_commands add_setshow_zuinteger_cmd(const char *name, enum command_class theclass, unsigned int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
@ class_maintenance
Definition command.h:65
return_value_convention
Definition defs.h:257
@ RETURN_VALUE_REGISTER_CONVENTION
Definition defs.h:260
@ RETURN_VALUE_STRUCT_CONVENTION
Definition defs.h:267
int default_frame_sniffer(const struct frame_unwind *self, frame_info_ptr this_frame, void **this_prologue_cache)
struct value * frame_unwind_got_register(frame_info_ptr frame, int regnum, int new_regnum)
void frame_unwind_append_unwinder(struct gdbarch *gdbarch, const struct frame_unwind *unwinder)
ULONGEST get_frame_register_unsigned(frame_info_ptr frame, int regnum)
Definition frame.c:1399
CORE_ADDR get_frame_pc(frame_info_ptr frame)
Definition frame.c:2712
struct frame_id frame_id_build(CORE_ADDR stack_addr, CORE_ADDR code_addr)
Definition frame.c:736
@ NORMAL_FRAME
Definition frame.h:187
unwind_stop_reason
Definition frame.h:653
void set_gdbarch_breakpoint_kind_from_pc(struct gdbarch *gdbarch, gdbarch_breakpoint_kind_from_pc_ftype *breakpoint_kind_from_pc)
void set_gdbarch_skip_prologue(struct gdbarch *gdbarch, gdbarch_skip_prologue_ftype *skip_prologue)
void set_gdbarch_register_name(struct gdbarch *gdbarch, gdbarch_register_name_ftype *register_name)
void set_gdbarch_return_value(struct gdbarch *gdbarch, gdbarch_return_value_ftype *return_value)
void set_gdbarch_inner_than(struct gdbarch *gdbarch, gdbarch_inner_than_ftype *inner_than)
void set_gdbarch_sp_regnum(struct gdbarch *gdbarch, int sp_regnum)
Definition gdbarch.c:2047
int gdbarch_sp_regnum(struct gdbarch *gdbarch)
Definition gdbarch.c:2037
void set_gdbarch_pc_regnum(struct gdbarch *gdbarch, int pc_regnum)
Definition gdbarch.c:2064
void set_gdbarch_call_dummy_location(struct gdbarch *gdbarch, enum call_dummy_location_type call_dummy_location)
Definition gdbarch.c:2279
void set_gdbarch_register_type(struct gdbarch *gdbarch, gdbarch_register_type_ftype *register_type)
void set_gdbarch_print_insn(struct gdbarch *gdbarch, gdbarch_print_insn_ftype *print_insn)
void set_gdbarch_dwarf2_reg_to_regnum(struct gdbarch *gdbarch, gdbarch_dwarf2_reg_to_regnum_ftype *dwarf2_reg_to_regnum)
void set_gdbarch_num_regs(struct gdbarch *gdbarch, int num_regs)
Definition gdbarch.c:1941
void set_gdbarch_sw_breakpoint_from_kind(struct gdbarch *gdbarch, gdbarch_sw_breakpoint_from_kind_ftype *sw_breakpoint_from_kind)
void set_gdbarch_dummy_id(struct gdbarch *gdbarch, gdbarch_dummy_id_ftype *dummy_id)
void set_gdbarch_push_dummy_call(struct gdbarch *gdbarch, gdbarch_push_dummy_call_ftype *push_dummy_call)
void set_gdbarch_frame_args_skip(struct gdbarch *gdbarch, CORE_ADDR frame_args_skip)
Definition gdbarch.c:3012
struct gdbarch * gdbarch_alloc(const struct gdbarch_info *info, gdbarch_tdep_up tdep)
Definition gdbarch.c:266
std::unique_ptr< gdbarch_tdep_base > gdbarch_tdep_up
Definition gdbarch.h:73
@ ON_STACK
Definition gdbarch.h:157
function_call_return_method
Definition gdbarch.h:114
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
size_t size
Definition go32-nat.c:239
info(Component c)
Definition gdbarch.py:41
void gdbarch_init_osabi(struct gdbarch_info info, struct gdbarch *gdbarch)
Definition osabi.c:382
struct type * builtin_func_ptr
Definition gdbtypes.h:2146
struct type * builtin_data_ptr
Definition gdbtypes.h:2135
struct type * builtin_int64
Definition gdbtypes.h:2121
ULONGEST length() const
Definition gdbtypes.h:983
Definition value.h:130
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition utils.c:3166
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stdlog
Definition utils.h:190