log_dump: Dump current system states 92/95692/4
authorSunmin Lee <sunm.lee@samsung.com>
Fri, 4 Nov 2016 08:05:52 +0000 (17:05 +0900)
committerSunmin Lee <sunm.lee@samsung.com>
Mon, 7 Nov 2016 08:55:57 +0000 (17:55 +0900)
log_dump is command line tool which is used to dump current system states.
The system states include from dump_systemstate's output
to application/module/system logs.
Further logs can be dumped by the scripts that would be installed
into dump_scripts directory.

Change-Id: Icb60ba85afc8b3d4fbec99e822ad20b8c8a2d36d
Signed-off-by: Sunmin Lee <sunm.lee@samsung.com>
CMakeLists.txt
dump_scripts/app_log.sh [new file with mode: 0755]
dump_scripts/module_log.sh [new file with mode: 0755]
dump_scripts/system_log.sh [new file with mode: 0755]
packaging/crash-worker.spec
src/dump_systemstate/CMakeLists.txt
src/log_dump/CMakeLists.txt [new file with mode: 0644]
src/log_dump/log_dump.c [new file with mode: 0644]
src/sys-assert/CMakeLists.txt

index 2f65b81..4eaaec1 100644 (file)
@@ -14,4 +14,4 @@ ADD_SUBDIRECTORY(src/crash-pipe)
 ADD_SUBDIRECTORY(src/crash-stack)
 
 ADD_SUBDIRECTORY(src/dump_systemstate)
