static int get_cmd_info(struct crash_info *cinfo)
{
- int fd;
- int ret;
- char cmdline[PATH_MAX];
- char cmdline_path[PATH_MAX];
- char exe_link[PATH_MAX];
-
- snprintf(cmdline_path, sizeof(cmdline_path),
- "/proc/%s/cmdline", cinfo->pid_info);
- fd = open(cmdline_path, O_RDONLY);
- if (fd < 0) {
- _E("Failed to open %s", cmdline_path);
- return -1;
- }
-
- ret = read(fd, cmdline, sizeof(cmdline) - 1);
- if (ret <= 0) {
- _E("Failed to read %s", cmdline_path);
- goto error;
- }
-
- cmdline[ret] = '\0';
-
- strncpy(cinfo->cmd_line, cmdline, sizeof(cinfo->cmd_line));
-
- snprintf(exe_link, sizeof(exe_link),
- "/proc/%s/exe", cinfo->pid_info);
-
- ret = readlink(exe_link, cinfo->cmd_path,
- sizeof(cinfo->cmd_path) - 1);
- if (ret <= 0) {
- _E("Failed to read link %s", exe_link);
- goto error;
- }
-
- cinfo->cmd_path[ret] = '\0';
-
- if (access(cinfo->cmd_path, F_OK) == -1) {
- _E("Invalid path %s", cinfo->cmd_path);
- ret = -1;
- goto error;
- }
-
-error:
- close(fd);
- return ret;
+ return get_cmd_line(cinfo->pid_info, cinfo->cmd_line, sizeof(cinfo->cmd_path)) &&
+ get_exe_path(cinfo->pid_info, cinfo->cmd_path, sizeof(cinfo->cmd_path));
}
static int set_prstatus()
# Common source code files
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
-set(CRASH_STACK_SRCS crash-stack.c crash-stack-libunw.c unwind.c proc.c mem_map.c)
+set(CRASH_STACK_SRCS crash-stack.c crash-stack-libunw.c unwind.c proc.c mem_map.c ${CMAKE_SOURCE_DIR}/src/shared/util.c)
# Add architecture dependent source files
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
#define LOG_TAG "CRASH_STACK"
#include "shared/log.h"
+#include "shared/util.h"
#define BUF_SIZE (BUFSIZ)
#define HEXA 16
*/
static void __crash_stack_print_exe(FILE* outputfile, pid_t pid)
{
- int fd, ret;
char file_path[PATH_MAX];
- char cmd_path[PATH_MAX];
+ char pid_str[11];
- snprintf(cmd_path, PATH_MAX, "/proc/%d/cmdline", pid);
- if ((fd = open(cmd_path, O_RDONLY)) < 0)
- return;
+ snprintf(pid_str, sizeof(pid_str), "%d", pid);
- if ((ret = read(fd, file_path, sizeof(file_path) - 1)) <= 0) {
- close(fd);
- return;
- }
- file_path[ret] = '\0';
+ if (get_exe_path(pid_str, file_path, sizeof(file_path)) == 0)
+ snprintf(file_path, sizeof(file_path), "<unknown>");
fprintf(outputfile, "Executable File Path: %s\n", file_path);
- close(fd);
}
/**
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
+#ifndef _GNU_SOURCE
#define _GNU_SOURCE
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
}
return -1;
}
+
+int get_cmd_line(char* pid, char *cmd, size_t len)
+{
+ char cmdline_path[PATH_MAX];
+
+ snprintf(cmdline_path, sizeof(cmdline_path),
+ "/proc/%s/cmdline", pid);
+
+ int fd = open(cmdline_path, O_RDONLY);
+ if (fd < 0) {
+ _E("Failed to open %s: %m", cmdline_path);
+ return 0;
+ }
+
+ ssize_t ret = read(fd, cmd, len - 1);
+ close(fd);
+
+ if (ret <= 0) {
+ _E("Failed to read %s: %m", cmdline_path);
+ return 0;
+ }
+
+ cmd[ret] = '\0';
+
+ return 1;
+}
+
+int get_exe_path(char* pid, char *path, size_t len)
+{
+ char exe_link[PATH_MAX];
+
+ snprintf(exe_link, sizeof(exe_link),
+ "/proc/%s/exe", pid);
+
+ ssize_t ret = readlink(exe_link, path, len - 1);
+
+ if (ret <= 0) {
+ _E("Failed to read link %s: %m", exe_link);
+ return 0;
+ }
+
+ path[ret] = '\0';
+
+ if (access(path, F_OK) == -1) {
+ _E("Invalid path %s", path);
+ return 0;
+ }
+
+ return 1;
+}
/**
* @}
*/
int log_kmsg(char *fmt, ...);
int find_crash_tid(int pid);
+
+int get_exe_path(char* pid, char *path, size_t len);
+
+int get_cmd_line(char* pid, char *cmd, size_t len);
/**
* @}
*/