sm-ext-accelerator-2023/test/test.cpp

239 lines
6.4 KiB
C++
Raw Normal View History

2018-07-19 18:33:19 +02:00
#if defined _LINUX
#include "client/linux/handler/exception_handler.h"
#include "common/linux/linux_libc_support.h"
#include "third_party/lss/linux_syscall_support.h"
2018-07-20 02:17:53 +02:00
#include "common/linux/dump_symbols.h"
#include "common/path_helper.h"
#include <sstream>
2018-07-19 18:33:19 +02:00
#include <signal.h>
#include <dirent.h>
#include <unistd.h>
2018-07-20 02:17:53 +02:00
#include <paths.h>
2018-07-19 18:33:19 +02:00
#define my_jmp_buf sigjmp_buf
#define my_setjmp(x) sigsetjmp(x, 0)
#define my_longjmp siglongjmp
using google_breakpad::MinidumpDescriptor;
2018-07-20 02:17:53 +02:00
using google_breakpad::WriteSymbolFile;
2018-07-19 18:33:19 +02:00
#elif defined _WINDOWS
#define _STDINT // ~.~
#include "client/windows/handler/exception_handler.h"
#define my_jmp_buf jmp_buf
#define my_setjmp(x) setjmp(x)
#define my_longjmp longjmp
#else
#error Bad platform.
#endif
2017-08-30 20:47:05 +02:00
#include <google_breakpad/processor/minidump.h>
#include <google_breakpad/processor/minidump_processor.h>
#include <google_breakpad/processor/process_state.h>
#include <google_breakpad/processor/call_stack.h>
#include <google_breakpad/processor/stack_frame.h>
#include <processor/pathname_stripper.h>
#include <streambuf>
2018-07-19 18:33:19 +02:00
#include <setjmp.h>
2017-08-30 20:47:05 +02:00
using google_breakpad::ExceptionHandler;
using google_breakpad::Minidump;
using google_breakpad::MinidumpProcessor;
using google_breakpad::ProcessState;
using google_breakpad::ProcessResult;
using google_breakpad::CallStack;
using google_breakpad::StackFrame;
using google_breakpad::PathnameStripper;
2018-07-19 14:55:28 +02:00
using google_breakpad::CodeModule;
2017-08-30 20:47:05 +02:00
2018-07-19 18:33:19 +02:00
my_jmp_buf envbuf;
2017-08-30 20:47:05 +02:00
char path[1024];
2018-07-19 18:33:19 +02:00
#if defined _LINUX
2017-08-30 20:47:05 +02:00
static bool dumpCallback(const MinidumpDescriptor &descriptor, void *context, bool succeeded)
{
if (succeeded) {
sys_write(STDOUT_FILENO, "Wrote minidump to: ", 19);
} else {
sys_write(STDOUT_FILENO, "Failed to write minidump to: ", 29);
}
sys_write(STDOUT_FILENO, descriptor.path(), my_strlen(descriptor.path()));
sys_write(STDOUT_FILENO, "\n", 1);
2017-08-30 20:47:05 +02:00
my_strlcpy(path, descriptor.path(), sizeof(path));
2018-07-19 18:33:19 +02:00
my_longjmp(envbuf, 1);
return succeeded;
}
#elif defined _WINDOWS
static bool dumpCallback(const wchar_t *dump_path, const wchar_t *minidump_id, void *context, EXCEPTION_POINTERS *exinfo, MDRawAssertionInfo *assertion, bool succeeded)
{
if (succeeded) {
printf("Wrote minidump to: %ls\\%ls.dmp\n", dump_path, minidump_id);
} else {
printf("Failed to write minidump to: %ls\\%ls.dmp\n", dump_path, minidump_id);
}
// TODO: setjmp/longjmp doesn't play nicely with SEH on Windows, so we never get back.
// But the exception handler is called and writes the dump, so the user can just invoke us again.
// snprintf(path, sizeof(path), "%ls\\%ls.dmp", dump_path, minidump_id);
// my_longjmp(envbuf, 1);
2017-08-30 20:47:05 +02:00
return succeeded;
}
2018-07-19 18:33:19 +02:00
#endif
int main(int argc, char *argv[])
{
2017-08-30 20:47:05 +02:00
bool generateCrash = false;
if (argc <= 1) {
generateCrash = true;
2018-07-19 18:33:19 +02:00
if (my_setjmp(envbuf) == 0) {
#if defined _LINUX
2017-08-30 20:47:05 +02:00
MinidumpDescriptor descriptor(".");
ExceptionHandler *handler = new ExceptionHandler(descriptor, NULL, dumpCallback, NULL, true, -1);
2018-07-19 18:33:19 +02:00
#elif defined _WINDOWS
ExceptionHandler *handler = new ExceptionHandler(L".", NULL, dumpCallback, NULL, ExceptionHandler::HANDLER_ALL);
#endif
2017-08-30 20:47:05 +02:00
2018-07-19 18:33:19 +02:00
volatile int *ptr = (volatile int *)(0xdeadbeef);
*ptr = 0;
2017-08-30 20:47:05 +02:00
delete handler;
return 0;
}
printf("Returned from signal handler, path: %s\n", path);
argc = 2;
}
for (int i = 1; i < argc; ++i) {
if (!generateCrash) {
2018-07-19 18:33:19 +02:00
strncpy(path, argv[i], sizeof(path));
2017-08-30 20:47:05 +02:00
}
MinidumpProcessor minidumpProcessor(nullptr, nullptr);
2017-08-30 20:47:05 +02:00
std::streambuf *old = std::clog.rdbuf();
std::clog.rdbuf(nullptr);
ProcessState processState;
ProcessResult processResult = minidumpProcessor.Process(path, &processState);
std::clog.rdbuf(old);
if (processResult != google_breakpad::PROCESS_OK) {
continue;
}
int requestingThread = processState.requesting_thread();
if (requestingThread == -1) {
requestingThread = 0;
}
const CallStack *stack = processState.threads()->at(requestingThread);
if (!stack) {
fprintf(stderr, "Minidump missing stack!\n");
continue;
}
int frameCount = stack->frames()->size();
if (frameCount > 10) {
frameCount = 10;
}
2018-07-19 18:33:19 +02:00
printf("1|%d|%s|%x|%d", processState.crashed(), processState.crash_reason().c_str(), (intptr_t)processState.crash_address(), requestingThread);
2018-07-19 14:55:28 +02:00
std::map<const CodeModule *, unsigned int> moduleMap;
2017-08-30 20:47:05 +02:00
2018-07-19 14:55:28 +02:00
unsigned int moduleCount = processState.modules()->module_count();
for (unsigned int moduleIndex = 0; moduleIndex < moduleCount; ++moduleIndex) {
auto module = processState.modules()->GetModuleAtIndex(moduleIndex);
moduleMap[module] = moduleIndex;
auto debugFile = PathnameStripper::File(module->debug_file());
auto debugIdentifier = module->debug_identifier();
2018-07-19 18:33:19 +02:00
printf("|M|%s|%s", debugFile.c_str(), debugIdentifier.c_str());
2018-07-19 14:55:28 +02:00
}
2017-08-30 20:47:05 +02:00
for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
const StackFrame *frame = stack->frames()->at(frameIndex);
if (frame->module) {
2018-07-19 14:55:28 +02:00
auto moduleIndex = moduleMap[frame->module];
2017-08-30 20:47:05 +02:00
auto moduleOffset = frame->ReturnAddress() - frame->module->base_address();
2018-07-19 18:33:19 +02:00
printf("|F|%d|%x", moduleIndex, (intptr_t)moduleOffset);
2017-08-30 20:47:05 +02:00
} else {
2018-07-19 18:33:19 +02:00
printf("|F|%d|%x", -1, (intptr_t)frame->ReturnAddress());
2017-08-30 20:47:05 +02:00
}
}
printf("\n");
2018-07-20 02:17:53 +02:00
#if defined _LINUX
for (unsigned int moduleIndex = 0; moduleIndex < moduleCount; ++moduleIndex) {
auto module = processState.modules()->GetModuleAtIndex(moduleIndex);
auto debugFile = module->debug_file();
if (debugFile[0] != '/') {
continue;
}
printf("%s\n", debugFile.c_str());
auto debugFileDir = google_breakpad::DirName(debugFile);
std::vector<string> debug_dirs{
debugFileDir,
};
FILE *saved_stderr = fdopen(dup(fileno(stderr)), "w");
if (freopen(_PATH_DEVNULL, "w", stderr)) {
// If it fails, not a lot we can (or should) do.
// Add this brace section to silence gcc warnings.
}
std::ostringstream outputStream;
google_breakpad::DumpOptions options(ALL_SYMBOL_DATA, true);
if (!WriteSymbolFile(debugFile, debug_dirs, options, outputStream)) {
outputStream.str("");
outputStream.clear();
// Try again without debug dirs.
if (!WriteSymbolFile(debugFile, {}, options, outputStream)) {
// TODO: Something.
}
}
fflush(stderr);
dup2(fileno(saved_stderr), fileno(stderr));
fclose(saved_stderr);
// WriteSymbolFileHeaderOnly would do this for us, but this is just for testing.
auto output = outputStream.str();
output = output.substr(0, output.find("\n"));
fprintf(stdout, "%s\n", output.c_str());
// break;
}
#endif
2017-08-30 20:47:05 +02:00
}
if (generateCrash) {
unlink(path);
}
return 0;
}