strace.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. int do_strace;
  24. /*
  25. * Utility functions
  26. */
  27. static void print_sysctl(const struct syscallname *name, abi_long arg1,
  28. abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
  29. abi_long arg6)
  30. {
  31. uint32_t i;
  32. int32_t *namep;
  33. gemu_log("%s({ ", name->name);
  34. namep = lock_user(VERIFY_READ, arg1, sizeof(int32_t) * arg2, 1);
  35. if (namep) {
  36. int32_t *p = namep;
  37. for (i = 0; i < (uint32_t)arg2; i++) {
  38. gemu_log("%d ", tswap32(*p++));
  39. }
  40. unlock_user(namep, arg1, 0);
  41. }
  42. gemu_log("}, %u, 0x" TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ", 0x"
  43. TARGET_ABI_FMT_lx ", 0x" TARGET_ABI_FMT_lx ")",
  44. (uint32_t)arg2, arg3, arg4, arg5, arg6);
  45. }
  46. static void print_execve(const struct syscallname *name, abi_long arg1,
  47. abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
  48. abi_long arg6)
  49. {
  50. abi_ulong arg_ptr_addr;
  51. char *s;
  52. s = lock_user_string(arg1);
  53. if (s == NULL) {
  54. return;
  55. }
  56. gemu_log("%s(\"%s\",{", name->name, s);
  57. unlock_user(s, arg1, 0);
  58. for (arg_ptr_addr = arg2; ; arg_ptr_addr += sizeof(abi_ulong)) {
  59. abi_ulong *arg_ptr, arg_addr;
  60. arg_ptr = lock_user(VERIFY_READ, arg_ptr_addr, sizeof(abi_ulong), 1);
  61. if (!arg_ptr) {
  62. return;
  63. }
  64. arg_addr = tswapl(*arg_ptr);
  65. unlock_user(arg_ptr, arg_ptr_addr, 0);
  66. if (!arg_addr) {
  67. break;
  68. }
  69. if ((s = lock_user_string(arg_addr))) {
  70. gemu_log("\"%s\",", s);
  71. unlock_user(s, arg_addr, 0);
  72. }
  73. }
  74. gemu_log("NULL})");
  75. }
  76. static void print_ioctl(const struct syscallname *name,
  77. abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4,
  78. abi_long arg5, abi_long arg6)
  79. {
  80. /* Decode the ioctl request */
  81. gemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x"
  82. TARGET_ABI_FMT_lx ", ...)",
  83. name->name,
  84. (int)arg1,
  85. (unsigned long)arg2,
  86. arg2 & IOC_OUT ? "R" : "",
  87. arg2 & IOC_IN ? "W" : "",
  88. (unsigned)IOCGROUP(arg2),
  89. isprint(IOCGROUP(arg2)) ? (char)IOCGROUP(arg2) : '?',
  90. (int)arg2 & 0xFF,
  91. (int)IOCPARM_LEN(arg2),
  92. arg3);
  93. }
  94. /*
  95. * Variants for the return value output function
  96. */
  97. static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
  98. {
  99. if (ret == -1) {
  100. gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno));
  101. } else {
  102. gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
  103. }
  104. }
  105. #if 0 /* currently unused */
  106. static void
  107. print_syscall_ret_raw(struct syscallname *name, abi_long ret)
  108. {
  109. gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret);
  110. }
  111. #endif
  112. /*
  113. * An array of all of the syscalls we know about
  114. */
  115. static const struct syscallname freebsd_scnames[] = {
  116. #include "freebsd/strace.list"
  117. };
  118. static const struct syscallname netbsd_scnames[] = {
  119. #include "netbsd/strace.list"
  120. };
  121. static const struct syscallname openbsd_scnames[] = {
  122. #include "openbsd/strace.list"
  123. };
  124. static void print_syscall(int num, const struct syscallname *scnames,
  125. unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3,
  126. abi_long arg4, abi_long arg5, abi_long arg6)
  127. {
  128. unsigned int i;
  129. const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
  130. TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
  131. TARGET_ABI_FMT_ld ")";
  132. gemu_log("%d ", getpid() );
  133. for (i = 0; i < nscnames; i++) {
  134. if (scnames[i].nr == num) {
  135. if (scnames[i].call != NULL) {
  136. scnames[i].call(&scnames[i], arg1, arg2, arg3, arg4, arg5,
  137. arg6);
  138. } else {
  139. /* XXX: this format system is broken because it uses
  140. host types and host pointers for strings */
  141. if (scnames[i].format != NULL) {
  142. format = scnames[i].format;
  143. }
  144. gemu_log(format, scnames[i].name, arg1, arg2, arg3, arg4, arg5,
  145. arg6);
  146. }
  147. return;
  148. }
  149. }
  150. gemu_log("Unknown syscall %d\n", num);
  151. }
  152. static void print_syscall_ret(int num, abi_long ret,
  153. const struct syscallname *scnames, unsigned int nscnames)
  154. {
  155. unsigned int i;
  156. for (i = 0; i < nscnames; i++) {
  157. if (scnames[i].nr == num) {
  158. if (scnames[i].result != NULL) {
  159. scnames[i].result(&scnames[i], ret);
  160. } else {
  161. if (ret < 0) {
  162. gemu_log(" = -1 errno=" TARGET_ABI_FMT_ld " (%s)\n", -ret,
  163. strerror(-ret));
  164. } else {
  165. gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
  166. }
  167. }
  168. break;
  169. }
  170. }
  171. }
  172. /*
  173. * The public interface to this module.
  174. */
  175. void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
  176. abi_long arg4, abi_long arg5, abi_long arg6)
  177. {
  178. print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
  179. arg3, arg4, arg5, arg6);
  180. }
  181. void print_freebsd_syscall_ret(int num, abi_long ret)
  182. {
  183. print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
  184. }
  185. void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
  186. abi_long arg4, abi_long arg5, abi_long arg6)
  187. {
  188. print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
  189. arg1, arg2, arg3, arg4, arg5, arg6);
  190. }
  191. void print_netbsd_syscall_ret(int num, abi_long ret)
  192. {
  193. print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
  194. }
  195. void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
  196. abi_long arg4, abi_long arg5, abi_long arg6)
  197. {
  198. print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
  199. arg3, arg4, arg5, arg6);
  200. }
  201. void print_openbsd_syscall_ret(int num, abi_long ret)
  202. {
  203. print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));
  204. }