2006-03-04 18:16:49 +00:00
|
|
|
/*
|
|
|
|
htop - TraceScreen.c
|
2006-03-23 18:55:29 +00:00
|
|
|
(C) 2005-2006 Hisham H. Muhammad
|
2006-03-04 18:16:49 +00:00
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
*/
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "TraceScreen.h"
|
|
|
|
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "ListItem.h"
|
2012-11-10 00:31:37 +00:00
|
|
|
#include "IncSet.h"
|
|
|
|
#include "String.h"
|
2015-03-23 18:26:56 +00:00
|
|
|
#include "FunctionBar.h"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2006-05-09 17:58:40 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2012-01-03 18:19:11 +00:00
|
|
|
#include <signal.h>
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
/*{
|
2006-03-04 18:16:49 +00:00
|
|
|
#include "Process.h"
|
2006-05-30 13:47:28 +00:00
|
|
|
#include "Panel.h"
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
typedef struct TraceScreen_ {
|
|
|
|
Process* process;
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel* display;
|
2006-03-04 18:16:49 +00:00
|
|
|
bool tracing;
|
|
|
|
} TraceScreen;
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
static const char* TraceScreenFunctions[] = {"Search ", "Filter ", "AutoScroll ", "Stop Tracing ", "Done ", NULL};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
static const char* TraceScreenKeys[] = {"F3", "F4", "F8", "F9", "Esc"};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
static int TraceScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(8), KEY_F(9), 27};
|
2006-03-04 18:16:49 +00:00
|
|
|
|
|
|
|
TraceScreen* TraceScreen_new(Process* process) {
|
|
|
|
TraceScreen* this = (TraceScreen*) malloc(sizeof(TraceScreen));
|
|
|
|
this->process = process;
|
2015-03-23 18:26:56 +00:00
|
|
|
FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents);
|
|
|
|
this->display = Panel_new(0, 1, COLS, LINES-2, false, Class(ListItem), fuBar);
|
2006-03-04 18:16:49 +00:00
|
|
|
this->tracing = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraceScreen_delete(TraceScreen* this) {
|
2006-05-30 13:47:28 +00:00
|
|
|
Panel_delete((Object*)this->display);
|
2006-03-04 18:16:49 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2012-11-10 00:31:37 +00:00
|
|
|
static void TraceScreen_draw(TraceScreen* this, IncSet* inc) {
|
2006-03-04 18:16:49 +00:00
|
|
|
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]);
|
2012-11-10 00:31:37 +00:00
|
|
|
IncSet_drawBar(inc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) {
|
|
|
|
Vector_add(lines, (Object*) ListItem_new(line, 0));
|
|
|
|
if (!incFilter || String_contains_i(line, incFilter))
|
|
|
|
Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void appendLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) {
|
|
|
|
ListItem* last = (ListItem*)Vector_get(lines, Vector_size(lines)-1);
|
|
|
|
ListItem_append(last, line);
|
|
|
|
if (incFilter && Panel_get(panel, Panel_size(panel)-1) != (Object*)last && String_contains_i(line, incFilter))
|
|
|
|
Panel_add(panel, (Object*)last);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TraceScreen_run(TraceScreen* this) {
|
|
|
|
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);
|
2014-04-22 23:34:09 +00:00
|
|
|
int ok = fcntl(fdpair[1], F_SETFL, O_NONBLOCK);
|
|
|
|
if (ok != -1) {
|
|
|
|
sprintf(buffer, "%d", this->process->pid);
|
|
|
|
execlp("strace", "strace", "-p", buffer, NULL);
|
|
|
|
}
|
2006-06-03 23:14:09 +00:00
|
|
|
const char* message = "Could not execute 'strace'. Please make sure it is available in your $PATH.";
|
2015-05-15 09:33:25 +00:00
|
|
|
ssize_t written = write(fdpair[1], message, strlen(message));
|
|
|
|
(void) written;
|
2006-03-04 18:16:49 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fcntl(fdpair[0], F_SETFL, O_NONBLOCK);
|
|
|
|
FILE* strace = fdopen(fdpair[0], "r");
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel* panel = this->display;
|
2006-03-04 18:16:49 +00:00
|
|
|
int fd_strace = fileno(strace);
|
|
|
|
CRT_disableDelay();
|
|
|
|
bool contLine = false;
|
|
|
|
bool follow = false;
|
|
|
|
bool looping = true;
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2015-03-23 18:26:56 +00:00
|
|
|
FunctionBar* bar = panel->defaultBar;
|
2012-11-10 00:31:37 +00:00
|
|
|
IncSet* inc = IncSet_new(bar);
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE);
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
TraceScreen_draw(this, inc);
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
while (looping) {
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
Panel_draw(panel, true);
|
|
|
|
const char* incFilter = IncSet_filter(inc);
|
|
|
|
|
|
|
|
if (inc->active)
|
|
|
|
move(LINES-1, CRT_cursorX);
|
|
|
|
int ch = getch();
|
|
|
|
|
|
|
|
if (ch == ERR) {
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
2012-12-05 15:12:20 +00:00
|
|
|
// FD_SET(STDIN_FILENO, &fds);
|
2012-11-10 00:31:37 +00:00
|
|
|
FD_SET(fd_strace, &fds);
|
2012-12-05 15:12:20 +00:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0; tv.tv_usec = 500;
|
|
|
|
int ready = select(fd_strace+1, &fds, NULL, NULL, &tv);
|
2012-11-10 00:31:37 +00:00
|
|
|
int nread = 0;
|
|
|
|
if (ready > 0 && FD_ISSET(fd_strace, &fds))
|
|
|
|
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) {
|
|
|
|
appendLine(line, lines, panel, incFilter);
|
|
|
|
contLine = false;
|
|
|
|
} else {
|
|
|
|
addLine(line, lines, panel, incFilter);
|
|
|
|
}
|
|
|
|
line = buffer+i+1;
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
if (line < buffer+nread) {
|
|
|
|
addLine(line, lines, panel, incFilter);
|
|
|
|
buffer[nread] = '\0';
|
|
|
|
contLine = true;
|
|
|
|
}
|
|
|
|
if (follow)
|
|
|
|
Panel_setSelected(panel, Panel_size(panel)-1);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
if (ch == KEY_MOUSE) {
|
|
|
|
MEVENT mevent;
|
|
|
|
int ok = getmouse(&mevent);
|
|
|
|
if (ok == OK)
|
2006-05-30 14:00:18 +00:00
|
|
|
if (mevent.y >= panel->y && mevent.y < LINES - 1) {
|
|
|
|
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
|
2006-03-04 18:16:49 +00:00
|
|
|
follow = false;
|
|
|
|
ch = 0;
|
|
|
|
} if (mevent.y == LINES - 1)
|
2015-03-23 20:04:53 +00:00
|
|
|
ch = IncSet_synthesizeEvent(inc, mevent.x);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
if (inc->active) {
|
|
|
|
IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
switch(ch) {
|
|
|
|
case ERR:
|
|
|
|
continue;
|
2010-07-31 00:11:34 +00:00
|
|
|
case KEY_HOME:
|
|
|
|
Panel_setSelected(panel, 0);
|
|
|
|
break;
|
|
|
|
case KEY_END:
|
|
|
|
Panel_setSelected(panel, Panel_size(panel)-1);
|
|
|
|
break;
|
2012-11-10 00:31:37 +00:00
|
|
|
case KEY_F(3):
|
|
|
|
case '/':
|
2015-03-23 20:04:53 +00:00
|
|
|
IncSet_activate(inc, INC_SEARCH, panel);
|
2012-11-10 00:31:37 +00:00
|
|
|
break;
|
2006-03-04 18:16:49 +00:00
|
|
|
case KEY_F(4):
|
2012-11-10 00:31:37 +00:00
|
|
|
case '\\':
|
2015-03-23 20:04:53 +00:00
|
|
|
IncSet_activate(inc, INC_FILTER, panel);
|
2012-11-10 00:31:37 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
case KEY_F(8):
|
2006-03-04 18:16:49 +00:00
|
|
|
follow = !follow;
|
|
|
|
if (follow)
|
2009-06-02 04:51:23 +00:00
|
|
|
Panel_setSelected(panel, Panel_size(panel)-1);
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
2012-11-10 00:31:37 +00:00
|
|
|
case 't':
|
|
|
|
case KEY_F(9):
|
|
|
|
this->tracing = !this->tracing;
|
|
|
|
FunctionBar_setLabel(bar, KEY_F(9), this->tracing?"Stop Tracing ":"Resume Tracing ");
|
|
|
|
TraceScreen_draw(this, inc);
|
|
|
|
break;
|
2006-03-04 18:16:49 +00:00
|
|
|
case 'q':
|
|
|
|
case 27:
|
2009-06-08 21:08:02 +00:00
|
|
|
case KEY_F(10):
|
2006-03-04 18:16:49 +00:00
|
|
|
looping = false;
|
|
|
|
break;
|
|
|
|
case KEY_RESIZE:
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_resize(panel, COLS, LINES-2);
|
2012-11-10 00:31:37 +00:00
|
|
|
TraceScreen_draw(this, inc);
|
2006-03-04 18:16:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
follow = false;
|
2006-05-30 14:00:18 +00:00
|
|
|
Panel_onKey(panel, ch);
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-10 00:31:37 +00:00
|
|
|
|
|
|
|
IncSet_delete(inc);
|
2012-12-05 15:12:20 +00:00
|
|
|
Vector_delete(lines);
|
2012-11-10 00:31:37 +00:00
|
|
|
|
2006-03-04 18:16:49 +00:00
|
|
|
kill(child, SIGTERM);
|
2006-05-09 17:58:40 +00:00
|
|
|
waitpid(child, NULL, 0);
|
2006-03-04 18:16:49 +00:00
|
|
|
fclose(strace);
|
2006-08-24 21:28:29 +00:00
|
|
|
CRT_enableDelay();
|
2006-03-04 18:16:49 +00:00
|
|
|
}
|