bugreport-service: Add crash-info-json report type 87/256187/3
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Tue, 30 Mar 2021 16:20:56 +0000 (18:20 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 14 Apr 2021 11:56:30 +0000 (13:56 +0200)
Change-Id: I7a3a1878085e64852fe19ca44a1045de6f71150f

packaging/crash-worker.spec
src/bugreport-service/diagnostics/diagnostics_dump.c
tests/system/CMakeLists.txt
tests/system/bugreport_crash_info_json/bugreport_crash_info_json.sh.template [new file with mode: 0644]

index b50175a..fa977fa 100644 (file)
@@ -150,6 +150,7 @@ Requires:       tlm
 Requires:       /bin/bash
 Requires:       /usr/bin/unzip
 Requires:       /usr/bin/mcookie
+Requires:       /usr/bin/json-glib-validate
 Requires:       %{_sbindir}/minicoredumper
 Requires:       %{name}-system-tests-debuginfo = %{version}-%{release}
 Requires:       %{name}-support-livecoredump = %{version}-%{release}
@@ -384,6 +385,7 @@ chsmack -a "System" -t %{crash_path}
 %manifest %{name}.manifest
 %{_bindir}/crash-worker-system-tests-run
 
+%{_libexecdir}/crash-worker/system-tests/bugreport_crash_info_json/bugreport_crash_info_json.sh
 %{_libexecdir}/crash-worker/system-tests/check_minicore_mem/check_minicore_mem.sh
 %{_libexecdir}/crash-worker/system-tests/check_minicore_mem/cp.sh
 %{_libexecdir}/crash-worker/system-tests/clean_temp/clean_temp.sh
index a5aea1b..2b91ac7 100644 (file)
@@ -33,7 +33,7 @@
 #include "shared/log.h"
 #include "shared/util.h"
 
-enum BUGREPORT_REPORT_TYPE { BR_UNKNOWN, BR_BUGREPORT, BR_CRASHINFO };
+enum BUGREPORT_REPORT_TYPE { BR_UNKNOWN, BR_BUGREPORT, BR_CRASHINFO, BR_CRASHINFO_JSON };
 
 struct report_file {
        char *file_name;
@@ -213,7 +213,7 @@ out:
        return result;
 }
 
-static bool write_crash_info(int fd, long time_from, long time_to)
+static bool write_crash_info(int fd, long time_from, long time_to, bool as_json)
 {
        bool result = true;
        size_t list_count = 0;
@@ -224,15 +224,23 @@ static bool write_crash_info(int fd, long time_from, long time_to)
                return false;
        }
 
-       for (int i = 0; i < list_count; i++) {
-               int nfd = diagnostics_report_get_file(list[i].file_path, INFO_FILE);
+
+       const enum diagnostics_entry entry_type = as_json ? INFO_JSON : INFO_FILE;
+       for (int i = 0, p = 0; i < list_count; i++) {
+               int nfd = diagnostics_report_get_file(list[i].file_path, entry_type);
                if (nfd <= 0) {
                        _I("No INFO_FILE in %s file", list[i].file_path);
                        dprintf(nfd, "No INFO_FILE in %s\n",  list[i].file_path);
                        continue;
                }
 
-               if (dprintf(fd, "%sBegin crash-info <%s>\n", (i > 0) ? "\n" : "", list[i].file_path) == -1) {
+               int res = 0;
+               if (as_json)
+                       res = dprintf(fd, "%c\n", (p == 0) ? '[' : ',');
+               else
+                       res = dprintf(fd, "%sBegin crash-info <%s>\n", (i > 0) ? "\n" : "", list[i].file_path);
+
+               if (res == -1) {
                        _E("Write error: %m");
                        result = false;
                        goto out;
@@ -244,10 +252,17 @@ static bool write_crash_info(int fd, long time_from, long time_to)
                        result = false;
                        goto out;
                }
+               p++;
 
                close(nfd);
 
-               if (dprintf(fd, "End crash-info <%s>\n", list[i].file_path) == -1) {
+               if (as_json) {
+                       if (i == list_count-1)
+                               res = dprintf(fd, "]\n");
+               } else {
+                       res = dprintf(fd, "End crash-info <%s>\n", list[i].file_path);
+               }
+               if (res == -1) {
                        _E("Write error: %m");
                        result = false;
                        goto out;
@@ -278,7 +293,7 @@ static bool diagnostics_call_parse_options(int out_fd, char **params, int params
        dco->from_set = false;
        dco->to_set = false;
 
-       enum PARAM_NAME { PN_TYPE = 1, PN_LAST, PN_TO, PN_FROM};
+       enum PARAM_NAME { PN_TYPE = 1, PN_LAST, PN_TO, PN_FROM };
 
        struct option long_options[] = {
                {"type", required_argument, NULL, PN_TYPE},
@@ -301,6 +316,8 @@ static bool diagnostics_call_parse_options(int out_fd, char **params, int params
                                dco->report_type = BR_BUGREPORT;
                        } else if (strcmp(optarg, "crash-info") == 0) {
                                dco->report_type = BR_CRASHINFO;
+                       } else if (strcmp(optarg, "crash-info-json") == 0) {
+                               dco->report_type = BR_CRASHINFO_JSON;
                        } else {
                                _E("Incorrect report type: %s", optarg);
                                dprintf(out_fd, "Incorrect report type: %s\n", optarg);
@@ -350,7 +367,10 @@ static void diagnostics_callback(diagnostics_data_h data, char **params, int par
 
        switch(dco.report_type) {
        case BR_CRASHINFO:
-               write_crash_info(fd, dco.from, dco.to);
+               write_crash_info(fd, dco.from, dco.to, false);
+               break;
+       case BR_CRASHINFO_JSON:
+               write_crash_info(fd, dco.from, dco.to, true);
                break;
        case BR_BUGREPORT:
                write_bugreport(fd, dco.from, dco.to);
index 8505fed..e770894 100644 (file)
@@ -17,6 +17,7 @@ macro(CONFIGURE_TEST test_name)
   endforeach()
 endmacro()
 
+configure_test("bugreport_crash_info_json")
 configure_test("check_minicore_mem")
 configure_test("clean_temp")
 configure_test("cmp_backtraces" "cp")
diff --git a/tests/system/bugreport_crash_info_json/bugreport_crash_info_json.sh.template b/tests/system/bugreport_crash_info_json/bugreport_crash_info_json.sh.template
new file mode 100644 (file)
index 0000000..0dc7655
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+# crash-info-json format validation
+
+if [ -z "${CRASH_WORKER_SYSTEM_TESTS}" ]; then
+    CRASH_WORKER_SYSTEM_TESTS="@CRASH_SYSTEM_TESTS_PATH@"
+fi
+
+. ${CRASH_WORKER_SYSTEM_TESTS}/utils/minicore-utils.sh
+
+clean_crash_dump
+
+TMP=$(mktemp /tmp/crash-info-test.XXXXXX)
+
+cleanup()
+{
+       rm -f "${TMP}"
+}
+
+trap cleanup 0
+
+for i in {1..3}; do
+
+  ( ${CRASH_WORKER_SYSTEM_TESTS}/utils/kenny &
+    pid=$!
+    sleep 2
+    kill -6 $pid ) &
+
+  sleep 3
+  wait_for_app crash-manager
+
+  dumpsys org.tizen.bugreport-service -- --type=crash-info-json > "${TMP}"
+
+  if [ $(wc -l "${TMP}" | cut -f1 -d' ') -lt 10 ]; then
+    fail "Output file is too short"
+  fi
+
+  if ! json-glib-validate "${TMP}"; then
+    fail "crash-info-json format error"
+  fi
+
+  if ! egrep "native_hash" "${TMP}"; then
+    fail "No native_hash in output file"
+  fi
+
+done
+
+exit_ok