/usr.bin/truss/powerpc-fbsd.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 351 lines · 211 code · 44 blank · 96 comment · 58 complexity · fde1f1fee5905e2b0667d64f17f1f083 MD5 · raw file

  1. /*
  2. * Copyright 2006 Peter Grehan <grehan@freebsd.org>
  3. * Copyright 2005 Orlando Bassotto <orlando@break.net>
  4. * Copyright 1998 Sean Eric Fagan
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifndef lint
  28. static const char rcsid[] =
  29. "$FreeBSD$";
  30. #endif /* not lint */
  31. /*
  32. * FreeBSD/powerpc-specific system call handling. This is probably the most
  33. * complex part of the entire truss program, although I've got lots of
  34. * it handled relatively cleanly now. The system call names are generated
  35. * automatically, thanks to /usr/src/sys/kern/syscalls.master. The
  36. * names used for the various structures are confusing, I sadly admit.
  37. *
  38. * This file is almost nothing more than a slightly-edited i386-fbsd.c.
  39. */
  40. #include <sys/types.h>
  41. #include <sys/ptrace.h>
  42. #include <sys/syscall.h>
  43. #include <machine/reg.h>
  44. #include <machine/frame.h>
  45. #include <err.h>
  46. #include <errno.h>
  47. #include <fcntl.h>
  48. #include <signal.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <time.h>
  53. #include <unistd.h>
  54. #include "truss.h"
  55. #include "syscall.h"
  56. #include "extern.h"
  57. #ifdef __powerpc64__ /* 32-bit compatibility */
  58. #include "freebsd32_syscalls.h"
  59. #define syscallnames freebsd32_syscallnames
  60. #else /* native 32-bit */
  61. #include "syscalls.h"
  62. #endif
  63. static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]);
  64. /*
  65. * This is what this particular file uses to keep track of a system call.
  66. * It is probably not quite sufficient -- I can probably use the same
  67. * structure for the various syscall personalities, and I also probably
  68. * need to nest system calls (for signal handlers).
  69. *
  70. * 'struct syscall' describes the system call; it may be NULL, however,
  71. * if we don't know about this particular system call yet.
  72. */
  73. struct freebsd_syscall {
  74. struct syscall *sc;
  75. const char *name;
  76. int number;
  77. unsigned long *args;
  78. int nargs; /* number of arguments -- *not* number of words! */
  79. char **s_args; /* the printable arguments */
  80. };
  81. static struct freebsd_syscall *
  82. alloc_fsc(void)
  83. {
  84. return (malloc(sizeof(struct freebsd_syscall)));
  85. }
  86. /* Clear up and free parts of the fsc structure. */
  87. static void
  88. free_fsc(struct freebsd_syscall *fsc)
  89. {
  90. int i;
  91. free(fsc->args);
  92. if (fsc->s_args) {
  93. for (i = 0; i < fsc->nargs; i++)
  94. free(fsc->s_args[i]);
  95. free(fsc->s_args);
  96. }
  97. free(fsc);
  98. }
  99. /*
  100. * Called when a process has entered a system call. nargs is the
  101. * number of words, not number of arguments (a necessary distinction
  102. * in some cases). Note that if the STOPEVENT() code in powerpc/powerpc/trap.c
  103. * is ever changed these functions need to keep up.
  104. */
  105. void
  106. powerpc_syscall_entry(struct trussinfo *trussinfo, int nargs)
  107. {
  108. struct ptrace_io_desc iorequest;
  109. struct reg regs;
  110. struct freebsd_syscall *fsc;
  111. struct syscall *sc;
  112. void *args;
  113. lwpid_t tid;
  114. int i, regargs, syscall_num;
  115. /* Account for a 64-bit argument with corresponding alignment. */
  116. nargs += 2;
  117. tid = trussinfo->curthread->tid;
  118. if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
  119. fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
  120. return;
  121. }
  122. /*
  123. * FreeBSD has two special kinds of system call redirctions --
  124. * SYS_syscall, and SYS___syscall. The former is the old syscall()
  125. * routine, basically; the latter is for quad-aligned arguments.
  126. */
  127. regargs = NARGREG;
  128. syscall_num = regs.fixreg[0];
  129. args = &regs.fixreg[3];
  130. if (syscall_num == SYS_syscall) {
  131. args = &regs.fixreg[4];
  132. regargs -= 1;
  133. syscall_num = regs.fixreg[3];
  134. } else if (syscall_num == SYS___syscall) {
  135. args = &regs.fixreg[5];
  136. regargs -= 2;
  137. syscall_num = regs.fixreg[4];
  138. }
  139. fsc = alloc_fsc();
  140. if (fsc == NULL)
  141. return;
  142. fsc->number = syscall_num;
  143. fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ?
  144. NULL : syscallnames[syscall_num];
  145. if (!fsc->name) {
  146. fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n",
  147. syscall_num);
  148. }
  149. if (fsc->name && (trussinfo->flags & FOLLOWFORKS) &&
  150. (strcmp(fsc->name, "fork") == 0 ||
  151. strcmp(fsc->name, "rfork") == 0 ||
  152. strcmp(fsc->name, "vfork") == 0))
  153. trussinfo->curthread->in_fork = 1;
  154. if (nargs == 0)
  155. return;
  156. fsc->args = malloc((1 + nargs) * sizeof(unsigned long));
  157. if (nargs > regargs) {
  158. memmove(&fsc->args[0], args, regargs * sizeof(fsc->args[0]));
  159. iorequest.piod_op = PIOD_READ_D;
  160. iorequest.piod_offs = (void *)(regs.fixreg[1] + 8);
  161. iorequest.piod_addr = &fsc->args[regargs];
  162. iorequest.piod_len = (nargs - regargs) * sizeof(fsc->args[0]);
  163. ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
  164. if (iorequest.piod_len == 0)
  165. return;
  166. } else
  167. memmove(&fsc->args[0], args, nargs * sizeof(fsc->args[0]));
  168. sc = get_syscall(fsc->name);
  169. if (sc)
  170. fsc->nargs = sc->nargs;
  171. else {
  172. #if DEBUG
  173. fprintf(trussinfo->outfile, "unknown syscall %s -- setting "
  174. "args to %d\n", fsc->name, nargs);
  175. #endif
  176. fsc->nargs = nargs;
  177. }
  178. fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *));
  179. fsc->sc = sc;
  180. /*
  181. * At this point, we set up the system call arguments.
  182. * We ignore any OUT ones, however -- those are arguments that
  183. * are set by the system call, and so are probably meaningless
  184. * now. This doesn't currently support arguments that are
  185. * passed in *and* out, however.
  186. */
  187. if (fsc->name) {
  188. #if DEBUG
  189. fprintf(stderr, "syscall %s(", fsc->name);
  190. #endif
  191. for (i = 0; i < fsc->nargs; i++) {
  192. #if DEBUG
  193. fprintf(stderr, "0x%x%s", sc ?
  194. fsc->args[sc->args[i].offset] : fsc->args[i],
  195. i < (fsc->nargs - 1) ? "," : "");
  196. #endif
  197. if (sc && !(sc->args[i].type & OUT)) {
  198. fsc->s_args[i] = print_arg(&sc->args[i],
  199. fsc->args, 0, trussinfo);
  200. }
  201. }
  202. #if DEBUG
  203. fprintf(stderr, ")\n");
  204. #endif
  205. }
  206. #if DEBUG
  207. fprintf(trussinfo->outfile, "\n");
  208. #endif
  209. if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 ||
  210. strcmp(fsc->name, "exit") == 0)) {
  211. /*
  212. * XXX
  213. * This could be done in a more general
  214. * manner but it still wouldn't be very pretty.
  215. */
  216. if (strcmp(fsc->name, "execve") == 0) {
  217. if ((trussinfo->flags & EXECVEARGS) == 0) {
  218. if (fsc->s_args[1]) {
  219. free(fsc->s_args[1]);
  220. fsc->s_args[1] = NULL;
  221. }
  222. }
  223. if ((trussinfo->flags & EXECVEENVS) == 0) {
  224. if (fsc->s_args[2]) {
  225. free(fsc->s_args[2]);
  226. fsc->s_args[2] = NULL;
  227. }
  228. }
  229. }
  230. }
  231. trussinfo->curthread->fsc = fsc;
  232. }
  233. /*
  234. * And when the system call is done, we handle it here.
  235. * Currently, no attempt is made to ensure that the system calls
  236. * match -- this needs to be fixed (and is, in fact, why S_SCX includes
  237. * the system call number instead of, say, an error status).
  238. */
  239. long
  240. powerpc_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused)
  241. {
  242. struct reg regs;
  243. struct freebsd_syscall *fsc;
  244. struct syscall *sc;
  245. lwpid_t tid;
  246. long retval;
  247. int errorp, i;
  248. if (trussinfo->curthread->fsc == NULL)
  249. return (-1);
  250. tid = trussinfo->curthread->tid;
  251. if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
  252. fprintf(trussinfo->outfile, "\n");
  253. return (-1);
  254. }
  255. retval = regs.fixreg[3];
  256. errorp = !!(regs.cr & 0x10000000);
  257. /*
  258. * This code, while simpler than the initial versions I used, could
  259. * stand some significant cleaning.
  260. */
  261. fsc = trussinfo->curthread->fsc;
  262. sc = fsc->sc;
  263. if (!sc) {
  264. for (i = 0; i < fsc->nargs; i++)
  265. asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]);
  266. } else {
  267. /*
  268. * On 32-bit big-endian, the low word of a 64-bit return is
  269. * in the greater address. Switch to this. XXX note that
  270. * print_syscall_ret can't handle 64-bit return values (llseek)
  271. */
  272. if (sc->ret_type == 2)
  273. retval = regs.fixreg[4];
  274. /*
  275. * Here, we only look for arguments that have OUT masked in --
  276. * otherwise, they were handled in the syscall_entry function.
  277. */
  278. for (i = 0; i < sc->nargs; i++) {
  279. char *temp;
  280. if (sc->args[i].type & OUT) {
  281. /*
  282. * If an error occurred, then don't bother
  283. * getting the data; it may not be valid.
  284. */
  285. if (errorp) {
  286. asprintf(&temp, "0x%lx",
  287. fsc->args[sc->args[i].offset]);
  288. } else {
  289. temp = print_arg(&sc->args[i],
  290. fsc->args, retval, trussinfo);
  291. }
  292. fsc->s_args[i] = temp;
  293. }
  294. }
  295. }
  296. if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 ||
  297. strcmp(fsc->name, "exit") == 0))
  298. trussinfo->curthread->in_syscall = 1;
  299. /*
  300. * It would probably be a good idea to merge the error handling,
  301. * but that complicates things considerably.
  302. */
  303. print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp,
  304. retval, fsc->sc);
  305. free_fsc(fsc);
  306. return (retval);
  307. }