strace.c 9.2 KB

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