Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit de591df3 authored by Naveen Kalla's avatar Naveen Kalla
Browse files

DO NOT MERGE. Add modem logs to the bugreport

When user takes bug report, collect modem logs and add it to the
bugreport zip file.

Test: Take Bugreport and ensure that the modem logs are present
in userdebug builds. Also check that bug reports are collected
normally if there are no modem logs.

BUG=32219165

Change-Id: Ia20fc95ceee95972e17ac4193d350bbae36b3630
parent 4466e11f
Loading
Loading
Loading
Loading
+80 −1
Original line number Original line Diff line number Diff line
@@ -34,8 +34,9 @@
#include <sys/wait.h>
#include <sys/wait.h>
#include <unistd.h>
#include <unistd.h>


#include <android-base/stringprintf.h>
#include <android-base/file.h>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/properties.h>
#include <cutils/properties.h>


#include "private/android_filesystem_config.h"
#include "private/android_filesystem_config.h"
@@ -385,6 +386,80 @@ static void dump_raft() {
    }
    }
}
}


/**
 * Finds the last modified file in the directory dir whose name starts with file_prefix
 * Function returns empty string when it does not find a file
 */
static std::string get_last_modified_file_matching_prefix(const std::string& dir,
                                                          const std::string& file_prefix) {
    std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dir.c_str()), closedir);
    if (d == nullptr) {
        MYLOGD("Error %d opening %s\n", errno, dir.c_str());
        return "";
    }

    // Find the newest file matching the file_prefix in dir
    struct dirent *de;
    time_t last_modified = 0;
    std::string last_modified_file = "";
    struct stat s;

    while ((de = readdir(d.get()))) {
        std::string file = std::string(de->d_name);
        if (!file_prefix.empty()) {
            if (!android::base::StartsWith(file, file_prefix.c_str())) continue;
        }
        file = dir + "/" + file;
        int ret = stat(file.c_str(), &s);

        if ((ret == 0) && (s.st_mtime > last_modified)) {
            last_modified_file = file;
            last_modified = s.st_mtime;
        }
    }

    return last_modified_file;
}

void dump_modem_logs() {
    DurationReporter duration_reporter("dump_modem_logs");
    if (is_user_build()) {
        return;
    }

    if (!is_zipping()) {
        MYLOGD("Not dumping modem logs. dumpstate is not generating a zipping bugreport\n");
        return;
    }

    char property[PROPERTY_VALUE_MAX];
    property_get("ro.radio.log_prefix", property, "");
    std::string file_prefix = std::string(property);
    if(file_prefix.empty()) {
        MYLOGD("No modem log : file_prefix is empty\n");
        return;
    }

    MYLOGD("dump_modem_logs: directory is %s and file_prefix is %s\n",
           bugreport_dir.c_str(), file_prefix.c_str());

    std::string modem_log_file =
        get_last_modified_file_matching_prefix(bugreport_dir, file_prefix);

    struct stat s;
    if (modem_log_file.empty() || stat(modem_log_file.c_str(), &s) != 0) {
        MYLOGD("Modem log %s does not exist\n", modem_log_file.c_str());
        return;
    }

    std::string filename = basename(modem_log_file.c_str());
    if (!add_zip_entry(filename, modem_log_file)) {
        MYLOGE("Unable to add modem log %s to zip file\n", modem_log_file.c_str());
    } else {
        MYLOGD("Modem Log %s is added to zip\n", modem_log_file.c_str());
    }
}

static bool skip_not_stat(const char *path) {
static bool skip_not_stat(const char *path) {
    static const char stat[] = "/stat";
    static const char stat[] = "/stat";
    size_t len = strlen(path);
    size_t len = strlen(path);
@@ -1119,6 +1194,10 @@ static void dumpstate(const std::string& screenshot_path, const std::string& ver


    run_command("APP PROVIDERS", 30, "dumpsys", "-t", "30", "activity", "provider", "all", NULL);
    run_command("APP PROVIDERS", 30, "dumpsys", "-t", "30", "activity", "provider", "all", NULL);


    // dump_modem_logs adds the modem logs if available to the bugreport.
    // Do this at the end to allow for sufficient time for the modem logs to be
    // collected.
    dump_modem_logs();


    printf("========================================================\n");
    printf("========================================================\n");
    printf("== Final progress (pid %d): %d/%d (originally %d)\n",
    printf("== Final progress (pid %d): %d/%d (originally %d)\n",