TraceScreen.c 5.01 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - TraceScreen.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2005-2006 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
9
10
11
12
13
#include "TraceScreen.h"

#include "CRT.h"
#include "ProcessList.h"
#include "ListItem.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
14
15
#include <stdio.h>
#include <unistd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
16
17
#include <string.h>
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
18
19
20
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
21
22
#include <sys/types.h>
#include <sys/wait.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
23

Hisham Muhammad's avatar
Hisham Muhammad committed
24
/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
25
#include "Process.h"
26
#include "Panel.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
27
#include "FunctionBar.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
28
29
30

typedef struct TraceScreen_ {
   Process* process;
31
   Panel* display;
Hisham Muhammad's avatar
Hisham Muhammad committed
32
33
34
35
36
37
   FunctionBar* bar;
   bool tracing;
} TraceScreen;

}*/

38
static const char* tsFunctions[] = {"AutoScroll ", "Stop Tracing   ", "Done   ", NULL};
Hisham Muhammad's avatar
Hisham Muhammad committed
39

40
static const char* tsKeys[] = {"F4", "F5", "Esc"};
Hisham Muhammad's avatar
Hisham Muhammad committed
41

42
static int tsEvents[] = {KEY_F(4), KEY_F(5), 27};
Hisham Muhammad's avatar
Hisham Muhammad committed
43
44
45
46

TraceScreen* TraceScreen_new(Process* process) {
   TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
   this->process = process;
47
   this->display = Panel_new(0, 1, COLS, LINES-2, LISTITEM_CLASS, true, ListItem_compare);
48
   this->bar = FunctionBar_new(tsFunctions, tsKeys, tsEvents);
Hisham Muhammad's avatar
Hisham Muhammad committed
49
50
51
52
53
   this->tracing = true;
   return this;
}

void TraceScreen_delete(TraceScreen* this) {
54
   Panel_delete((Object*)this->display);
Hisham Muhammad's avatar
Hisham Muhammad committed
55
56
57
58
   FunctionBar_delete((Object*)this->bar);
   free(this);
}

59
static void TraceScreen_draw(TraceScreen* this) {
Hisham Muhammad's avatar
Hisham Muhammad committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
   attrset(CRT_colors[PANEL_HEADER_FOCUS]);
   mvhline(0, 0, ' ', COLS);
   mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm);
   attrset(CRT_colors[DEFAULT_COLOR]);
   FunctionBar_draw(this->bar, NULL);
}

