| 1 | /* |
| 2 | * System call tracing and debugging |
| 3 | * |
| 4 | * |
| 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 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 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. |
| 14 | * |
| 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/>. |
| 17 | */ |
| 18 | |
| 19 | #include "qemu/osdep.h" |
| 20 | #include <sys/select.h> |
| 21 | #include <sys/syscall.h> |
| 22 | #include <sys/ioccom.h> |
| 23 | |
| 24 | #include "qemu.h" |
| 25 | #include "user/tswap-target.h" |
| 26 | |
| 27 | #include "os-strace.h" /* OS dependent strace print functions */ |
| 28 | |
| 29 | int do_strace; |
| 30 | |
| 31 | /* |
| 32 | * Utility functions |
| 33 | */ |
| 34 | static const char * |
| 35 | get_comma(int last) |
| 36 | { |
| 37 | return (last) ? "" : ","; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Prints out raw parameter using given format. Caller needs |
| 42 | * to do byte swapping if needed. |
| 43 | */ |
| 44 | static void |
| 45 | print_raw_param(const char *fmt, abi_long param, int last) |
| 46 | { |
| 47 | char format[64]; |
| 48 | |
| 49 | (void)snprintf(format, sizeof(format), "%s%s", fmt, get_comma(last)); |
| 50 | gemu_log(format, param); |
| 51 | } |
| 52 | |
| 53 | static void print_sysctl(const struct syscallname *name, abi_long arg1, |
| 54 | abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, |
| 55 | abi_long arg6) |
| 56 | { |
| 57 | uint32_t i; |
| 58 | int32_t *namep; |
| 59 | |
| 60 | gemu_log("%s({ ", name->name); |
| 61 | namep = lock_user(VERIFY_READ, arg1, sizeof(int32_t) * arg2, 1); |
| 62 | if (namep) { |
| 63 | int32_t *p = namep; |
| 64 | |
| 65 | for (i = 0; i < (uint32_t)arg2; i++) { |
| 66 | gemu_log("%d ", tswap32(*p++)); |
| 67 | } |
| 68 | unlock_user(namep, arg1, 0); |
| 69 | } |
| 70 | gemu_log("}, %u, 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x" |
| 71 | TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ")", |
| 72 | (uint32_t)arg2, arg3, arg4, arg5, arg6); |
| 73 | } |
| 74 | |
| 75 | static void print_execve(const struct syscallname *name, abi_long arg1, |
| 76 | abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, |
| 77 | abi_long arg6) |
| 78 | { |
| 79 | abi_ulong arg_ptr_addr; |
| 80 | char *s; |
| 81 | |
| 82 | s = lock_user_string(arg1); |
| 83 | if (s == NULL) { |
| 84 | return; |
| 85 | } |
| 86 | gemu_log("%s(\"%s\",{", name->name, s); |
| 87 | unlock_user(s, arg1, 0); |
| 88 | |
| 89 | for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) { |
| 90 | abi_ulong *arg_ptr, arg_addr; |
| 91 | |
| 92 | arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1); |
| 93 | if (!arg_ptr) { |
| 94 | return; |
| 95 | } |
| 96 | arg_addr = tswapl(*arg_ptr); |
| 97 | unlock_user(arg_ptr, arg_ptr_addr, 0); |
| 98 | if (!arg_addr) { |
| 99 | break; |
| 100 | } |
| 101 | if ((s = lock_user_string(arg_addr))) { |
| 102 | gemu_log("\"%s\",", s); |
| 103 | unlock_user(s, arg_addr, 0); |
| 104 | } |
| 105 | } |
| 106 | gemu_log("NULL})"); |
| 107 | } |
| 108 | |
| 109 | static void print_ioctl(const struct syscallname *name, |
| 110 | abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4, |
| 111 | abi_long arg5, abi_long arg6) |
| 112 | { |
| 113 | /* Decode the ioctl request */ |
| 114 | gemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x" |
| 115 | TARGET_ABI_FMT_lx ", ...)", |
| 116 | name->name, |
| 117 | (int)arg1, |
| 118 | (unsigned long)arg2, |
| 119 | arg2 & IOC_OUT ? "R" : "", |
| 120 | arg2 & IOC_IN ? "W" : "", |
| 121 | (unsigned)IOCGROUP(arg2), |
| 122 | isprint(IOCGROUP(arg2)) ? (char)IOCGROUP(arg2) : '?', |
| 123 | (int)arg2 & 0xFF, |
| 124 | (int)IOCPARM_LEN(arg2), |
| 125 | arg3); |
| 126 | } |
| 127 | |
| 128 | static void print_sysarch(const struct syscallname *name, abi_long arg1, |
| 129 | abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, |
| 130 | abi_long arg6) |
| 131 | { |
| 132 | /* This is os dependent. */ |
| 133 | do_os_print_sysarch(name, arg1, arg2, arg3, arg4, arg5, arg6); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Variants for the return value output function |
| 138 | */ |
| 139 | |
| 140 | static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret) |
| 141 | { |
| 142 | if (ret == -1) { |
| 143 | gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno)); |
| 144 | } else { |
| 145 | gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * An array of all of the syscalls we know about |
| 151 | */ |
| 152 | |
| 153 | static const struct syscallname freebsd_scnames[] = { |
| 154 | #include "freebsd/strace.list" |
| 155 | }; |
| 156 | |
| 157 | static void print_syscall(int num, const struct syscallname *scnames, |
| 158 | unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3, |
| 159 | abi_long arg4, abi_long arg5, abi_long arg6) |
| 160 | { |
| 161 | unsigned int i; |
| 162 | const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," |
| 163 | TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," |
| 164 | TARGET_ABI_FMT_ld ")"; |
| 165 | |
| 166 | gemu_log("%d ", getpid() ); |
| 167 | |
| 168 | for (i = 0; i < nscnames; i++) { |
| 169 | if (scnames[i].nr == num) { |
| 170 | if (scnames[i].call != NULL) { |
| 171 | scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5, |
| 172 | arg6); |
| 173 | } else { |
| 174 | /* XXX: this format system is broken because it uses |
| 175 | host types and host pointers for strings */ |
| 176 | if (scnames[i].format != NULL) { |
| 177 | format = scnames[i].format; |
| 178 | } |
| 179 | gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5, |
| 180 | arg6); |
| 181 | } |
| 182 | return; |
| 183 | } |
| 184 | } |
| 185 | gemu_log("Unknown syscall %d\n", num); |
| 186 | } |
| 187 | |
| 188 | static void print_syscall_ret(int num, abi_long ret, |
| 189 | const struct syscallname *scnames, unsigned int nscnames) |
| 190 | { |
| 191 | unsigned int i; |
| 192 | |
| 193 | for (i = 0; i < nscnames; i++) { |
| 194 | if (scnames[i].nr == num) { |
| 195 | if (scnames[i].result != NULL) { |
| 196 | scnames[i].result(&scnames[i], ret); |
| 197 | } else { |
| 198 | if (ret < 0) { |
| 199 | gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret, |
| 200 | strerror(-ret)); |
| 201 | } else { |
| 202 | gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret); |
| 203 | } |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * The public interface to this module. |
| 212 | */ |
| 213 | void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3, |
| 214 | abi_long arg4, abi_long arg5, abi_long arg6) |
| 215 | { |
| 216 | |
| 217 | print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2, |
| 218 | arg3, arg4, arg5, arg6); |
| 219 | } |
| 220 | |
| 221 | void print_freebsd_syscall_ret(int num, abi_long ret) |
| 222 | { |
| 223 | |
| 224 | print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames)); |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | print_signal(abi_ulong arg, int last) |
| 229 | { |
| 230 | const char *signal_name = NULL; |
| 231 | switch (arg) { |
| 232 | case TARGET_SIGHUP: |
| 233 | signal_name = "SIGHUP"; |
| 234 | break; |
| 235 | case TARGET_SIGINT: |
| 236 | signal_name = "SIGINT"; |
| 237 | break; |
| 238 | case TARGET_SIGQUIT: |
| 239 | signal_name = "SIGQUIT"; |
| 240 | break; |
| 241 | case TARGET_SIGILL: |
| 242 | signal_name = "SIGILL"; |
| 243 | break; |
| 244 | case TARGET_SIGABRT: |
| 245 | signal_name = "SIGABRT"; |
| 246 | break; |
| 247 | case TARGET_SIGFPE: |
| 248 | signal_name = "SIGFPE"; |
| 249 | break; |
| 250 | case TARGET_SIGKILL: |
| 251 | signal_name = "SIGKILL"; |
| 252 | break; |
| 253 | case TARGET_SIGSEGV: |
| 254 | signal_name = "SIGSEGV"; |
| 255 | break; |
| 256 | case TARGET_SIGPIPE: |
| 257 | signal_name = "SIGPIPE"; |
| 258 | break; |
| 259 | case TARGET_SIGALRM: |
| 260 | signal_name = "SIGALRM"; |
| 261 | break; |
| 262 | case TARGET_SIGTERM: |
| 263 | signal_name = "SIGTERM"; |
| 264 | break; |
| 265 | case TARGET_SIGUSR1: |
| 266 | signal_name = "SIGUSR1"; |
| 267 | break; |
| 268 | case TARGET_SIGUSR2: |
| 269 | signal_name = "SIGUSR2"; |
| 270 | break; |
| 271 | case TARGET_SIGCHLD: |
| 272 | signal_name = "SIGCHLD"; |
| 273 | break; |
| 274 | case TARGET_SIGCONT: |
| 275 | signal_name = "SIGCONT"; |
| 276 | break; |
| 277 | case TARGET_SIGSTOP: |
| 278 | signal_name = "SIGSTOP"; |
| 279 | break; |
| 280 | case TARGET_SIGTTIN: |
| 281 | signal_name = "SIGTTIN"; |
| 282 | break; |
| 283 | case TARGET_SIGTTOU: |
| 284 | signal_name = "SIGTTOU"; |
| 285 | break; |
| 286 | } |
| 287 | if (signal_name == NULL) { |
| 288 | print_raw_param("%ld", arg, last); |
| 289 | return; |
| 290 | } |
| 291 | gemu_log("%s%s", signal_name, get_comma(last)); |
| 292 | } |
| 293 | |
| 294 | void print_taken_signal(int target_signum, const target_siginfo_t *tinfo) |
| 295 | { |
| 296 | /* |
| 297 | * Print the strace output for a signal being taken: |
| 298 | * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} --- |
| 299 | */ |
| 300 | gemu_log("%d ", getpid()); |
| 301 | gemu_log("--- "); |
| 302 | print_signal(target_signum, 1); |
| 303 | gemu_log(" ---\n"); |
| 304 | } |