xref: /openbmc/qemu/tests/tcg/plugins/syscall.c (revision 7d87775f)
1 /*
2  * Copyright (C) 2020, Matthias Weckbecker <matthias@weckbecker.name>
3  *
4  * License: GNU GPL, version 2 or later.
5  *   See the COPYING file in the top-level directory.
6  */
7 #include <inttypes.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <glib.h>
14 
15 #include <qemu-plugin.h>
16 
17 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
18 
19 typedef struct {
20     int64_t num;
21     int64_t calls;
22     int64_t errors;
23 } SyscallStats;
24 
25 struct SyscallInfo {
26     const char *name;
27     int64_t write_sysno;
28 };
29 
30 static const struct SyscallInfo arch_syscall_info[] = {
31     { "aarch64", 64 },
32     { "aarch64_be", 64 },
33     { "alpha", 4 },
34     { "arm", 4 },
35     { "armeb", 4 },
36     { "avr", -1 },
37     { "hexagon", 64 },
38     { "hppa", -1 },
39     { "i386", 4 },
40     { "loongarch64", -1 },
41     { "m68k", 4 },
42     { "microblaze", 4 },
43     { "microblazeel", 4 },
44     { "mips", 1 },
45     { "mips64", 1 },
46     { "mips64el", 1 },
47     { "mipsel", 1 },
48     { "mipsn32", 1 },
49     { "mipsn32el", 1 },
50     { "or1k", -1 },
51     { "ppc", 4 },
52     { "ppc64", 4 },
53     { "ppc64le", 4 },
54     { "riscv32", 64 },
55     { "riscv64", 64 },
56     { "rx", -1 },
57     { "s390x", -1 },
58     { "sh4", -1 },
59     { "sh4eb", -1 },
60     { "sparc", 4 },
61     { "sparc32plus", 4 },
62     { "sparc64", 4 },
63     { "tricore", -1 },
64     { "x86_64", 1 },
65     { "xtensa", 13 },
66     { "xtensaeb", 13 },
67     { NULL, -1 },
68 };
69 
70 static GMutex lock;
71 static GHashTable *statistics;
72 static GByteArray *memory_buffer;
73 static bool do_log_writes;
74 static int64_t write_sysno = -1;
75 
76 static SyscallStats *get_or_create_entry(int64_t num)
77 {
78     SyscallStats *entry =
79         (SyscallStats *) g_hash_table_lookup(statistics, GINT_TO_POINTER(num));
80 
81     if (!entry) {
82         entry = g_new0(SyscallStats, 1);
83         entry->num = num;
84         g_hash_table_insert(statistics, GINT_TO_POINTER(num), (gpointer) entry);
85     }
86 
87     return entry;
88 }
89 
90 /*
91  * Hex-dump a GByteArray to the QEMU plugin output in the format:
92  * 61 63 63 65 6c 09 09 20 20 20 66 70 75 09 09 09  | accel.....fpu...
93  * 20 6d 6f 64 75 6c 65 2d 63 6f 6d 6d 6f 6e 2e 63  | .module-common.c
94  */
95 static void hexdump(const GByteArray *data)
96 {
97     g_autoptr(GString) out = g_string_new("");
98 
99     for (guint index = 0; index < data->len; index += 16) {
100         for (guint col = 0; col < 16; col++) {
101             if (index + col < data->len) {
102                 g_string_append_printf(out, "%02x ", data->data[index + col]);
103             } else {
104                 g_string_append(out, "   ");
105             }
106         }
107 
108         g_string_append(out, " | ");
109 
110         for (guint col = 0; col < 16; col++) {
111             if (index + col >= data->len) {
112                 break;
113             }
114 
115             if (g_ascii_isgraph(data->data[index + col])) {
116                 g_string_append_printf(out, "%c", data->data[index + col]);
117             } else {
118                 g_string_append(out, ".");
119             }
120         }
121 
122         g_string_append(out, "\n");
123     }
124 
125     qemu_plugin_outs(out->str);
126 }
127 
128 static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
129                          int64_t num, uint64_t a1, uint64_t a2,
130                          uint64_t a3, uint64_t a4, uint64_t a5,
131                          uint64_t a6, uint64_t a7, uint64_t a8)
132 {
133     if (statistics) {
134         SyscallStats *entry;
135         g_mutex_lock(&lock);
136         entry = get_or_create_entry(num);
137         entry->calls++;
138         g_mutex_unlock(&lock);
139     } else {
140         g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
141         qemu_plugin_outs(out);
142     }
143 
144     if (do_log_writes && num == write_sysno) {
145         if (qemu_plugin_read_memory_vaddr(a2, memory_buffer, a3)) {
146             hexdump(memory_buffer);
147         } else {
148             fprintf(stderr, "Error reading memory from vaddr %"PRIu64"\n", a2);
149         }
150     }
151 }
152 
153 static void vcpu_syscall_ret(qemu_plugin_id_t id, unsigned int vcpu_idx,
154                              int64_t num, int64_t ret)
155 {
156     if (statistics) {
157         SyscallStats *entry;
158 
159         g_mutex_lock(&lock);
160         /* Should always return an existent entry. */
161         entry = get_or_create_entry(num);
162         if (ret < 0) {
163             entry->errors++;
164         }
165         g_mutex_unlock(&lock);
166     } else {
167         g_autofree gchar *out = g_strdup_printf(
168              "syscall #%" PRIi64 " returned -> %" PRIi64 "\n", num, ret);
169         qemu_plugin_outs(out);
170     }
171 }
172 
173 static void print_entry(gpointer val, gpointer user_data)
174 {
175     SyscallStats *entry = (SyscallStats *) val;
176     int64_t syscall_num = entry->num;
177     g_autofree gchar *out = g_strdup_printf(
178         "%-13" PRIi64 "%-6" PRIi64 " %" PRIi64 "\n",
179         syscall_num, entry->calls, entry->errors);
180     qemu_plugin_outs(out);
181 }
182 
183 static gint comp_func(gconstpointer ea, gconstpointer eb)
184 {
185     SyscallStats *ent_a = (SyscallStats *) ea;
186     SyscallStats *ent_b = (SyscallStats *) eb;
187 
188     return ent_a->calls > ent_b->calls ? -1 : 1;
189 }
190 
191 /* ************************************************************************* */
192 static void plugin_exit(qemu_plugin_id_t id, void *p)
193 {
194     if (!statistics) {
195         return;
196     }
197 
198     g_mutex_lock(&lock);
199     GList *entries = g_hash_table_get_values(statistics);
200     entries = g_list_sort(entries, comp_func);
201     qemu_plugin_outs("syscall no.  calls  errors\n");
202 
203     g_list_foreach(entries, print_entry, NULL);
204 
205     g_list_free(entries);
206     g_hash_table_destroy(statistics);
207     g_mutex_unlock(&lock);
208 }
209 
210 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
211                                            const qemu_info_t *info,
212                                            int argc, char **argv)
213 {
214     bool do_print = false;
215 
216     for (int i = 0; i < argc; i++) {
217         char *opt = argv[i];
218         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
219 
220         if (g_strcmp0(tokens[0], "print") == 0) {
221             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_print)) {
222                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
223             }
224         } else if (g_strcmp0(tokens[0], "log_writes") == 0) {
225             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_log_writes)) {
226                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
227             }
228         } else {
229             fprintf(stderr, "unsupported argument: %s\n", argv[i]);
230             return -1;
231         }
232     }
233 
234     if (!do_print) {
235         statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
236     }
237 
238     if (do_log_writes) {
239         for (const struct SyscallInfo *syscall_info = arch_syscall_info;
240             syscall_info->name != NULL; syscall_info++) {
241 
242             if (g_strcmp0(syscall_info->name, info->target_name) == 0) {
243                 write_sysno = syscall_info->write_sysno;
244                 break;
245             }
246         }
247 
248         if (write_sysno == -1) {
249             fprintf(stderr, "write syscall number not found\n");
250             return -1;
251         }
252 
253         memory_buffer = g_byte_array_new();
254     }
255 
256     qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
257     qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
258     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
259     return 0;
260 }
261