1 /* Emulation of eBPF helpers.
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 This file is part of GDB, the GNU debugger.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* BPF programs rely on the existence of several helper functions,
20 which are provided by the kernel. This simulator provides an
21 implementation of the helpers, which can be customized by the
24 /* This must come before any other includes. */
27 #define WANT_CPU_BPFBF
28 #define WANT_CPU bpfbf
35 #include "bpf-helpers.h"
37 /* bpf_trace_printk is a printk-like facility for debugging.
39 In the kernel, it appends a line to the Linux's tracing debugging
42 In this simulator, it uses the simulator's tracing interface
45 The format tags recognized by this helper are:
46 %d, %i, %u, %x, %ld, %li, %lu, %lx, %lld, %lli, %llu, %llx,
49 A maximum of three tags are supported.
51 This helper returns the number of bytes written, or a negative
52 value in case of failure. */
55 bpf_trace_printk (SIM_CPU
*current_cpu
)
58 SIM_DESC sd
= CPU_STATE (current_cpu
);
61 uint32_t size
, tags_processed
;
62 size_t i
, bytes_written
= 0;
64 /* The first argument is the format string, which is passed as a
66 fmt_address
= GET_H_GPR (1);
68 /* The second argument is the length of the format string, as an
69 unsigned 32-bit number in %r2. */
72 /* Read the format string from the memory pointed by %r2, printing
73 out the stuff as we go. There is a maximum of three format tags
74 supported, which are read from %r3, %r4 and %r5 respectively. */
75 for (i
= 0, tags_processed
= 0; i
< size
;)
78 QI c
= GETMEMUQI (current_cpu
, CPU_PC_GET (current_cpu
),
84 /* Check we are not exceeding the limit of three format
86 if (tags_processed
> 2)
87 return -1; /* XXX look for kernel error code. */
89 /* Depending on the kind of tag, extract the value from the
92 return -1; /* XXX look for kernel error code. */
94 value
= GET_H_GPR (3 + tags_processed
);
96 switch ((GETMEMUQI (current_cpu
, CPU_PC_GET (current_cpu
),
100 trace_printf (sd
, current_cpu
, "%d", (int) value
);
103 trace_printf (sd
, current_cpu
, "%i", (int) value
);
106 trace_printf (sd
, current_cpu
, "%u", (unsigned int) value
);
109 trace_printf (sd
, current_cpu
, "%x", (unsigned int) value
);
115 switch (GETMEMUQI (current_cpu
, CPU_PC_GET (current_cpu
),
119 trace_printf (sd
, current_cpu
, "%ld", (long) value
);
122 trace_printf (sd
, current_cpu
, "%li", (long) value
);
125 trace_printf (sd
, current_cpu
, "%lu", (unsigned long) value
);
128 trace_printf (sd
, current_cpu
, "%lx", (unsigned long) value
);
134 switch (GETMEMUQI (current_cpu
, CPU_PC_GET (current_cpu
),
137 trace_printf (sd
, current_cpu
, "%lld", (long long) value
);
140 trace_printf (sd
, current_cpu
, "%lli", (long long) value
);
143 trace_printf (sd
, current_cpu
, "%llu", (unsigned long long) value
);
146 trace_printf (sd
, current_cpu
, "%llx", (unsigned long long) value
);
173 trace_printf (sd
, current_cpu
, "%c", c
);
180 return bytes_written
;