From ee920752cb9326e365dad65fb55c1534817824e1 Mon Sep 17 00:00:00 2001 From: zhjc1124 Date: Thu, 19 Oct 2023 07:39:28 +0000 Subject: [PATCH] [CP] fix file read error --- src/observer/main.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/observer/main.cpp b/src/observer/main.cpp index 8b80359a22..2800a03ff7 100644 --- a/src/observer/main.cpp +++ b/src/observer/main.cpp @@ -408,22 +408,23 @@ static void print_all_thread(const char* desc) struct dirent *entry; while ((entry = readdir(dir)) != NULL) { char *tid = entry->d_name; - if (tid[0] == '.') - continue; // pass . and .. - char path[256]; - sprintf(path, "/proc/self/task/%s/comm", tid); - FILE *file = fopen(path, "r"); - if (file == NULL) { - MPRINT("fail to print thread tid: %s", tid); + if (tid[0] != '.') { // pass . and .. + char path[256]; + sprintf(path, "/proc/self/task/%s/comm", tid); + FILE *file = fopen(path, "r"); + if (file == NULL) { + MPRINT("fail to print thread tid: %s", tid); + } else { + char name[256]; + fgets(name, 256, file); + size_t len = strlen(name); + if (len > 0 && name[len - 1] == '\n') { + name[len - 1] = '\0'; + } + MPRINT("[%s] detect unstopped thread, tid: %s, name: %s", desc, tid, name); + fclose(file); + } } - char name[256]; - fgets(name, 256, file); - size_t len = strlen(name); - if (len > 0 && name[len - 1] == '\n') { - name[len - 1] = '\0'; - } - MPRINT("[%s] detect unstopped thread, tid: %s, name: %s", desc, tid, name); - fclose(file); } } closedir(dir);