2
0

strace.c 6.8 KB

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