Reduce effort and memory of log monitor

This commit is contained in:
topjohnwu 2017-12-18 18:17:37 +08:00
parent 5fc2058336
commit b0567eadfd
3 changed files with 120 additions and 96 deletions

View File

@ -13,9 +13,36 @@
#include "magisk.h"
#include "utils.h"
int logcat_events[] = { -1, -1, -1 };
extern int is_daemon_init;
static int am_proc_start_filter(const char *log) {
return strstr(log, "am_proc_start") != NULL;
}
static int magisk_log_filter(const char *log) {
char *ss;
return (ss = strstr(log, " Magisk")) && (ss[-1] != 'D') && (ss[-1] != 'V');
}
static int magisk_debug_log_filter(const char *log) {
return strstr(log, "Magisk") != NULL;
}
struct log_listener log_events[] = {
{ /* HIDE_EVENT */
.fd = -1,
.filter = am_proc_start_filter
},
{ /* LOG_EVENT */
.fd = -1,
.filter = magisk_log_filter
},
{ /* DEBUG_EVENT */
.fd = -1,
.filter = magisk_debug_log_filter
}
};
#ifdef MAGISK_DEBUG
static int debug_log_pid, debug_log_fd;
#endif
@ -30,10 +57,10 @@ static void *logger_thread(void *args) {
// Start logcat
log_pid = exec_command(0, &log_fd, NULL, "logcat", "-b", "all" , "-v", "threadtime", "-s", "am_proc_start", "Magisk", NULL);
while (fdgets(line, sizeof(line), log_fd)) {
for (int i = 0; i < (sizeof(logcat_events) / sizeof(int)); ++i) {
if (logcat_events[i] > 0) {
for (int i = 0; i < (sizeof(log_events) / sizeof(struct log_listener)); ++i) {
if (log_events[i].fd > 0 && log_events[i].filter(line)) {
char *s = strdup(line);
xwrite(logcat_events[i], &s, sizeof(s));
xwrite(log_events[i].fd, &s, sizeof(s));
}
}
if (kill(log_pid, 0))
@ -66,14 +93,12 @@ static void *magisk_log_thread(void *args) {
return NULL;
// Register our listener
logcat_events[LOG_EVENT] = pipefd[1];
log_events[LOG_EVENT].fd = pipefd[1];
LOGD("log_monitor: magisk log dumper start");
FILE *log;
char *ss;
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line)) {
if ((ss = strstr(line, " Magisk")) && (ss[-1] != 'D') && (ss[-1] != 'V')) {
if (!have_data) {
if ((have_data = check_data())) {
// Dump buffered logs to file
@ -92,7 +117,6 @@ static void *magisk_log_thread(void *args) {
if (have_data)
fprintf(log, "%s", line);
}
}
return NULL;
}
@ -106,13 +130,11 @@ static void *debug_magisk_log_thread(void *args) {
LOGD("log_monitor: debug log dumper start");
// Register our listener
logcat_events[DEBUG_EVENT] = pipefd[1];
log_events[DEBUG_EVENT].fd = pipefd[1];
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line)) {
char *ss;
if ((ss = strstr(line, "Magisk")))
for (char *line; xxread(pipefd[0], &line, sizeof(line)) > 0; free(line))
fprintf(log, "%s", line);
}
return NULL;
}

View File

@ -51,7 +51,13 @@ enum {
LOG_EVENT,
DEBUG_EVENT
};
extern int logcat_events[];
struct log_listener {
int fd;
int (*filter) (const char*);
};
extern struct log_listener log_events[];
void monitor_logs();
void start_debug_full_log();

View File

@ -28,7 +28,7 @@ static void term_thread(int sig) {
destroy_list();
hideEnabled = 0;
// Unregister listener
logcat_events[HIDE_EVENT] = -1;
log_events[HIDE_EVENT].fd = -1;
close(pipefd[0]);
close(pipefd[1]);
pipefd[0] = pipefd[1] = -1;
@ -193,16 +193,15 @@ void proc_monitor() {
// Register our listener to logcat monitor
xpipe2(pipefd, O_CLOEXEC);
logcat_events[HIDE_EVENT] = pipefd[1];
log_events[HIDE_EVENT].fd = pipefd[1];
for (char *log, *line; 1; free(log)) {
for (char *log, *line;; free(log)) {
if (read(pipefd[0], &log, sizeof(log)) != sizeof(log)) {
/* It might be interrupted */
log = NULL;
continue;
}
char *ss;
if ((ss = strstr(log, "am_proc_start")) && (ss = strchr(ss, '['))) {
char *ss = strchr(log, '[');
int pid, ret, comma = 0;
char *pos = ss, processName[256], ns[32];
@ -222,8 +221,6 @@ void proc_monitor() {
if(ret != 2)
continue;
ret = 0;
// Critical region
pthread_mutex_lock(&hide_lock);
vec_for_each(hide_list, line) {
@ -268,4 +265,3 @@ void proc_monitor() {
pthread_mutex_unlock(&hide_lock);
}
}
}