2009-07-06 15:51:25 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* ede-crasher, a crash handler tool
|
|
|
|
* Part of Equinox Desktop Environment (EDE).
|
|
|
|
* Copyright (c) 2008-2009 EDE Authors.
|
|
|
|
*
|
|
|
|
* This program is licensed under terms of the
|
|
|
|
* GNU General Public License version 2 or newer.
|
|
|
|
* See COPYING for details.
|
|
|
|
*/
|
|
|
|
|
2009-07-09 16:04:19 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-07-06 15:51:25 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <edelib/Debug.h>
|
|
|
|
#include <edelib/File.h>
|
|
|
|
#include <edelib/FileTest.h>
|
2009-07-09 16:04:19 +04:00
|
|
|
#include <edelib/TempFile.h>
|
2009-07-06 15:51:25 +04:00
|
|
|
|
|
|
|
#include "GdbOutput.h"
|
|
|
|
|
|
|
|
#define CORE_FILE "core"
|
|
|
|
|
|
|
|
EDELIB_NS_USING(String)
|
2009-07-09 16:04:19 +04:00
|
|
|
EDELIB_NS_USING(TempFile)
|
2009-07-06 15:51:25 +04:00
|
|
|
EDELIB_NS_USING(file_path)
|
|
|
|
EDELIB_NS_USING(file_test)
|
|
|
|
EDELIB_NS_USING(file_remove)
|
|
|
|
EDELIB_NS_USING(FILE_TEST_IS_REGULAR)
|
|
|
|
|
|
|
|
static int write_str(int fd, const char *str) {
|
|
|
|
int len = strlen(str);
|
|
|
|
return ::write(fd, str, len);
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:20:21 +04:00
|
|
|
bool gdb_output_generate(const char *path, TempFile &t, int pid) {
|
2009-07-09 16:04:19 +04:00
|
|
|
E_RETURN_VAL_IF_FAIL(path != NULL, false);
|
2009-07-06 15:51:25 +04:00
|
|
|
|
2009-07-09 16:04:19 +04:00
|
|
|
int tfd = -1;
|
|
|
|
TempFile scr;
|
2009-07-06 15:51:25 +04:00
|
|
|
|
2009-07-09 16:04:19 +04:00
|
|
|
if(!scr.create("/tmp/.ecrash-script")) {
|
|
|
|
E_WARNING(E_STRLOC ": Unable to create temporary file for debugger script: (%i) %s",
|
|
|
|
scr.status(), strerror(scr.status()));
|
2009-07-06 15:51:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-09 16:04:19 +04:00
|
|
|
if(!t.create("/tmp/.ecrash-output")) {
|
|
|
|
E_WARNING(E_STRLOC ": Unable to create temporary file for debugger output: (%i) %s",
|
|
|
|
t.status(), strerror(t.status()));
|
2009-07-06 15:51:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-09 16:04:19 +04:00
|
|
|
tfd = t.handle();
|
2009-07-06 15:51:25 +04:00
|
|
|
|
|
|
|
/* write script */
|
2009-07-09 16:04:19 +04:00
|
|
|
::write(scr.handle(), "bt\nquit\n", 8);
|
|
|
|
scr.set_auto_delete(true);
|
|
|
|
scr.close();
|
2009-07-06 15:51:25 +04:00
|
|
|
|
|
|
|
String gdb_path = file_path("gdb");
|
|
|
|
if(gdb_path.empty()) {
|
|
|
|
/* write straight to the file, so dialog could show it */
|
2009-07-09 16:04:19 +04:00
|
|
|
write_str(tfd, "Unable to find gdb. Please install it first");
|
2009-07-06 15:51:25 +04:00
|
|
|
|
|
|
|
/* see it as valid, so dialog could be shown */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:20:21 +04:00
|
|
|
/*
|
|
|
|
* to find core file, we will try these strategies: first try to open 'core.PID' if
|
|
|
|
* we got PID (default on linux); if does not exists, try to open 'core'; everything is
|
|
|
|
* assumed current folder, whatever it was set
|
|
|
|
*/
|
|
|
|
bool core_found = false;
|
|
|
|
String core_path;
|
|
|
|
if(pid > -1) {
|
|
|
|
core_path.printf("%s.%i", CORE_FILE, pid);
|
|
|
|
if(file_test(core_path.c_str(), FILE_TEST_IS_REGULAR))
|
|
|
|
core_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!core_found) {
|
|
|
|
core_path = CORE_FILE;
|
|
|
|
if(file_test(core_path.c_str(), FILE_TEST_IS_REGULAR))
|
|
|
|
core_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!core_found) {
|
|
|
|
write_str(tfd, "Unable to find core file. Backtrace will not be done.");
|
2009-07-06 15:51:25 +04:00
|
|
|
/* see it as valid, so dialog could be shown */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:20:21 +04:00
|
|
|
pid_t gdb_pid = fork();
|
2009-07-06 15:51:25 +04:00
|
|
|
|
2012-10-23 14:20:21 +04:00
|
|
|
if(gdb_pid == -1) {
|
2009-07-06 15:51:25 +04:00
|
|
|
E_WARNING(E_STRLOC ": Unable to fork the process\n");
|
|
|
|
return false;
|
2012-10-23 14:20:21 +04:00
|
|
|
} else if(gdb_pid == 0) {
|
2009-07-09 16:04:19 +04:00
|
|
|
/* child; redirect to the file */
|
|
|
|
dup2(tfd, 1);
|
|
|
|
t.close();
|
|
|
|
|
|
|
|
::write(1, " ", 1);
|
2009-07-06 15:51:25 +04:00
|
|
|
|
|
|
|
char* argv[8];
|
|
|
|
argv[0] = (char*)gdb_path.c_str();
|
2009-11-20 20:14:40 +03:00
|
|
|
argv[1] = (char*)"--quiet";
|
|
|
|
argv[2] = (char*)"--batch";
|
|
|
|
argv[3] = (char*)"-x";
|
2009-07-09 16:04:19 +04:00
|
|
|
argv[4] = (char*)scr.name();
|
|
|
|
argv[5] = (char*)path;
|
2012-10-23 14:20:21 +04:00
|
|
|
argv[6] = (char*)core_path.c_str();
|
2009-07-06 15:51:25 +04:00
|
|
|
argv[7] = 0;
|
|
|
|
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
|
2012-10-23 14:20:21 +04:00
|
|
|
if(waitpid(gdb_pid, &status, 0) != gdb_pid) {
|
2009-07-06 15:51:25 +04:00
|
|
|
E_WARNING(E_STRLOC ": Failed to execute waitpid() properly\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:20:21 +04:00
|
|
|
file_remove(core_path.c_str());
|
2009-07-06 15:51:25 +04:00
|
|
|
return true;
|
|
|
|
}
|