logo

mstrace

Small Linux strace(1) implementationgit clone https://anongit.hacktivis.me/git/mstrace.git/

print_syscall.c (1742B)


  1. // SPDX-FileCopyrightText: 2024 Haelwenn (lanodan) Monnier <contact+mstrace@hacktivis.me>
  2. // SPDX-License-Identifier: MPL-2.0
  3. #define _DEFAULT_SOURCE
  4. #include "strsyscall.h"
  5. #include <assert.h>
  6. #include <inttypes.h> // PRIu64
  7. #include <limits.h> // PATH_MAX
  8. #include <linux/ptrace.h> // __u64
  9. #include <stdio.h> // fprintf
  10. #include <string.h> // memcpy
  11. #include <sys/ptrace.h> // ptrace()
  12. #include <sys/syscall.h> // SYS_*
  13. #include <unistd.h> // pid_t
  14. extern pid_t child;
  15. static char *
  16. read_str(intptr_t addr, size_t len)
  17. {
  18. static char str_buf[sizeof(unsigned long) * 128] = "";
  19. if(len == 0 || len > sizeof(unsigned long) * 128) len = sizeof(unsigned long) * 128;
  20. size_t i = 0;
  21. while(i < len)
  22. {
  23. unsigned long tmp = ptrace(PTRACE_PEEKDATA, child, addr + i);
  24. memcpy(str_buf + i, &tmp, sizeof(tmp));
  25. i += sizeof(tmp);
  26. if(memchr(&tmp, 0, sizeof(tmp)) != NULL) break;
  27. if(i > len)
  28. {
  29. str_buf[len - 1] = '\0';
  30. break;
  31. }
  32. }
  33. return str_buf;
  34. }
  35. #define Pri_str "\"%s\""
  36. #define Pri_i "%" PRIi64
  37. #define Pri_u "%" PRIu64
  38. #define Pri_o "0o%" PRIo64
  39. #define Pri_x "0x%" PRIx64
  40. void
  41. print_syscall(__u64 nr, __u64 args[6])
  42. {
  43. switch(nr)
  44. {
  45. #include "syscalls_cases.h"
  46. // fallback
  47. default:
  48. assert(nr < SYSCALLS_MAX);
  49. if(str_syscalls[nr] != NULL)
  50. {
  51. fprintf(stderr, "%s(", str_syscalls[nr]);
  52. }
  53. else
  54. {
  55. fprintf(stderr, "syscall(%" PRIu64 ", ", (uint64_t)nr);
  56. }
  57. fprintf(stderr,
  58. "0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64
  59. ")",
  60. (uint64_t)args[0],
  61. (uint64_t)args[1],
  62. (uint64_t)args[2],
  63. (uint64_t)args[3],
  64. (uint64_t)args[4],
  65. (uint64_t)args[5]);
  66. }
  67. }