1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#include "Unix.h"
#include <cstring>
#include <signal.h>
namespace DebuggerCorePlugin {
namespace {
struct Exception {
qlonglong value;
const char *const name;
};
constexpr Exception Exceptions[] = {
#ifdef SIGABRT
{SIGABRT, "SIGABRT"},
#endif
#ifdef SIGALRM
{SIGALRM, "SIGALRM"},
#endif
#ifdef SIGVTALRM
{SIGVTALRM, "SIGVTALRM"},
#endif
#ifdef SIGPROF
{SIGPROF, "SIGPROF"},
#endif
#ifdef SIGBUS
{SIGBUS, "SIGBUS"},
#endif
#ifdef SIGCHLD
{SIGCHLD, "SIGCHLD"},
#endif
#ifdef SIGCONT
{SIGCONT, "SIGCONT"},
#endif
#ifdef SIGFPE
{SIGFPE, "SIGFPE"},
#endif
#ifdef SIGHUP
{SIGHUP, "SIGHUP"},
#endif
#ifdef SIGILL
{SIGILL, "SIGILL"},
#endif
#ifdef SIGINT
{SIGINT, "SIGINT"},
#endif
#ifdef SIGKILL
{SIGKILL, "SIGKILL"},
#endif
#ifdef SIGPIPE
{SIGPIPE, "SIGPIPE"},
#endif
#ifdef SIGQUIT
{SIGQUIT, "SIGQUIT"},
#endif
#ifdef SIGSEGV
{SIGSEGV, "SIGSEGV"},
#endif
#ifdef SIGSTOP
{SIGSTOP, "SIGSTOP"},
#endif
#ifdef SIGTERM
{SIGTERM, "SIGTERM"},
#endif
#ifdef SIGTSTP
{SIGTSTP, "SIGTSTP"},
#endif
#ifdef SIGTTIN
{SIGTTIN, "SIGTTIN"},
#endif
#ifdef SIGTTOU
{SIGTTOU, "SIGTTOU"},
#endif
#ifdef SIGUSR1
{SIGUSR1, "SIGUSR1"},
#endif
#ifdef SIGUSR2
{SIGUSR2, "SIGUSR2"},
#endif
#ifdef SIGPOLL
{SIGPOLL, "SIGPOLL"},
#endif
#ifdef SIGSYS
{SIGSYS, "SIGSYS"},
#endif
#ifdef SIGTRAP
{SIGTRAP, "SIGTRAP"},
#endif
#ifdef SIGURG
{SIGURG, "SIGURG"},
#endif
#ifdef SIGXCPU
{SIGXCPU, "SIGXCPU"},
#endif
#ifdef SIGXFSZ
{SIGXFSZ, "SIGXFSZ"},
#endif
#ifdef SIGIO
{SIGIO, "SIGIO"},
#endif
#ifdef SIGSTKFLT
{SIGSTKFLT, "SIGSTKFLT"},
#endif
#ifdef SIGWINCH
{SIGWINCH, "SIGWINCH"},
#endif
};
/**
* @brief copyString
* @param str
* @return
*/
char *copyString(const QByteArray &str) {
char *p = new char[str.length() + 1];
std::strcpy(p, str.constData());
return p;
}
}
/**
* @brief Unix::exceptions
* @return
*/
QMap<qlonglong, QString> Unix::exceptions() {
QMap<qlonglong, QString> exceptions;
for (Exception e : Exceptions) {
exceptions.insert(e.value, e.name);
}
return exceptions;
}
/**
* @brief Unix::exception_name
* @param value
* @return
*/
QString Unix::exception_name(qlonglong value) {
auto it = std::find_if(std::begin(Exceptions), std::end(Exceptions), [value](const Exception &ex) {
return ex.value == value;
});
if (it != std::end(Exceptions)) {
return it->name;
}
return QString();
}
/**
* @brief Unix::exception_value
* @param name
* @return
*/
qlonglong Unix::exception_value(const QString &name) {
auto it = std::find_if(std::begin(Exceptions), std::end(Exceptions), [&name](const Exception &ex) {
return ex.name == name;
});
if (it != std::end(Exceptions)) {
return it->value;
}
return -1;
}
/**
* @brief Unix::execute_process
* @param path
* @param cwd
* @param args
* @return
*/
Status Unix::execute_process(const QString &path, const QString &cwd, const QList<QByteArray> &args) {
QString errorString = "internal error";
// change to the desired working directory
if (::chdir(qPrintable(cwd)) == 0) {
// allocate space for all the arguments
auto argv_pointers = new char *[args.count() + 2];
char **p = argv_pointers;
*p++ = copyString(path.toLocal8Bit());
for (int i = 0; i < args.count(); ++i) {
*p++ = copyString(args[i]);
}
*p = nullptr;
// NOTE: it's a bad idea to use execvp and similar functions searching in
// $PATH. At least on Linux, if the file is corrupted/unsupported, they
// instead appear to launch shell
const int ret = execv(argv_pointers[0], argv_pointers);
// should be no need to cleanup, the process which allocated all that
// space no longer exists!
// if we get here...execv failed!
if (ret == -1) {
errorString = QString("execv() failed: %1").arg(strerror(errno));
p = argv_pointers;
while (*p) {
delete[] * p++;
}
delete[] argv_pointers;
}
}
// frankly, any return is technically an error I think
// this is only executed from a fork
return Status(errorString);
}
}
|