syscall.c 7.1 KB

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