Skip to content

Commit 5503cda

Browse files
charlesBenBE
authored andcommitted
Fixed the issue that the process traced by strace ends and causes the CPU to be fully loaded.
1 parent 171f828 commit 5503cda

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

TraceScreen.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TraceScreen* TraceScreen_new(const Process* process) {
3939
TraceScreen* this = xCalloc(1, sizeof(TraceScreen));
4040
Object_setClass(this, Class(TraceScreen));
4141
this->tracing = true;
42+
this->strace_alive = false;
4243
FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents);
4344
CRT_disableDelay();
4445
return (TraceScreen*) InfoScreen_init(&this->super, process, fuBar, LINES - 2, " ");
@@ -121,6 +122,8 @@ bool TraceScreen_forkTracer(TraceScreen* this) {
121122

122123
this->child = child;
123124
this->strace = fp;
125+
this->strace_alive = true;
126+
124127
return true;
125128

126129
err:
@@ -131,19 +134,21 @@ bool TraceScreen_forkTracer(TraceScreen* this) {
131134

132135
static void TraceScreen_updateTrace(InfoScreen* super) {
133136
TraceScreen* this = (TraceScreen*) super;
134-
char buffer[1025];
135137

136138
int fd_strace = fileno(this->strace);
137-
assert(fd_strace != -1);
138139

139140
fd_set fds;
140141
FD_ZERO(&fds);
141-
// FD_SET(STDIN_FILENO, &fds);
142-
FD_SET(fd_strace, &fds);
142+
FD_SET(STDIN_FILENO, &fds);
143+
if (this->strace_alive) {
144+
assert(fd_strace != -1);
145+
FD_SET(fd_strace, &fds);
146+
}
143147

144148
struct timeval tv = { .tv_sec = 0, .tv_usec = 500 };
145-
int ready = select(fd_strace + 1, &fds, NULL, NULL, &tv);
149+
int ready = select(MAXIMUM(STDIN_FILENO, fd_strace) + 1, &fds, NULL, NULL, &tv);
146150

151+
char buffer[1025];
147152
size_t nread = 0;
148153
if (ready > 0 && FD_ISSET(fd_strace, &fds))
149154
nread = fread(buffer, 1, sizeof(buffer) - 1, this->strace);
@@ -171,6 +176,9 @@ static void TraceScreen_updateTrace(InfoScreen* super) {
171176
if (this->follow) {
172177
Panel_setSelected(this->super.display, Panel_size(this->super.display) - 1);
173178
}
179+
} else {
180+
if (this->strace_alive && waitpid(this->child, NULL, WNOHANG) != 0)
181+
this->strace_alive = false;
174182
}
175183
}
176184

TraceScreen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct TraceScreen_ {
2323
FILE* strace;
2424
bool contLine;
2525
bool follow;
26+
bool strace_alive;
2627
} TraceScreen;
2728

2829

0 commit comments

Comments
 (0)