static void do_log_file(FileBuff log, const char* procfile) { char buff[1024]; int fd; do_log_uptime(log); /* append file content */ fd = open(procfile,O_RDONLY); if (fd >= 0) { close_on_exec(fd); for (;;) { int ret; ret = unix_read(fd, buff, sizeof(buff)); if (ret <= 0) break; file_buff_write(log, buff, ret); if (ret < (int)sizeof(buff)) break; } close(fd); } do_log_ln(log); }
static void do_log_file(FILE* log, const char* procfile) { do_log_uptime(log); std::string content; if (android::base::ReadFileToString(procfile, &content)) { fprintf(log, "%s\n", content.c_str()); } }
static void do_log_procs(FileBuff log) { DIR* dir = opendir("/proc"); struct dirent* entry; do_log_uptime(log); while ((entry = readdir(dir)) != NULL) { /* only match numeric values */ char* end; int pid = strtol( entry->d_name, &end, 10); if (end != NULL && end > entry->d_name && *end == 0) { char filename[32]; char buff[1024]; char cmdline[1024]; int len; int fd; /* read command line and extract program name */ snprintf(filename,sizeof(filename),"/proc/%d/cmdline",pid); proc_read(filename, cmdline, sizeof(cmdline)); /* read process stat line */ snprintf(filename,sizeof(filename),"/proc/%d/stat",pid); fd = open(filename,O_RDONLY); if (fd >= 0) { len = unix_read(fd, buff, sizeof(buff)-1); close(fd); if (len > 0) { int len2 = strlen(cmdline); if (len2 > 0) { /* we want to substitute the process name with its real name */ const char* p1; const char* p2; buff[len] = 0; p1 = strchr(buff, '('); p2 = strchr(p1, ')'); file_buff_write(log, buff, p1+1-buff); file_buff_write(log, cmdline, strlen(cmdline)); file_buff_write(log, p2, strlen(p2)); } else { /* no substitution */ file_buff_write(log,buff,len); } } } } } closedir(dir); do_log_ln(log); }
static void do_log_procs(FILE* log) { do_log_uptime(log); std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir); struct dirent* entry; while ((entry = readdir(dir.get())) != NULL) { // Only match numeric values. char* end; int pid = strtol(entry->d_name, &end, 10); if (end != NULL && end > entry->d_name && *end == 0) { char filename[32]; // /proc/<pid>/stat only has truncated task names, so get the full // name from /proc/<pid>/cmdline. snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); std::string cmdline; android::base::ReadFileToString(filename, &cmdline); const char* full_name = cmdline.c_str(); // So we stop at the first NUL. // Read process stat line. snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); std::string stat; if (android::base::ReadFileToString(filename, &stat)) { if (!cmdline.empty()) { // Substitute the process name with its real name. size_t open = stat.find('('); size_t close = stat.find_last_of(')'); if (open != std::string::npos && close != std::string::npos) { stat.replace(open + 1, close - open - 1, full_name); } } fputs(stat.c_str(), log); } } } fputc('\n', log); }