Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tracing_backend.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Implementation of functions related to the tracing backend interface.
  3. *
  4. * Copyright (c) 2017-2019 The strace developers.
  5. * All rights reserved.
  6. *
  7. * SPDX-License-Identifier: LGPL-2.1-or-later
  8. */
  9. #include "defs.h"
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include "tracing_backend.h"
  14. #if ADDITIONAL_TRACING_BACKENDS
  15. #include <assert.h>
  16. #include <limits.h>
  17. #include <signal.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/socket.h>
  21. #include <sys/xattr.h>
  22. #include "ptrace_backend.h"
  23. /* Tracing backeng management functions */
  24. const struct tracing_backend *cur_tracing_backend = &ptrace_backend;
  25. void
  26. set_tracing_backend(struct tracing_backend *backend)
  27. {
  28. /* Trying to catch uninitialised tracing backend fields early */
  29. assert(backend->name);
  30. assert(backend->kill);
  31. assert(backend->next_event);
  32. assert(backend->restart_process);
  33. assert(backend->clear_regs);
  34. assert(backend->get_regs);
  35. assert(backend->get_scno);
  36. assert(backend->set_scno);
  37. assert(backend->set_error);
  38. assert(backend->set_success);
  39. assert(backend->get_syscall_args);
  40. assert(backend->get_syscall_result);
  41. assert(backend->umoven);
  42. assert(backend->umovestr);
  43. assert(backend->upeek);
  44. assert(backend->upoke);
  45. assert(backend->open);
  46. assert(backend->pread);
  47. assert(backend->close);
  48. assert(backend->readlink);
  49. cur_tracing_backend = backend;
  50. }
  51. # ifdef O_PATH
  52. # define O_PATH_ O_PATH
  53. # else
  54. # define O_PATH_ 0
  55. # endif
  56. int
  57. tracing_backend_stat_via_fstat(struct tcb *tcp, const char *path,
  58. strace_stat_t *buf)
  59. {
  60. int fd = cur_tracing_backend->open(tcp, path, O_NOATIME | O_PATH_, 0);
  61. int ret;
  62. int errnum = 0;
  63. if (fd < 0)
  64. return -1;
  65. ret = cur_tracing_backend->fstat(tcp, fd, buf);
  66. if (ret < 0)
  67. errnum = errno;
  68. cur_tracing_backend->close(tcp, fd);
  69. errno = errnum;
  70. return ret;
  71. }
  72. #undef O_PATH_
  73. /* Simple syscall wrappers for a local tracing backend */
  74. int
  75. local_kill(struct tcb *tcp, int sig)
  76. {
  77. return kill(tcp->pid, sig);
  78. }
  79. char *
  80. local_realpath(struct tcb *tcp, const char *path, char *resolved_path)
  81. {
  82. return realpath(path, resolved_path);
  83. }
  84. int
  85. local_open(struct tcb *tcp, const char *path, int flags, int mode)
  86. {
  87. return open_file(path, flags, mode);
  88. }
  89. ssize_t
  90. local_pread(struct tcb *tcp, int fd, void *buf, size_t count, off_t offset)
  91. {
  92. return pread(fd, buf, count, offset);
  93. }
  94. int
  95. local_close(struct tcb *tcp, int fd)
  96. {
  97. return close(fd);
  98. }
  99. ssize_t
  100. local_readlink(struct tcb *tcp, const char *path, char *buf, size_t buf_size)
  101. {
  102. return readlink(path, buf, buf_size);
  103. }
  104. int
  105. local_stat(struct tcb *tcp, const char *path, strace_stat_t *buf)
  106. {
  107. return stat_file(path, buf);
  108. }
  109. int
  110. local_fstat(struct tcb *tcp, int fd, strace_stat_t *buf)
  111. {
  112. return fstat_file(fd, buf);
  113. }
  114. ssize_t
  115. local_getxattr(struct tcb *tcp, const char *path, const char *name, void *buf,
  116. size_t buf_size)
  117. {
  118. return getxattr(path, name, buf, buf_size);
  119. }
  120. int
  121. local_socket(struct tcb *tcp, int domain, int type, int protocol)
  122. {
  123. return socket(domain, type, protocol);
  124. }
  125. ssize_t
  126. local_sendmsg(struct tcb *tcp, int fd, const struct msghdr *msg, int flags)
  127. {
  128. return sendmsg(fd, msg, flags);
  129. }
  130. ssize_t
  131. local_recvmsg(struct tcb *tcp, int fd, struct msghdr *msg, int flags)
  132. {
  133. return recvmsg(fd, msg, flags);
  134. }
  135. #endif /* ADDITIONAL_TRACING_BACKENDS */