-
+ADD_SUBDIRECTORY(src/log_dump)
diff --git a/dump_scripts/app_log.sh b/dump_scripts/app_log.sh
new file mode 100755 (executable)
index 0000000..bd2dd35
--- /dev/null
@@ -0,0 +1,13 @@
+#!/bin/sh
+#
+# Dump application log
+#
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+DUMP_DEST=$1/app_log
+
+. /etc/tizen-platform.conf
+
+mkdir -p ${DUMP_DEST}
+
+/bin/mv ${TZ_SYS_CRASH}/* ${DUMP_DEST}
diff --git a/dump_scripts/module_log.sh b/dump_scripts/module_log.sh
new file mode 100755 (executable)
index 0000000..8e1a187
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/sh
+#
+# Dump module log
+#
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+DUMP_DEST=$1/module_log
+DUMP_SCRIPT_DIR=/opt/etc/dump.d/module.d
+
+mkdir -p ${DUMP_DEST}
+
+if [ -d ${DUMP_SCRIPT_DIR} ]
+then
+       SCRIPTS=`/bin/ls ${DUMP_SCRIPT_DIR}`
+
+       for SCRIPT in ${SCRIPTS}; do
+               /bin/sh ${DUMP_SCRIPT_DIR}/${SCRIPT} ${DUMP_DEST}
+       done
+fi
diff --git a/dump_scripts/system_log.sh b/dump_scripts/system_log.sh
new file mode 100755 (executable)
index 0000000..6d57f64
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/sh
+#
+# Dump system log
+#
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+DUMP_DEST=$1/system_log
+
+mkdir -p ${DUMP_DEST}
+
+/bin/cp -fr /opt/var/log/* ${DUMP_DEST}
+/bin/cp -fr /run/systemd/journal ${DUMP_DEST}
index 3b98404..4e12337 100644 (file)
@@ -10,6 +10,7 @@ Source0:    %{name}-%{version}.tar.gz
 Source1001:    crash-worker.manifest
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(libtzplatform-config)
+BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  cmake
 %if "%{?sys_assert}" == "on"
 BuildRequires:  pkgconfig(libunwind)
@@ -37,6 +38,10 @@ crash-manager
 %define crash_path      %{TZ_SYS_CRASH}
 %define crash_temp      %{crash_root_path}/temp
 
+#Path for log_dump module
+%define crash_all_log   %{TZ_SYS_ALLLOGS}
+%define crash_dump_gen  %{TZ_SYS_DUMPGEN}
+
 %define upgrade_script_path %{TZ_SYS_RO_SHARE}/upgrade/scripts
 
 %build
@@ -82,7 +87,10 @@ mkdir -p %{buildroot}%{crash_root_path}
 mkdir -p %{buildroot}%{crash_path}
 mkdir -p %{buildroot}%{crash_temp}
 
-
+# log_dump dir
+mkdir -p %{buildroot}%{crash_all_log}
+mkdir -p %{buildroot}%{crash_dump_gen}
+cp dump_scripts/* %{buildroot}%{crash_dump_gen}
 
 %if "%{?sys_assert}" == "on"
 
@@ -119,8 +127,11 @@ sed -i "/${pattern}/D" %{_sysconfdir}/ld.so.preload
 %dir %{crash_root_path}
 %dir %{crash_path}
 %dir %{crash_temp}
+%dir %{crash_all_log}
+%{crash_dump_gen}/*
 %attr(0755,system,system) %{_bindir}/dump_systemstate
 %attr(0755,system,system) %{_bindir}/crash-manager.sh
+%attr(0755,system,system) %{_bindir}/log_dump
 %{_prefix}/lib/sysctl.d/99-crash-manager.conf
 
 %if "%{?sys_assert}" == "on"
index bd00d95..85585ce 100755 (executable)
@@ -8,9 +8,9 @@ SET(SRCS
    )
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED dlog libunwind)
+pkg_check_modules(dump_systemstate_pkgs REQUIRED dlog libunwind)
 
-FOREACH(flag ${pkgs_CFLAGS})
+FOREACH(flag ${dump_systemstate_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
@@ -20,7 +20,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 MESSAGE("FLAGS: ${CMAKE_C_FLAGS}")
 
 ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
-TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS})
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${dump_systemstate_pkgs_LDFLAGS})
 
 INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin
                PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
diff --git a/src/log_dump/CMakeLists.txt b/src/log_dump/CMakeLists.txt
new file mode 100644 (file)
index 0000000..8920559
--- /dev/null
@@ -0,0 +1,28 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(log_dump C)
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
+SET(LOG_DUMP_SRCS
+       log_dump.c
+       ${CMAKE_SOURCE_DIR}/src/shared/util.c
+   )
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(log_dump_pkgs REQUIRED
+       dlog
+       capi-system-info
+       libtzplatform-config
+       )
+
+FOREACH(flag ${log_dump_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+ADD_EXECUTABLE(${PROJECT_NAME} ${LOG_DUMP_SRCS})
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${log_dump_pkgs_LDFLAGS})
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin
+               PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
+               GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
diff --git a/src/log_dump/log_dump.c b/src/log_dump/log_dump.c
new file mode 100644 (file)
index 0000000..38180f5
--- /dev/null
@@ -0,0 +1,198 @@
+/* log_dump: dump current system states
+ *
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <limits.h>
+#include <time.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <dlog.h>
+#include <system_info.h>
+#include <tzplatform_config.h>
+#include "shared/util.h"
+
+#undef LOG_TAG
+#define LOG_TAG          "LOG_DUMP"
+#define LOG_DUMP_ROOT    tzplatform_getenv(TZ_SYS_ALLLOGS)
+#define LOG_DUMP_DIR     tzplatform_mkpath(TZ_SYS_ALLLOGS, "dump")
+#define DUMP_SCRIPTS_DIR tzplatform_getenv(TZ_SYS_DUMPGEN)
+#define SYSTEM_INFO_KEY_BUILD_STRING "http://tizen.org/system/build.string"
+
+enum {
+       OPT_NORMAL,
+       OPT_SHORT,
+};
+
+static const struct option opts[] = {
+       { "normal", no_argument, 0, OPT_NORMAL },
+       { "short",  no_argument, 0, OPT_SHORT  },
+       { 0, 0, 0, 0 }
+};
+
+static inline void usage(void)
+{
+       printf("Usage: log_dump [OPTION]\n");
+       printf("Dump options:\n");
+       printf("  %-10s %s (%s)\n", "--normal",
+                       "dump all logs", DUMP_SCRIPTS_DIR);
+       printf("  %-10s %s\n", "--short",
+                       "dump systemstate only");
+}
+
+static int dump_scripts(const char *path)
+{
+       struct dirent **dir_list = NULL;
+       char command[PATH_MAX];
+       int script_num, i;
+
+       script_num = scandir(path, &dir_list, NULL, NULL);
+       if (script_num < 0) {
+               LOGE("Failed to scandir");
+               return -1;
+       }
+
+       for (i = 0; i < script_num; i++) {
+               if (dir_list[i]->d_type != DT_REG)
+                       continue;
+
+               snprintf(command, sizeof(command), "%s/%s %s",
+                               path, dir_list[i]->d_name,
+                               LOG_DUMP_DIR);
+               LOGD("%s", command);
+               system_command(command);
+       }
+
+       for (i = 0; i < script_num; i++)
+               free(dir_list[i]);
+       free(dir_list);
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int c, ret;
+       int opt_normal, opt_short;
+       char *version_str = NULL;
+       char timestr[80];
+       char command[PATH_MAX];
+       char dump_filename[NAME_MAX];
+       time_t cur_time;
+       struct tm loc_tm;
+
+       if (argc < 2) {
+               usage();
+               exit(EXIT_SUCCESS);
+       }
+
+       opt_normal = opt_short = 0;
+       while ((c = getopt_long_only(argc, argv, "", opts, NULL)) != -1) {
+               switch (c) {
+               case OPT_NORMAL:
+                       opt_normal = 1;
+                       break;
+               case OPT_SHORT:
+                       opt_short = 1;
+                       break;
+               default:
+                       usage();
+                       exit(EXIT_SUCCESS);
+                       break;
+               }
+       }
+
+       if (opt_normal & opt_short) {
+               usage();
+               exit(EXIT_SUCCESS);
+       }
+
+       /* Make debug directory */
+       ret = snprintf(command, sizeof(command),
+                       "/usr/bin/mkdir -p %s", LOG_DUMP_DIR);
+       if (ret < 0) {
+               LOGE("Failed to mkdir");
+               exit(EXIT_FAILURE);
+       }
+       system_command(command);
+
+       /* Get timestamp */
+       cur_time = time(NULL);
+       localtime_r(&cur_time, &loc_tm);
+       strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", &loc_tm);
+
+       /* Get version */
+       ret = system_info_get_platform_string(SYSTEM_INFO_KEY_BUILD_STRING,
+                       &version_str);
+       if (ret != SYSTEM_INFO_ERROR_NONE) {
+               LOGE("Failed to system_info_get_platform_string");
+               version_str = NULL;
+       }
+
+       /* Dump system states */
+       ret = snprintf(command, sizeof(command),
+                       "/usr/bin/dump_systemstate -k -d -f "
+                       "%s/dump_systemstate_%s.log", LOG_DUMP_DIR, timestr);
+       if (ret < 0) {
+               LOGE("Failed to snprintf for command");
+               exit(EXIT_FAILURE);
+       }
+       system_command(command);
+
+       /* Dump all logs */
+       if (opt_normal)
+               dump_scripts(DUMP_SCRIPTS_DIR);
+
+       if (version_str) {
+               ret = snprintf(dump_filename, sizeof(dump_filename), "%s_%s",
+                               "log_dump", version_str);
+               if (ret < 0) {
+                       LOGE("Failed to snprintf for dump path");
+                       exit(EXIT_FAILURE);
+               }
+       } else {
+               ret = snprintf(dump_filename, sizeof(dump_filename), "%s",
+                               "log_dump");
+               if (ret < 0) {
+                       LOGE("Failed to snprintf for dump path");
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       /* Compression */
+       ret = snprintf(command, sizeof(command),
+                       "/bin/tar -zcf %s/%s%s.tar.gz -C %s dump",
+                       LOG_DUMP_ROOT, dump_filename, timestr, LOG_DUMP_ROOT);
+       if (ret < 0) {
+               LOGE("Failed to snprintf for command");
+               exit(EXIT_FAILURE);
+       }
+       system_command(command);
+
+       sync();
+
+       if (remove_dir(LOG_DUMP_DIR, 0) < 0) {
+               ret = -1;
+               LOGE("Failed to delete dump directory");
+       }
+
+       if (version_str)
+               free(version_str);
+
+       return ret;
+}
index 9656d52..99d3383 100644 (file)
@@ -38,9 +38,9 @@ ENDIF("${ARCH_BIT}" STREQUAL "64")
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(ppkgs REQUIRED libtzplatform-config libunwind)
+pkg_check_modules(sys-assert_pkgs REQUIRED libtzplatform-config libunwind)
 
-FOREACH(flag ${ppkgs_CFLAGS})
+FOREACH(flag ${sys-assert_pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
@@ -56,7 +56,7 @@ IF("${ARCH}" STREQUAL "arm")
 ENDIF("${ARCH}" STREQUAL "arm")
 
 ADD_LIBRARY(${LIBNAME} SHARED ${SRCS})
-TARGET_LINK_LIBRARIES(${LIBNAME} ${ppkgs_LDFLAGS} -ldl)
+TARGET_LINK_LIBRARIES(${LIBNAME} ${sys-assert_pkgs_LDFLAGS} -ldl)
 
 INSTALL(TARGETS ${LIBNAME} LIBRARY DESTINATION ${LIB_INSTALL_DIR})