2
0

strace.c 9.2 KB

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