TraceScreen.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. htop - TraceScreen.c
  3. (C) 2005-2006 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "TraceScreen.h"
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <signal.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <sys/select.h>
  19. #include <sys/wait.h>
  20. #include "CRT.h"
  21. #include "FunctionBar.h"
  22. #include "Panel.h"
  23. #include "ProvideCurses.h"
  24. #include "XUtils.h"
  25. static const char* const TraceScreenFunctions[] = {"Search ", "Filter ", "AutoScroll ", "Stop Tracing ", "Done ", NULL};
  26. static const char* const TraceScreenKeys[] = {"F3", "F4", "F8", "F9", "Esc"};
  27. static const int TraceScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(8), KEY_F(9), 27};
  28. TraceScreen* TraceScreen_new(const Process* process) {
  29. // This initializes all TraceScreen variables to "false" so only default = true ones need to be set below
  30. TraceScreen* this = xCalloc(1, sizeof(TraceScreen));
  31. Object_setClass(this, Class(TraceScreen));
  32. this->tracing = true;
  33. this->strace_alive = false;
  34. FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents);
  35. CRT_disableDelay();
  36. return (TraceScreen*) InfoScreen_init(&this->super, process, fuBar, LINES - 2, " ");
  37. }
  38. void TraceScreen_delete(Object* cast) {
  39. TraceScreen* this = (TraceScreen*) cast;
  40. if (this->child > 0) {
  41. kill(this->child, SIGTERM);
  42. while (waitpid(this->child, NULL, 0) == -1)
  43. if (errno != EINTR)
  44. break;
  45. }
  46. if (this->strace) {
  47. fclose(this->strace);
  48. }
  49. CRT_enableDelay();
  50. free(InfoScreen_done((InfoScreen*)this));
  51. }
  52. static void TraceScreen_draw(InfoScreen* this) {
  53. InfoScreen_drawTitled(this, "Trace of process %d - %s", Process_getPid(this->process), Process_getCommand(this->process));
  54. }
  55. bool TraceScreen_forkTracer(TraceScreen* this) {
  56. int fdpair[2] = {0, 0};
  57. if (pipe(fdpair) == -1)
  58. return false;
  59. if (fcntl(fdpair[0], F_SETFL, O_NONBLOCK) < 0)
  60. goto err;
  61. if (fcntl(fdpair[1], F_SETFL, O_NONBLOCK) < 0)
  62. goto err;
  63. pid_t child = fork();
  64. if (child == -1)
  65. goto err;
  66. if (child == 0) {
  67. close(fdpair[0]);
  68. dup2(fdpair[1], STDOUT_FILENO);
  69. dup2(fdpair[1], STDERR_FILENO);
  70. close(fdpair[1]);
  71. char buffer[32] = {0};
  72. xSnprintf(buffer, sizeof(buffer), "%d", Process_getPid(this->super.process));
  73. #if defined(HTOP_FREEBSD) || defined(HTOP_OPENBSD) || defined(HTOP_NETBSD) || defined(HTOP_DRAGONFLYBSD) || defined(HTOP_SOLARIS)
  74. // Use of NULL in variadic functions must have a pointer cast.
  75. // The NULL constant is not required by standard to have a pointer type.
  76. execlp("truss", "truss", "-s", "512", "-p", buffer, (void*)NULL);
  77. // Should never reach here, unless execlp fails ...
  78. const char* message = "Could not execute 'truss'. Please make sure it is available in your $PATH.";
  79. (void)! write(STDERR_FILENO, message, strlen(message));
  80. #elif defined(HTOP_LINUX)
  81. execlp("strace", "strace", "-T", "-tt", "-s", "512", "-p", buffer, (void*)NULL);
  82. // Should never reach here, unless execlp fails ...
  83. const char* message = "Could not execute 'strace'. Please make sure it is available in your $PATH.";
  84. (void)! write(STDERR_FILENO, message, strlen(message));
  85. #else // HTOP_DARWIN, HTOP_PCP == HTOP_UNSUPPORTED
  86. const char* message = "Tracing unavailable on not supported system.";
  87. (void)! write(STDERR_FILENO, message, strlen(message));
  88. #endif
  89. exit(127);
  90. }
  91. FILE* fp = fdopen(fdpair[0], "r");
  92. if (!fp)
  93. goto err;
  94. close(fdpair[1]);
  95. this->child = child;
  96. this->strace = fp;
  97. this->strace_alive = true;
  98. return true;
  99. err:
  100. close(fdpair[1]);
  101. close(fdpair[0]);
  102. return false;
  103. }
  104. static void TraceScreen_updateTrace(InfoScreen* super) {
  105. TraceScreen* this = (TraceScreen*) super;
  106. int fd_strace = fileno(this->strace);
  107. fd_set fds;
  108. FD_ZERO(&fds);
  109. FD_SET(STDIN_FILENO, &fds);
  110. if (this->strace_alive) {
  111. assert(fd_strace != -1);
  112. FD_SET(fd_strace, &fds);
  113. }
  114. struct timeval tv = { .tv_sec = 0, .tv_usec = 500 };
  115. int ready = select(MAXIMUM(STDIN_FILENO, fd_strace) + 1, &fds, NULL, NULL, &tv);
  116. char buffer[1025];
  117. size_t nread = 0;
  118. if (ready > 0 && FD_ISSET(fd_strace, &fds))
  119. nread = fread(buffer, 1, sizeof(buffer) - 1, this->strace);
  120. if (nread && this->tracing) {
  121. const char* line = buffer;
  122. buffer[nread] = '\0';
  123. for (size_t i = 0; i < nread; i++) {
  124. if (buffer[i] == '\n') {
  125. buffer[i] = '\0';
  126. if (this->contLine) {
  127. InfoScreen_appendLine(&this->super, line);
  128. this->contLine = false;
  129. } else {
  130. InfoScreen_addLine(&this->super, line);
  131. }
  132. line = buffer + i + 1;
  133. }
  134. }
  135. if (line < buffer + nread) {
  136. InfoScreen_addLine(&this->super, line);
  137. buffer[nread] = '\0';
  138. this->contLine = true;
  139. }
  140. if (this->follow) {
  141. Panel_setSelected(this->super.display, Panel_size(this->super.display) - 1);
  142. }
  143. } else {
  144. if (this->strace_alive && waitpid(this->child, NULL, WNOHANG) != 0)
  145. this->strace_alive = false;
  146. }
  147. }
  148. static bool TraceScreen_onKey(InfoScreen* super, int ch) {
  149. TraceScreen* this = (TraceScreen*) super;
  150. switch (ch) {
  151. case 'f':
  152. case KEY_F(8):
  153. this->follow = !(this->follow);
  154. if (this->follow)
  155. Panel_setSelected(super->display, Panel_size(super->display) - 1);
  156. return true;
  157. case 't':
  158. case KEY_F(9):
  159. this->tracing = !this->tracing;
  160. FunctionBar_setLabel(super->display->defaultBar, KEY_F(9), this->tracing ? "Stop Tracing " : "Resume Tracing ");
  161. InfoScreen_draw(this);
  162. return true;
  163. }
  164. this->follow = false;
  165. return false;
  166. }
  167. const InfoScreenClass TraceScreen_class = {
  168. .super = {
  169. .extends = Class(Object),
  170. .delete = TraceScreen_delete
  171. },
  172. .draw = TraceScreen_draw,
  173. .onErr = TraceScreen_updateTrace,
  174. .onKey = TraceScreen_onKey,
  175. };