void TraceScreen_run(TraceScreen* this) {
//   if (this->process->pid == getpid()) return;
   char buffer[1001];
   int fdpair[2];
   int err = pipe(fdpair);
   if (err == -1) return;
   int child = fork();
   if (child == -1) return;
   if (child == 0) {
      dup2(fdpair[1], STDERR_FILENO);
      fcntl(fdpair[1], F_SETFL, O_NONBLOCK);
      sprintf(buffer, "%d", this->process->pid);
      execlp("strace", "strace", "-p", buffer, NULL);
80
81
      const char* message = "Could not execute 'strace'. Please make sure it is available in your $PATH.";
      write(fdpair[1], message, strlen(message));
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
84
85
      exit(1);
   }
   fcntl(fdpair[0], F_SETFL, O_NONBLOCK);
   FILE* strace = fdopen(fdpair[0], "r");
Hisham Muhammad's avatar
Hisham Muhammad committed
86
   Panel* panel = this->display;
Hisham Muhammad's avatar
Hisham Muhammad committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
   int fd_strace = fileno(strace);
   TraceScreen_draw(this);
   CRT_disableDelay();
   bool contLine = false;
   bool follow = false;
   bool looping = true;
   while (looping) {
      fd_set fds;
      FD_ZERO(&fds);
      FD_SET(fd_strace, &fds);
      struct timeval tv;
      tv.tv_sec = 0; tv.tv_usec = 500;
      int ready = select(fd_strace+1, &fds, NULL, NULL, &tv);
      int nread = 0;
      if (ready > 0)
         nread = fread(buffer, 1, 1000, strace);
      if (nread && this->tracing) {
         char* line = buffer;
         buffer[nread] = '\0';
         for (int i = 0; i < nread; i++) {
            if (buffer[i] == '\n') {
               buffer[i] = '\0';
               if (contLine) {
Hisham Muhammad's avatar
Hisham Muhammad committed
110
                  ListItem_append((ListItem*)Panel_get(panel,
Hisham Muhammad's avatar
Hisham Muhammad committed
111
                     Panel_size(panel)-1), line);
Hisham Muhammad's avatar
Hisham Muhammad committed
112
113
                  contLine = false;
               } else {
Hisham Muhammad's avatar
Hisham Muhammad committed
114
                  Panel_add(panel, (Object*) ListItem_new(line, 0));
Hisham Muhammad's avatar
Hisham Muhammad committed
115
116
117
118
119
               }
               line = buffer+i+1;
            }
         }
         if (line < buffer+nread) {
Hisham Muhammad's avatar
Hisham Muhammad committed
120
            Panel_add(panel, (Object*) ListItem_new(line, 0));
Hisham Muhammad's avatar
Hisham Muhammad committed
121
122
123
124
            buffer[nread] = '\0';
            contLine = true;
         }
         if (follow)
Hisham Muhammad's avatar
Hisham Muhammad committed
125
            Panel_setSelected(panel, Panel_size(panel)-1);
Hisham Muhammad's avatar
Hisham Muhammad committed
126
         Panel_draw(panel, true);
Hisham Muhammad's avatar
Hisham Muhammad committed
127
128
129
130
131
132
      }
      int ch = getch();
      if (ch == KEY_MOUSE) {
         MEVENT mevent;
         int ok = getmouse(&mevent);
         if (ok == OK)
Hisham Muhammad's avatar
Hisham Muhammad committed
133
134
            if (mevent.y >= panel->y && mevent.y < LINES - 1) {
               Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
Hisham Muhammad's avatar
Hisham Muhammad committed
135
136
137
138
139
140
141
142
143
144
145
146
147
               follow = false;
               ch = 0;
            } if (mevent.y == LINES - 1)
               ch = FunctionBar_synthesizeEvent(this->bar, mevent.x);
      }
      switch(ch) {
      case ERR:
         continue;
      case KEY_F(5):
         this->tracing = !this->tracing;
         FunctionBar_setLabel(this->bar, KEY_F(5), this->tracing?"Stop Tracing   ":"Resume Tracing ");
         TraceScreen_draw(this);
         break;
148
149
150
151
152
153
      case KEY_HOME:
         Panel_setSelected(panel, 0);
         break;
      case KEY_END:
         Panel_setSelected(panel, Panel_size(panel)-1);
         break;
Hisham Muhammad's avatar
Hisham Muhammad committed
154
155
156
157
      case 'f':
      case KEY_F(4):
         follow = !follow;
         if (follow)
Hisham Muhammad's avatar
Hisham Muhammad committed
158
            Panel_setSelected(panel, Panel_size(panel)-1);
Hisham Muhammad's avatar
Hisham Muhammad committed
159
160
161
         break;
      case 'q':
      case 27:
162
      case KEY_F(10):
Hisham Muhammad's avatar
Hisham Muhammad committed
163
164
165
         looping = false;
         break;
      case KEY_RESIZE:
Hisham Muhammad's avatar
Hisham Muhammad committed
166
         Panel_resize(panel, COLS, LINES-2);
Hisham Muhammad's avatar
Hisham Muhammad committed
167
168
169
170
         TraceScreen_draw(this);
         break;
      default:
         follow = false;
Hisham Muhammad's avatar
Hisham Muhammad committed
171
         Panel_onKey(panel, ch);
Hisham Muhammad's avatar
Hisham Muhammad committed
172
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
173
      Panel_draw(panel, true);
Hisham Muhammad's avatar
Hisham Muhammad committed
174
175
   }
   kill(child, SIGTERM);
176
   waitpid(child, NULL, 0);
Hisham Muhammad's avatar
Hisham Muhammad committed
177
   fclose(strace);
Hisham Muhammad's avatar
Hisham Muhammad committed
178
   CRT_enableDelay();
Hisham Muhammad's avatar
Hisham Muhammad committed
179
}