1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-24 14:42:53 +02:00

* Added breakpad support for Linux.

This commit is contained in:
Christian Muehlhaeuser
2011-09-15 07:27:31 +02:00
parent d8b07cee9c
commit d8d7347394
1163 changed files with 465521 additions and 4 deletions

View File

@@ -0,0 +1,258 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include "client/solaris/handler/exception_handler.h"
#include "common/solaris/guid_creator.h"
#include "common/solaris/message_output.h"
#include "google_breakpad/common/minidump_format.h"
namespace google_breakpad {
// Signals that we are interested.
static const int kSigTable[] = {
SIGSEGV,
SIGABRT,
SIGFPE,
SIGILL,
SIGBUS
};
std::vector<ExceptionHandler*> *ExceptionHandler::handler_stack_ = NULL;
int ExceptionHandler::handler_stack_index_ = 0;
pthread_mutex_t ExceptionHandler::handler_stack_mutex_ =
PTHREAD_MUTEX_INITIALIZER;
ExceptionHandler::ExceptionHandler(const string &dump_path,
FilterCallback filter,
MinidumpCallback callback,
void *callback_context,
bool install_handler)
: filter_(filter),
callback_(callback),
callback_context_(callback_context),
dump_path_(),
installed_handler_(install_handler) {
set_dump_path(dump_path);
if (install_handler) {
SetupHandler();
}
if (install_handler) {
pthread_mutex_lock(&handler_stack_mutex_);
if (handler_stack_ == NULL)
handler_stack_ = new std::vector<ExceptionHandler *>;
handler_stack_->push_back(this);
pthread_mutex_unlock(&handler_stack_mutex_);
}
}
ExceptionHandler::~ExceptionHandler() {
TeardownAllHandlers();
pthread_mutex_lock(&handler_stack_mutex_);
if (handler_stack_->back() == this) {
handler_stack_->pop_back();
} else {
print_message1(2, "warning: removing Breakpad handler out of order\n");
for (std::vector<ExceptionHandler *>::iterator iterator =
handler_stack_->begin();
iterator != handler_stack_->end();
++iterator) {
if (*iterator == this) {
handler_stack_->erase(iterator);
}
}
}
if (handler_stack_->empty()) {
// When destroying the last ExceptionHandler that installed a handler,
// clean up the handler stack.
delete handler_stack_;
handler_stack_ = NULL;
}
pthread_mutex_unlock(&handler_stack_mutex_);
}
bool ExceptionHandler::WriteMinidump() {
return InternalWriteMinidump(0, 0, NULL);
}
// static
bool ExceptionHandler::WriteMinidump(const string &dump_path,
MinidumpCallback callback,
void *callback_context) {
ExceptionHandler handler(dump_path, NULL, callback,
callback_context, false);
return handler.InternalWriteMinidump(0, 0, NULL);
}
void ExceptionHandler::SetupHandler() {
// Signal on a different stack to avoid using the stack
// of the crashing lwp.
struct sigaltstack sig_stack;
sig_stack.ss_sp = malloc(MINSIGSTKSZ);
if (sig_stack.ss_sp == NULL)
return;
sig_stack.ss_size = MINSIGSTKSZ;
sig_stack.ss_flags = 0;
if (sigaltstack(&sig_stack, NULL) < 0)
return;
for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i)
SetupHandler(kSigTable[i]);
}
void ExceptionHandler::SetupHandler(int signo) {
struct sigaction act, old_act;
act.sa_handler = HandleException;
act.sa_flags = SA_ONSTACK;
if (sigaction(signo, &act, &old_act) < 0)
return;
old_handlers_[signo] = old_act.sa_handler;
}
void ExceptionHandler::TeardownHandler(int signo) {
if (old_handlers_.find(signo) != old_handlers_.end()) {
struct sigaction act;
act.sa_handler = old_handlers_[signo];
act.sa_flags = 0;
sigaction(signo, &act, 0);
}
}
void ExceptionHandler::TeardownAllHandlers() {
for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i) {
TeardownHandler(kSigTable[i]);
}
}
// static
void ExceptionHandler::HandleException(int signo) {
//void ExceptionHandler::HandleException(int signo, siginfo_t *sip, ucontext_t *sig_ctx) {
// The context information about the signal is put on the stack of
// the signal handler frame as value parameter. For some reasons, the
// prototype of the handler doesn't declare this information as parameter, we
// will do it by hand. The stack layout for a signal handler frame is here:
// http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libproc/common/Pstack.c#81
//
// However, if we are being called by another signal handler passing the
// signal up the chain, then we may not have this random extra parameter,
// so we may have to walk the stack to find it. We do the actual work
// on another thread, where it's a little safer, but we want the ebp
// from this frame to find it.
uintptr_t current_ebp = (uintptr_t)_getfp();
pthread_mutex_lock(&handler_stack_mutex_);
ExceptionHandler *current_handler =
handler_stack_->at(handler_stack_->size() - ++handler_stack_index_);
pthread_mutex_unlock(&handler_stack_mutex_);
// Restore original handler.
current_handler->TeardownHandler(signo);
ucontext_t *sig_ctx = NULL;
if (current_handler->InternalWriteMinidump(signo, current_ebp, &sig_ctx)) {
// if (current_handler->InternalWriteMinidump(signo, &sig_ctx)) {
// Fully handled this exception, safe to exit.
exit(EXIT_FAILURE);
} else {
// Exception not fully handled, will call the next handler in stack to
// process it.
typedef void (*SignalHandler)(int signo);
SignalHandler old_handler =
reinterpret_cast<SignalHandler>(current_handler->old_handlers_[signo]);
if (old_handler != NULL)
old_handler(signo);
}
pthread_mutex_lock(&handler_stack_mutex_);
current_handler->SetupHandler(signo);
--handler_stack_index_;
// All the handlers in stack have been invoked to handle the exception,
// normally the process should be terminated and should not reach here.
// In case we got here, ask the OS to handle it to avoid endless loop,
// normally the OS will generate a core and termiate the process. This
// may be desired to debug the program.
if (handler_stack_index_ == 0)
signal(signo, SIG_DFL);
pthread_mutex_unlock(&handler_stack_mutex_);
}
bool ExceptionHandler::InternalWriteMinidump(int signo,
uintptr_t sighandler_ebp,
ucontext_t **sig_ctx) {
if (filter_ && !filter_(callback_context_))
return false;
bool success = false;
GUID guid;
char guid_str[kGUIDStringLength + 1];
if (CreateGUID(&guid) && GUIDToString(&guid, guid_str, sizeof(guid_str))) {
char minidump_path[PATH_MAX];
snprintf(minidump_path, sizeof(minidump_path), "%s/%s.dmp",
dump_path_c_, guid_str);
// Block all the signals we want to process when writing minidump.
// We don't want it to be interrupted.
sigset_t sig_blocked, sig_old;
bool blocked = true;
sigfillset(&sig_blocked);
for (size_t i = 0; i < sizeof(kSigTable) / sizeof(kSigTable[0]); ++i)
sigdelset(&sig_blocked, kSigTable[i]);
if (sigprocmask(SIG_BLOCK, &sig_blocked, &sig_old) != 0) {
blocked = false;
print_message1(2, "HandleException: failed to block signals.\n");
}
success = minidump_generator_.WriteMinidumpToFile(
minidump_path, signo, sighandler_ebp, sig_ctx);
// Unblock the signals.
if (blocked)
sigprocmask(SIG_SETMASK, &sig_old, &sig_old);
if (callback_)
success = callback_(dump_path_c_, guid_str, callback_context_, success);
}
return success;
}
} // namespace google_breakpad

View File

@@ -0,0 +1,201 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: Alfred Peng
#ifndef CLIENT_SOLARIS_HANDLER_EXCEPTION_HANDLER_H__
#define CLIENT_SOLARIS_HANDLER_EXCEPTION_HANDLER_H__
#include <map>
#include <string>
#include <vector>
#include "client/solaris/handler/minidump_generator.h"
namespace google_breakpad {
using std::string;
//
// ExceptionHandler
//
// ExceptionHandler can write a minidump file when an exception occurs,
// or when WriteMinidump() is called explicitly by your program.
//
// To have the exception handler write minidumps when an uncaught exception
// (crash) occurs, you should create an instance early in the execution
// of your program, and keep it around for the entire time you want to
// have crash handling active (typically, until shutdown).
// (NOTE): There should be only one this kind of exception handler
// object per process.
//
// If you want to write minidumps without installing the exception handler,
// you can create an ExceptionHandler with install_handler set to false,
// then call WriteMinidump. You can also use this technique if you want to
// use different minidump callbacks for different call sites.
//
// In either case, a callback function is called when a minidump is written,
// which receives the unqiue id of the minidump. The caller can use this
// id to collect and write additional application state, and to launch an
// external crash-reporting application.
//
// Caller should try to make the callbacks as crash-friendly as possible,
// it should avoid use heap memory allocation as much as possible.
//
class ExceptionHandler {
public:
// A callback function to run before Breakpad performs any substantial
// processing of an exception. A FilterCallback is called before writing
// a minidump. context is the parameter supplied by the user as
// callback_context when the handler was created.
//
// If a FilterCallback returns true, Breakpad will continue processing,
// attempting to write a minidump. If a FilterCallback returns false,
// Breakpad will immediately report the exception as unhandled without
// writing a minidump, allowing another handler the opportunity to handle it.
typedef bool (*FilterCallback)(void *context);
// A callback function to run after the minidump has been written.
// minidump_id is a unique id for the dump, so the minidump
// file is <dump_path>/<minidump_id>.dmp. context is the parameter supplied
// by the user as callback_context when the handler was created. succeeded
// indicates whether a minidump file was successfully written.
//
// If an exception occurred and the callback returns true, Breakpad will
// treat the exception as fully-handled, suppressing any other handlers from
// being notified of the exception. If the callback returns false, Breakpad
// will treat the exception as unhandled, and allow another handler to handle
// it. If there are no other handlers, Breakpad will report the exception to
// the system as unhandled, allowing a debugger or native crash dialog the
// opportunity to handle the exception. Most callback implementations
// should normally return the value of |succeeded|, or when they wish to
// not report an exception of handled, false. Callbacks will rarely want to
// return true directly (unless |succeeded| is true).
typedef bool (*MinidumpCallback)(const char *dump_path,
const char *minidump_id,
void *context,
bool succeeded);
// Creates a new ExceptionHandler instance to handle writing minidumps.
// Before writing a minidump, the optional filter callback will be called.
// Its return value determines whether or not Breakpad should write a
// minidump. Minidump files will be written to dump_path, and the optional
// callback is called after writing the dump file, as described above.
// If install_handler is true, then a minidump will be written whenever
// an unhandled exception occurs. If it is false, minidumps will only
// be written when WriteMinidump is called.
ExceptionHandler(const string &dump_path,
FilterCallback filter, MinidumpCallback callback,
void *callback_context,
bool install_handler);
~ExceptionHandler();
// Get and Set the minidump path.
string dump_path() const { return dump_path_; }
void set_dump_path(const string &dump_path) {
dump_path_ = dump_path;
dump_path_c_ = dump_path_.c_str();
}
// Writes a minidump immediately. This can be used to capture the
// execution state independently of a crash. Returns true on success.
bool WriteMinidump();
// Convenience form of WriteMinidump which does not require an
// ExceptionHandler instance.
static bool WriteMinidump(const string &dump_path,
MinidumpCallback callback,
void *callback_context);
private:
// Setup crash handler.
void SetupHandler();
// Setup signal handler for a signal.
void SetupHandler(int signo);
// Teardown the handler for a signal.
void TeardownHandler(int signo);
// Teardown all handlers.
void TeardownAllHandlers();
// Runs the main loop for the exception handler thread.
static void* ExceptionHandlerThreadMain(void *lpParameter);
// Signal handler.
static void HandleException(int signo);
// Write all the information to the dump file.
// If called from a signal handler, sighandler_ebp is the ebp of
// that signal handler's frame, and sig_ctx is an out parameter
// that will be set to point at the ucontext_t that was placed
// on the stack by the kernel. You can pass zero and NULL
// for the second and third parameters if you are not calling
// this from a signal handler.
bool InternalWriteMinidump(int signo, uintptr_t sighandler_ebp,
ucontext_t **sig_ctx);
private:
// The callbacks before and after writing the dump file.
FilterCallback filter_;
MinidumpCallback callback_;
void *callback_context_;
// The directory in which a minidump will be written, set by the dump_path
// argument to the constructor, or set_dump_path.
string dump_path_;
// C style dump path. Keep this when setting dump path, since calling
// c_str() of std::string when crashing may not be safe.
const char *dump_path_c_;
// True if the ExceptionHandler installed an unhandled exception filter
// when created (with an install_handler parameter set to true).
bool installed_handler_;
// Keep the previous handlers for the signal.
typedef void (*sighandler_t)(int);
std::map<int, sighandler_t> old_handlers_;
// The global exception handler stack. This is need becuase there may exist
// multiple ExceptionHandler instances in a process. Each will have itself
// registered in this stack.
static std::vector<ExceptionHandler *> *handler_stack_;
// The index of the handler that should handle the next exception.
static int handler_stack_index_;
static pthread_mutex_t handler_stack_mutex_;
// The minidump generator.
MinidumpGenerator minidump_generator_;
// disallow copy ctor and operator=
explicit ExceptionHandler(const ExceptionHandler &);
void operator=(const ExceptionHandler &);
};
} // namespace google_breakpad
#endif // CLIENT_SOLARIS_HANDLER_EXCEPTION_HANDLER_H__

View File

@@ -0,0 +1,119 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "client/solaris/handler/exception_handler.h"
#include "client/solaris/handler/solaris_lwp.h"
using namespace google_breakpad;
// Thread use this to see if it should stop working.
static bool should_exit = false;
static int foo2(int arg) {
// Stack variable, used for debugging stack dumps.
int c = 0xcccccccc;
fprintf(stderr, "Thread trying to crash: %x\n", getpid());
c = *reinterpret_cast<int *>(0x5);
return c;
}
static int foo(int arg) {
// Stack variable, used for debugging stack dumps.
int b = 0xbbbbbbbb;
b = foo2(b);
return b;
}
static void *thread_crash(void *) {
// Stack variable, used for debugging stack dumps.
int a = 0xaaaaaaaa;
sleep(3);
a = foo(a);
printf("%x\n", a);
return NULL;
}
static void *thread_main(void *) {
while (!should_exit)
sleep(1);
return NULL;
}
static void CreateCrashThread() {
pthread_t h;
pthread_create(&h, NULL, thread_crash, NULL);
pthread_detach(h);
}
// Create working threads.
static void CreateThread(int num) {
pthread_t h;
for (int i = 0; i < num; ++i) {
pthread_create(&h, NULL, thread_main, NULL);
pthread_detach(h);
}
}
// Callback when minidump written.
static bool MinidumpCallback(const char *dump_path,
const char *minidump_id,
void *context,
bool succeeded) {
int index = reinterpret_cast<int>(context);
if (index == 0) {
should_exit = true;
return true;
}
// Don't process it.
return false;
}
int main(int argc, char *argv[]) {
int handler_index = 1;
ExceptionHandler handler_ignore(".", NULL, MinidumpCallback,
(void*)handler_index, true);
CreateCrashThread();
CreateThread(10);
while (true)
sleep(20);
should_exit = true;
return 0;
}

View File

@@ -0,0 +1,786 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#include <fcntl.h>
#include <sys/frame.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include "client/solaris/handler/minidump_generator.h"
#include "client/minidump_file_writer-inl.h"
#include "common/solaris/file_id.h"
namespace {
using namespace google_breakpad;
// Argument for the writer function.
struct WriterArgument {
MinidumpFileWriter *minidump_writer;
// Pid of the lwp who called WriteMinidumpToFile
int requester_pid;
// The stack bottom of the lwp which caused the dump.
// Mainly used to find the lwp id of the crashed lwp since signal
// handler may not be called in the lwp who caused it.
uintptr_t crashed_stack_bottom;
// Id of the crashing lwp.
int crashed_lwpid;
// Signal number when crash happened. Can be 0 if this is a requested dump.
int signo;
// The ebp of the signal handler frame on x86. Can be 0 if this is a
// requested dump.
uintptr_t sighandler_ebp;
// User context when crash happens. Can be NULL if this is a requested dump.
// This is actually an out parameter, but it will be filled in at the start
// of the writer LWP.
ucontext_t *sig_ctx;
// Used to get information about the lwps.
SolarisLwp *lwp_lister;
};
// Holding context information for the callback of finding the crashing lwp.
struct FindCrashLwpContext {
const SolarisLwp *lwp_lister;
uintptr_t crashing_stack_bottom;
int crashing_lwpid;
FindCrashLwpContext() :
lwp_lister(NULL),
crashing_stack_bottom(0UL),
crashing_lwpid(-1) {
}
};
// Callback for list lwps.
// It will compare the stack bottom of the provided lwp with the stack
// bottom of the crashed lwp, it they are eqaul, this lwp is the one
// who crashed.
bool IsLwpCrashedCallback(lwpstatus_t *lsp, void *context) {
FindCrashLwpContext *crashing_context =
static_cast<FindCrashLwpContext *>(context);
const SolarisLwp *lwp_lister = crashing_context->lwp_lister;
const prgregset_t *gregs = &(lsp->pr_reg);
#if TARGET_CPU_SPARC
uintptr_t last_ebp = (*gregs)[R_FP];
#elif TARGET_CPU_X86
uintptr_t last_ebp = (*gregs)[EBP];
#endif
uintptr_t stack_bottom = lwp_lister->GetLwpStackBottom(last_ebp);
if (stack_bottom > last_ebp &&
stack_bottom == crashing_context->crashing_stack_bottom) {
// Got it. Stop iteration.
crashing_context->crashing_lwpid = lsp->pr_lwpid;
return false;
}
return true;
}
// Find the crashing lwpid.
// This is done based on stack bottom comparing.
int FindCrashingLwp(uintptr_t crashing_stack_bottom,
int requester_pid,
const SolarisLwp *lwp_lister) {
FindCrashLwpContext context;
context.lwp_lister = lwp_lister;
context.crashing_stack_bottom = crashing_stack_bottom;
CallbackParam<LwpCallback> callback_param(IsLwpCrashedCallback,
&context);
lwp_lister->Lwp_iter_all(lwp_lister->getpid(), &callback_param);
return context.crashing_lwpid;
}
bool WriteLwpStack(const SolarisLwp *lwp_lister,
uintptr_t last_esp,
UntypedMDRVA *memory,
MDMemoryDescriptor *loc) {
uintptr_t stack_bottom = lwp_lister->GetLwpStackBottom(last_esp);
if (stack_bottom >= last_esp) {
int size = stack_bottom - last_esp;
if (size > 0) {
if (!memory->Allocate(size))
return false;
memory->Copy(reinterpret_cast<void *>(last_esp), size);
loc->start_of_memory_range = last_esp;
loc->memory = memory->location();
}
return true;
}
return false;
}
#if TARGET_CPU_SPARC
bool WriteContext(MDRawContextSPARC *context, ucontext_t *sig_ctx) {
assert(sig_ctx != NULL);
int* regs = sig_ctx->uc_mcontext.gregs;
context->context_flags = MD_CONTEXT_SPARC_FULL;
context->ccr = (unsigned int)(regs[0]);
context->pc = (unsigned int)(regs[REG_PC]);
context->npc = (unsigned int)(regs[REG_nPC]);
context->y = (unsigned int)(regs[REG_Y]);
context->asi = (unsigned int)(regs[19]);
context->fprs = (unsigned int)(regs[20]);
for ( int i = 0 ; i < 32; ++i ) {
context->g_r[i] = 0;
}
for ( int i = 1 ; i < 16; ++i ) {
context->g_r[i] = (uintptr_t)(sig_ctx->uc_mcontext.gregs[i + 3]);
}
context->g_r[30] = (uintptr_t)(((struct frame *)context->g_r[14])->fr_savfp);
return true;
}
bool WriteContext(MDRawContextSPARC *context, prgregset_t regs,
prfpregset_t *fp_regs) {
if (!context || !regs)
return false;
context->context_flags = MD_CONTEXT_SPARC_FULL;
context->ccr = (uintptr_t)(regs[32]);
context->pc = (uintptr_t)(regs[R_PC]);
context->npc = (uintptr_t)(regs[R_nPC]);
context->y = (uintptr_t)(regs[R_Y]);
context->asi = (uintptr_t)(regs[36]);
context->fprs = (uintptr_t)(regs[37]);
for ( int i = 0 ; i < 32 ; ++i ){
context->g_r[i] = (uintptr_t)(regs[i]);
}
return true;
}
#elif TARGET_CPU_X86
bool WriteContext(MDRawContextX86 *context, prgregset_t regs,
prfpregset_t *fp_regs) {
if (!context || !regs)
return false;
context->context_flags = MD_CONTEXT_X86_FULL;
context->cs = regs[CS];
context->ds = regs[DS];
context->es = regs[ES];
context->fs = regs[FS];
context->gs = regs[GS];
context->ss = regs[SS];
context->edi = regs[EDI];
context->esi = regs[ESI];
context->ebx = regs[EBX];
context->edx = regs[EDX];
context->ecx = regs[ECX];
context->eax = regs[EAX];
context->ebp = regs[EBP];
context->eip = regs[EIP];
context->esp = regs[UESP];
context->eflags = regs[EFL];
return true;
}
#endif /* TARGET_CPU_XXX */
// Write information about a crashed Lwp.
// When a lwp crash, kernel will write something on the stack for processing
// signal. This makes the current stack not reliable, and our stack walker
// won't figure out the whole call stack for this. So we write the stack at the
// time of the crash into the minidump file, not the current stack.
bool WriteCrashedLwpStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
const lwpstatus_t *lsp,
MDRawThread *lwp) {
assert(writer_args->sig_ctx != NULL);
lwp->thread_id = lsp->pr_lwpid;
#if TARGET_CPU_SPARC
UntypedMDRVA memory(minidump_writer);
if (!WriteLwpStack(writer_args->lwp_lister,
writer_args->sig_ctx->uc_mcontext.gregs[REG_O6],
&memory,
&lwp->stack))
return false;
TypedMDRVA<MDRawContextSPARC> context(minidump_writer);
if (!context.Allocate())
return false;
lwp->thread_context = context.location();
memset(context.get(), 0, sizeof(MDRawContextSPARC));
return WriteContext(context.get(), writer_args->sig_ctx);
#elif TARGET_CPU_X86
UntypedMDRVA memory(minidump_writer);
if (!WriteLwpStack(writer_args->lwp_lister,
writer_args->sig_ctx->uc_mcontext.gregs[UESP],
&memory,
&lwp->stack))
return false;
TypedMDRVA<MDRawContextX86> context(minidump_writer);
if (!context.Allocate())
return false;
lwp->thread_context = context.location();
memset(context.get(), 0, sizeof(MDRawContextX86));
return WriteContext(context.get(),
(int *)&writer_args->sig_ctx->uc_mcontext.gregs,
&writer_args->sig_ctx->uc_mcontext.fpregs);
#endif
}
bool WriteLwpStream(MinidumpFileWriter *minidump_writer,
const SolarisLwp *lwp_lister,
const lwpstatus_t *lsp, MDRawThread *lwp) {
prfpregset_t fp_regs = lsp->pr_fpreg;
const prgregset_t *gregs = &(lsp->pr_reg);
UntypedMDRVA memory(minidump_writer);
#if TARGET_CPU_SPARC
if (!WriteLwpStack(lwp_lister,
(*gregs)[R_SP],
&memory,
&lwp->stack))
return false;
// Write context
TypedMDRVA<MDRawContextSPARC> context(minidump_writer);
if (!context.Allocate())
return false;
// should be the thread_id
lwp->thread_id = lsp->pr_lwpid;
lwp->thread_context = context.location();
memset(context.get(), 0, sizeof(MDRawContextSPARC));
#elif TARGET_CPU_X86
if (!WriteLwpStack(lwp_lister,
(*gregs)[UESP],
&memory,
&lwp->stack))
return false;
// Write context
TypedMDRVA<MDRawContextX86> context(minidump_writer);
if (!context.Allocate())
return false;
// should be the thread_id
lwp->thread_id = lsp->pr_lwpid;
lwp->thread_context = context.location();
memset(context.get(), 0, sizeof(MDRawContextX86));
#endif /* TARGET_CPU_XXX */
return WriteContext(context.get(), (int *)gregs, &fp_regs);
}
bool WriteCPUInformation(MDRawSystemInfo *sys_info) {
struct utsname uts;
char *major, *minor, *build;
sys_info->number_of_processors = sysconf(_SC_NPROCESSORS_CONF);
sys_info->processor_architecture = MD_CPU_ARCHITECTURE_UNKNOWN;
if (uname(&uts) != -1) {
// Match "i86pc" as X86 architecture.
if (strcmp(uts.machine, "i86pc") == 0)
sys_info->processor_architecture = MD_CPU_ARCHITECTURE_X86;
else if (strcmp(uts.machine, "sun4u") == 0)
sys_info->processor_architecture = MD_CPU_ARCHITECTURE_SPARC;
}
major = uts.release;
minor = strchr(major, '.');
*minor = '\0';
++minor;
sys_info->major_version = atoi(major);
sys_info->minor_version = atoi(minor);
build = strchr(uts.version, '_');
++build;
sys_info->build_number = atoi(build);
return true;
}
bool WriteOSInformation(MinidumpFileWriter *minidump_writer,
MDRawSystemInfo *sys_info) {
sys_info->platform_id = MD_OS_SOLARIS;
struct utsname uts;
if (uname(&uts) != -1) {
char os_version[512];
size_t space_left = sizeof(os_version);
memset(os_version, 0, space_left);
const char *os_info_table[] = {
uts.sysname,
uts.release,
uts.version,
uts.machine,
"OpenSolaris",
NULL
};
for (const char **cur_os_info = os_info_table;
*cur_os_info != NULL;
++cur_os_info) {
if (cur_os_info != os_info_table && space_left > 1) {
strcat(os_version, " ");
--space_left;
}
if (space_left > strlen(*cur_os_info)) {
strcat(os_version, *cur_os_info);
space_left -= strlen(*cur_os_info);
} else {
break;
}
}
MDLocationDescriptor location;
if (!minidump_writer->WriteString(os_version, 0, &location))
return false;
sys_info->csd_version_rva = location.rva;
}
return true;
}
// Callback context for get writting lwp information.
struct LwpInfoCallbackCtx {
MinidumpFileWriter *minidump_writer;
const WriterArgument *writer_args;
TypedMDRVA<MDRawThreadList> *list;
int lwp_index;
};
bool LwpInformationCallback(lwpstatus_t *lsp, void *context) {
bool success = true;
LwpInfoCallbackCtx *callback_context =
static_cast<LwpInfoCallbackCtx *>(context);
// The current lwp is the one to handle the crash. Ignore it.
if (lsp->pr_lwpid != pthread_self()) {
LwpInfoCallbackCtx *callback_context =
static_cast<LwpInfoCallbackCtx *>(context);
MDRawThread lwp;
memset(&lwp, 0, sizeof(MDRawThread));
if (lsp->pr_lwpid != callback_context->writer_args->crashed_lwpid ||
callback_context->writer_args->sig_ctx == NULL) {
success = WriteLwpStream(callback_context->minidump_writer,
callback_context->writer_args->lwp_lister,
lsp, &lwp);
} else {
success = WriteCrashedLwpStream(callback_context->minidump_writer,
callback_context->writer_args,
lsp, &lwp);
}
if (success) {
callback_context->list->CopyIndexAfterObject(
callback_context->lwp_index++,
&lwp, sizeof(MDRawThread));
}
}
return success;
}
bool WriteLwpListStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
MDRawDirectory *dir) {
// Get the lwp information.
const SolarisLwp *lwp_lister = writer_args->lwp_lister;
int lwp_count = lwp_lister->GetLwpCount();
if (lwp_count < 0)
return false;
TypedMDRVA<MDRawThreadList> list(minidump_writer);
if (!list.AllocateObjectAndArray(lwp_count - 1, sizeof(MDRawThread)))
return false;
dir->stream_type = MD_THREAD_LIST_STREAM;
dir->location = list.location();
list.get()->number_of_threads = lwp_count - 1;
LwpInfoCallbackCtx context;
context.minidump_writer = minidump_writer;
context.writer_args = writer_args;
context.list = &list;
context.lwp_index = 0;
CallbackParam<LwpCallback> callback_param(LwpInformationCallback,
&context);
int written =
lwp_lister->Lwp_iter_all(lwp_lister->getpid(), &callback_param);
return written == lwp_count;
}
bool WriteCVRecord(MinidumpFileWriter *minidump_writer,
MDRawModule *module,
const char *module_path,
char *realname) {
TypedMDRVA<MDCVInfoPDB70> cv(minidump_writer);
char path[PATH_MAX];
const char *module_name = module_path ? module_path : "<Unknown>";
snprintf(path, sizeof(path), "/proc/self/object/%s", module_name);
size_t module_name_length = strlen(realname);
if (!cv.AllocateObjectAndArray(module_name_length + 1, sizeof(u_int8_t)))
return false;
if (!cv.CopyIndexAfterObject(0, realname, module_name_length))
return false;
module->cv_record = cv.location();
MDCVInfoPDB70 *cv_ptr = cv.get();
memset(cv_ptr, 0, sizeof(MDCVInfoPDB70));
cv_ptr->cv_signature = MD_CVINFOPDB70_SIGNATURE;
cv_ptr->age = 0;
// Get the module identifier
FileID file_id(path);
unsigned char identifier[16];
if (file_id.ElfFileIdentifier(identifier)) {
cv_ptr->signature.data1 = (uint32_t)identifier[0] << 24 |
(uint32_t)identifier[1] << 16 | (uint32_t)identifier[2] << 8 |
(uint32_t)identifier[3];
cv_ptr->signature.data2 = (uint32_t)identifier[4] << 8 | identifier[5];
cv_ptr->signature.data3 = (uint32_t)identifier[6] << 8 | identifier[7];
cv_ptr->signature.data4[0] = identifier[8];
cv_ptr->signature.data4[1] = identifier[9];
cv_ptr->signature.data4[2] = identifier[10];
cv_ptr->signature.data4[3] = identifier[11];
cv_ptr->signature.data4[4] = identifier[12];
cv_ptr->signature.data4[5] = identifier[13];
cv_ptr->signature.data4[6] = identifier[14];
cv_ptr->signature.data4[7] = identifier[15];
}
return true;
}
struct ModuleInfoCallbackCtx {
MinidumpFileWriter *minidump_writer;
const WriterArgument *writer_args;
TypedMDRVA<MDRawModuleList> *list;
int module_index;
};
bool ModuleInfoCallback(const ModuleInfo &module_info, void *context) {
ModuleInfoCallbackCtx *callback_context =
static_cast<ModuleInfoCallbackCtx *>(context);
// Skip those modules without name, or those that are not modules.
if (strlen(module_info.name) == 0)
return true;
MDRawModule module;
memset(&module, 0, sizeof(module));
MDLocationDescriptor loc;
char path[PATH_MAX];
char buf[PATH_MAX];
char *realname;
int count;
snprintf(path, sizeof (path), "/proc/self/path/%s", module_info.name);
if ((count = readlink(path, buf, PATH_MAX)) < 0)
return false;
buf[count] = '\0';
if ((realname = strrchr(buf, '/')) == NULL)
return false;
realname++;
if (!callback_context->minidump_writer->WriteString(realname, 0, &loc))
return false;
module.base_of_image = (u_int64_t)module_info.start_addr;
module.size_of_image = module_info.size;
module.module_name_rva = loc.rva;
if (!WriteCVRecord(callback_context->minidump_writer, &module,
module_info.name, realname))
return false;
callback_context->list->CopyIndexAfterObject(
callback_context->module_index++, &module, MD_MODULE_SIZE);
return true;
}
bool WriteModuleListStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
MDRawDirectory *dir) {
TypedMDRVA<MDRawModuleList> list(minidump_writer);
int module_count = writer_args->lwp_lister->GetModuleCount();
if (module_count <= 0 ||
!list.AllocateObjectAndArray(module_count, MD_MODULE_SIZE)) {
return false;
}
dir->stream_type = MD_MODULE_LIST_STREAM;
dir->location = list.location();
list.get()->number_of_modules = module_count;
ModuleInfoCallbackCtx context;
context.minidump_writer = minidump_writer;
context.writer_args = writer_args;
context.list = &list;
context.module_index = 0;
CallbackParam<ModuleCallback> callback(ModuleInfoCallback, &context);
return writer_args->lwp_lister->ListModules(&callback) == module_count;
}
bool WriteSystemInfoStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
MDRawDirectory *dir) {
TypedMDRVA<MDRawSystemInfo> sys_info(minidump_writer);
if (!sys_info.Allocate())
return false;
dir->stream_type = MD_SYSTEM_INFO_STREAM;
dir->location = sys_info.location();
return WriteCPUInformation(sys_info.get()) &&
WriteOSInformation(minidump_writer, sys_info.get());
}
bool WriteExceptionStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
MDRawDirectory *dir) {
// This happenes when this is not a crash, but a requested dump.
if (writer_args->sig_ctx == NULL)
return false;
TypedMDRVA<MDRawExceptionStream> exception(minidump_writer);
if (!exception.Allocate())
return false;
dir->stream_type = MD_EXCEPTION_STREAM;
dir->location = exception.location();
exception.get()->thread_id = writer_args->crashed_lwpid;
exception.get()->exception_record.exception_code = writer_args->signo;
exception.get()->exception_record.exception_flags = 0;
#if TARGET_CPU_SPARC
if (writer_args->sig_ctx != NULL) {
exception.get()->exception_record.exception_address =
writer_args->sig_ctx->uc_mcontext.gregs[REG_PC];
} else {
return true;
}
// Write context of the exception.
TypedMDRVA<MDRawContextSPARC> context(minidump_writer);
if (!context.Allocate())
return false;
exception.get()->thread_context = context.location();
memset(context.get(), 0, sizeof(MDRawContextSPARC));
return WriteContext(context.get(), writer_args->sig_ctx);
#elif TARGET_CPU_X86
if (writer_args->sig_ctx != NULL) {
exception.get()->exception_record.exception_address =
writer_args->sig_ctx->uc_mcontext.gregs[EIP];
} else {
return true;
}
// Write context of the exception.
TypedMDRVA<MDRawContextX86> context(minidump_writer);
if (!context.Allocate())
return false;
exception.get()->thread_context = context.location();
memset(context.get(), 0, sizeof(MDRawContextX86));
return WriteContext(context.get(),
(int *)&writer_args->sig_ctx->uc_mcontext.gregs,
NULL);
#endif
}
bool WriteMiscInfoStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
MDRawDirectory *dir) {
TypedMDRVA<MDRawMiscInfo> info(minidump_writer);
if (!info.Allocate())
return false;
dir->stream_type = MD_MISC_INFO_STREAM;
dir->location = info.location();
info.get()->size_of_info = sizeof(MDRawMiscInfo);
info.get()->flags1 = MD_MISCINFO_FLAGS1_PROCESS_ID;
info.get()->process_id = writer_args->requester_pid;
return true;
}
bool WriteBreakpadInfoStream(MinidumpFileWriter *minidump_writer,
const WriterArgument *writer_args,
MDRawDirectory *dir) {
TypedMDRVA<MDRawBreakpadInfo> info(minidump_writer);
if (!info.Allocate())
return false;
dir->stream_type = MD_BREAKPAD_INFO_STREAM;
dir->location = info.location();
info.get()->validity = MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID |
MD_BREAKPAD_INFO_VALID_REQUESTING_THREAD_ID;
info.get()->dump_thread_id = getpid();
info.get()->requesting_thread_id = writer_args->requester_pid;
return true;
}
class AutoLwpResumer {
public:
AutoLwpResumer(SolarisLwp *lwp) : lwp_(lwp) {}
~AutoLwpResumer() { lwp_->ControlAllLwps(false); }
private:
SolarisLwp *lwp_;
};
// Prototype of writer functions.
typedef bool (*WriteStreamFN)(MinidumpFileWriter *,
const WriterArgument *,
MDRawDirectory *);
// Function table to writer a full minidump.
const WriteStreamFN writers[] = {
WriteLwpListStream,
WriteModuleListStream,
WriteSystemInfoStream,
WriteExceptionStream,
WriteMiscInfoStream,
WriteBreakpadInfoStream,
};
// Will call each writer function in the writers table.
//void* MinidumpGenerator::Write(void *argument) {
void* Write(void *argument) {
WriterArgument *writer_args = static_cast<WriterArgument *>(argument);
if (!writer_args->lwp_lister->ControlAllLwps(true))
return NULL;
AutoLwpResumer lwpResumer(writer_args->lwp_lister);
if (writer_args->sighandler_ebp != 0 &&
writer_args->lwp_lister->FindSigContext(writer_args->sighandler_ebp,
&writer_args->sig_ctx)) {
writer_args->crashed_stack_bottom =
writer_args->lwp_lister->GetLwpStackBottom(
#if TARGET_CPU_SPARC
writer_args->sig_ctx->uc_mcontext.gregs[REG_O6]
#elif TARGET_CPU_X86
writer_args->sig_ctx->uc_mcontext.gregs[UESP]
#endif
);
int crashed_lwpid = FindCrashingLwp(writer_args->crashed_stack_bottom,
writer_args->requester_pid,
writer_args->lwp_lister);
if (crashed_lwpid > 0)
writer_args->crashed_lwpid = crashed_lwpid;
}
MinidumpFileWriter *minidump_writer = writer_args->minidump_writer;
TypedMDRVA<MDRawHeader> header(minidump_writer);
TypedMDRVA<MDRawDirectory> dir(minidump_writer);
if (!header.Allocate())
return 0;
int writer_count = sizeof(writers) / sizeof(writers[0]);
// Need directory space for all writers.
if (!dir.AllocateArray(writer_count))
return 0;
header.get()->signature = MD_HEADER_SIGNATURE;
header.get()->version = MD_HEADER_VERSION;
header.get()->time_date_stamp = time(NULL);
header.get()->stream_count = writer_count;
header.get()->stream_directory_rva = dir.position();
int dir_index = 0;
MDRawDirectory local_dir;
for (int i = 0; i < writer_count; ++i) {
if ((*writers[i])(minidump_writer, writer_args, &local_dir))
dir.CopyIndex(dir_index++, &local_dir);
}
return 0;
}
} // namespace
namespace google_breakpad {
MinidumpGenerator::MinidumpGenerator() {
}
MinidumpGenerator::~MinidumpGenerator() {
}
// Write minidump into file.
// It runs in a different thread from the crashing thread.
bool MinidumpGenerator::WriteMinidumpToFile(const char *file_pathname,
int signo,
uintptr_t sighandler_ebp,
ucontext_t **sig_ctx) const {
// The exception handler thread.
pthread_t handler_thread;
assert(file_pathname != NULL);
if (file_pathname == NULL)
return false;
MinidumpFileWriter minidump_writer;
if (minidump_writer.Open(file_pathname)) {
WriterArgument argument;
memset(&argument, 0, sizeof(argument));
SolarisLwp lwp_lister(getpid());
argument.lwp_lister = &lwp_lister;
argument.minidump_writer = &minidump_writer;
argument.requester_pid = getpid();
argument.crashed_lwpid = pthread_self();
argument.signo = signo;
argument.sighandler_ebp = sighandler_ebp;
argument.sig_ctx = NULL;
pthread_create(&handler_thread, NULL, Write, (void *)&argument);
pthread_join(handler_thread, NULL);
return true;
}
return false;
}
} // namespace google_breakpad

View File

@@ -0,0 +1,70 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#ifndef CLIENT_SOLARIS_HANDLER_MINIDUMP_GENERATOR_H__
#define CLIENT_SOLARIS_HANDLER_MINIDUMP_GENERATOR_H__
#include <ucontext.h>
#include "client/minidump_file_writer.h"
#include "client/solaris/handler/solaris_lwp.h"
#include "google_breakpad/common/breakpad_types.h"
#include "google_breakpad/common/minidump_format.h"
namespace google_breakpad {
//
// MinidumpGenerator
//
// A minidump generator should be created before any exception happen.
//
class MinidumpGenerator {
// Callback run for writing lwp information in the process.
friend bool LwpInformationCallback(lwpstatus_t *lsp, void *context);
// Callback run for writing module information in the process.
friend bool ModuleInfoCallback(const ModuleInfo &module_info, void *context);
public:
MinidumpGenerator();
~MinidumpGenerator();
// Write minidump.
bool WriteMinidumpToFile(const char *file_pathname,
int signo,
uintptr_t sighandler_ebp,
ucontext_t **sig_ctx) const;
};
} // namespace google_breakpad
#endif // CLIENT_SOLARIS_HANDLER_MINIDUMP_GENERATOR_H_

View File

@@ -0,0 +1,75 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#include <pthread.h>
#include <unistd.h>
#include "client/minidump_file_writer.h"
#include "client/solaris/handler/minidump_generator.h"
using std::string;
using google_breakpad::MinidumpGenerator;
static bool doneWritingReport = false;
static void *Reporter(void *) {
char buffer[PATH_MAX];
MinidumpGenerator md;
// Write it to the desktop
snprintf(buffer, sizeof(buffer), "./minidump_test.out");
fprintf(stdout, "Writing %s\n", buffer);
md.WriteMinidumpToFile(buffer, 0, 0, NULL);
doneWritingReport = true;
return NULL;
}
static void SleepyFunction() {
while (!doneWritingReport) {
usleep(100);
}
}
int main(int argc, char * const argv[]) {
pthread_t reporter_thread;
if (pthread_create(&reporter_thread, NULL, Reporter, NULL) == 0) {
pthread_detach(reporter_thread);
} else {
perror("pthread_create");
}
SleepyFunction();
return 0;
}

View File

@@ -0,0 +1,436 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#include <dirent.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/frame.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include "client/solaris/handler/solaris_lwp.h"
#include "common/solaris/message_output.h"
using namespace google_breakpad;
// This unamed namespace contains helper function.
namespace {
uintptr_t stack_base_address = 0;
static const int HEADER_MAX = 2000;
static const int MAP_MAX = 1000;
// Context information for the callbacks when validating address by listing
// modules.
struct AddressValidatingContext {
uintptr_t address;
bool is_mapped;
AddressValidatingContext() : address(0UL), is_mapped(false) {
}
};
// Convert from string to int.
static bool LocalAtoi(char *s, int *r) {
assert(s != NULL);
assert(r != NULL);
char *endptr = NULL;
int ret = strtol(s, &endptr, 10);
if (endptr == s)
return false;
*r = ret;
return true;
}
// Callback invoked for each mapped module.
// It uses the module's adderss range to validate the address.
static bool AddressNotInModuleCallback(const ModuleInfo &module_info,
void *context) {
AddressValidatingContext *addr =
reinterpret_cast<AddressValidatingContext *>(context);
if (addr->is_mapped = ((module_info.start_addr > 0) &&
(addr->address >= module_info.start_addr) &&
(addr->address <= module_info.start_addr +
module_info.size))) {
stack_base_address = module_info.start_addr + module_info.size;
}
return !addr->is_mapped;
}
static int IterateLwpAll(int pid,
CallbackParam<LwpidCallback> *callback_param) {
char lwp_path[40];
DIR *dir;
int count = 0;
snprintf(lwp_path, sizeof (lwp_path), "/proc/%d/lwp", (int)pid);
if ((dir = opendir(lwp_path)) == NULL)
return -1;
struct dirent *entry = NULL;
while ((entry = readdir(dir)) != NULL) {
if ((strcmp(entry->d_name, ".") != 0) &&
(strcmp(entry->d_name, "..") != 0)) {
int lwpid = 0;
int last_pid = 0;
if (LocalAtoi(entry->d_name, &lwpid) && last_pid != lwpid) {
last_pid = lwpid;
++count;
if (callback_param &&
!(callback_param->call_back)(lwpid, callback_param->context)) {
break;
}
}
}
}
closedir(dir);
return count;
}
#if defined(__i386) && !defined(NO_FRAME_POINTER)
void *GetNextFrame(void **last_ebp) {
void *sp = *last_ebp;
if ((unsigned long)sp == (unsigned long)last_ebp)
return NULL;
if ((unsigned long)sp & (sizeof(void *) - 1))
return NULL;
if ((unsigned long)sp - (unsigned long)last_ebp > 100000)
return NULL;
return sp;
}
#elif defined(__sparc)
void *GetNextFrame(void *last_ebp) {
return reinterpret_cast<struct frame *>(last_ebp)->fr_savfp;
}
#else
void *GetNextFrame(void **last_ebp) {
return reinterpret_cast<void*>(last_ebp);
}
#endif
class AutoCloser {
public:
AutoCloser(int fd) : fd_(fd) {}
~AutoCloser() { if (fd_) close(fd_); }
private:
int fd_;
};
// Control the execution of the lwp.
// Suspend/Resume lwp based on the value of context.
static bool ControlLwp(int lwpid, void *context) {
// The current thread is the one to handle the crash. Ignore it.
if (lwpid != pthread_self()) {
int ctlfd;
char procname[PATH_MAX];
bool suspend = *(bool *)context;
// Open the /proc/$pid/lwp/$lwpid/lwpctl files
snprintf(procname, sizeof (procname), "/proc/self/lwp/%d/lwpctl", lwpid);
if ((ctlfd = open(procname, O_WRONLY|O_EXCL)) < 0) {
print_message2(2, "failed to open %s in ControlLwp\n", procname);
return false;
}
AutoCloser autocloser(ctlfd);
long ctl[2];
ctl[0] = suspend ? PCSTOP : PCRUN;
ctl[1] = 0;
if (write(ctlfd, ctl, sizeof (ctl)) != sizeof (ctl)) {
print_message2(2, "failed in lwp %d\n", lwpid);
return false;
}
}
return true;
}
/*
* Utility function to read the contents of a file that contains a
* prheader_t at the start (/proc/$pid/lstatus or /proc/$pid/lpsinfo).
* Return true on success.
*/
static bool read_lfile(int pid, const char *lname, prheader_t *lhp) {
char lpath[PATH_MAX];
struct stat statb;
int fd;
size_t size;
snprintf(lpath, sizeof (lpath), "/proc/%d/%s", pid, lname);
if ((fd = open(lpath, O_RDONLY)) < 0) {
print_message2(2, "failed to open %s in read_lfile\n", lpath);
return false;
}
AutoCloser autocloser(fd);
if (fstat(fd, &statb) != 0)
return false;
size = statb.st_size;
if ((size / sizeof (prheader_t)) + 32 > HEADER_MAX) {
print_message1(2, "map size overflow\n");
return false;
}
if (pread(fd, lhp, size, 0) <= sizeof (prheader_t))
return false;
return true;
}
} // namespace
namespace google_breakpad {
SolarisLwp::SolarisLwp(int pid) : pid_(pid) {
}
SolarisLwp::~SolarisLwp() {
}
int SolarisLwp::ControlAllLwps(bool suspend) {
CallbackParam<LwpidCallback> callback_param(ControlLwp, &suspend);
return IterateLwpAll(pid_, &callback_param);
}
int SolarisLwp::GetLwpCount() const {
return IterateLwpAll(pid_, NULL);
}
int SolarisLwp::Lwp_iter_all(int pid,
CallbackParam<LwpCallback> *callback_param) const {
lwpstatus_t *Lsp;
lwpstatus_t *sp;
prheader_t lphp[HEADER_MAX];
prheader_t lhp[HEADER_MAX];
prheader_t *Lphp = lphp;
prheader_t *Lhp = lhp;
lwpsinfo_t *Lpsp;
long nstat;
long ninfo;
int rv = 0;
/*
* The /proc/pid/lstatus file has the array of lwpstatus_t's and the
* /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
*/
if (read_lfile(pid, "lstatus", Lhp) == NULL)
return -1;
if (read_lfile(pid, "lpsinfo", Lphp) == NULL) {
return -1;
}
Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
for (ninfo = Lphp->pr_nent; ninfo != 0; --ninfo) {
if (Lpsp->pr_sname != 'Z') {
sp = Lsp;
Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
} else {
sp = NULL;
}
if (callback_param &&
!(callback_param->call_back)(sp, callback_param->context))
break;
++rv;
Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
}
return rv;
}
uintptr_t SolarisLwp::GetLwpStackBottom(uintptr_t current_esp) const {
AddressValidatingContext addr;
addr.address = current_esp;
CallbackParam<ModuleCallback> callback_param(AddressNotInModuleCallback,
&addr);
ListModules(&callback_param);
return stack_base_address;
}
int SolarisLwp::GetModuleCount() const {
return ListModules(NULL);
}
int SolarisLwp::ListModules(
CallbackParam<ModuleCallback> *callback_param) const {
const char *maps_path = "/proc/self/map";
struct stat status;
int fd = 0, num;
prmap_t map_array[MAP_MAX];
prmap_t *maps = map_array;
size_t size;
if ((fd = open(maps_path, O_RDONLY)) == -1) {
print_message2(2, "failed to open %s in ListModules\n", maps_path);
return -1;
}
AutoCloser autocloser(fd);
if (fstat(fd, &status))
return -1;
/*
* Determine number of mappings, this value must be
* larger than the actual module count
*/
size = status.st_size;
if ((num = (int)(size / sizeof (prmap_t))) > MAP_MAX) {
print_message1(2, "map size overflow\n");
return -1;
}
if (read(fd, (void *)maps, size) < 0) {
print_message2(2, "failed to read %d\n", fd);
return -1;
}
prmap_t *_maps;
int _num;
int module_count = 0;
/*
* Scan each mapping - note it is assummed that the mappings are
* presented in order. We fill holes between mappings. On intel
* the last mapping is usually the data segment of ld.so.1, after
* this comes a red zone into which non-fixed mapping won't get
* place. Thus we can simply bail from the loop after seeing the
* last mapping.
*/
for (_num = 0, _maps = maps; _num < num; ++_num, ++_maps) {
ModuleInfo module;
char *name = _maps->pr_mapname;
memset(&module, 0, sizeof (module));
module.start_addr = _maps->pr_vaddr;
module.size = _maps->pr_size;
if (strlen(name) > 0) {
int objectfd = 0;
char path[PATH_MAX];
char buf[SELFMAG];
snprintf(path, sizeof (path), "/proc/self/object/%s", name);
if ((objectfd = open(path, O_RDONLY)) < 0) {
print_message1(2, "can't open module file\n");
continue;
}
AutoCloser autocloser(objectfd);
if (read(objectfd, buf, SELFMAG) != SELFMAG) {
print_message1(2, "can't read module file\n");
continue;
}
if (buf[0] != ELFMAG0 || buf[1] != ELFMAG1 ||
buf[2] != ELFMAG2 || buf[3] != ELFMAG3) {
continue;
}
strncpy(module.name, name, sizeof (module.name) - 1);
++module_count;
}
if (callback_param &&
(!callback_param->call_back(module, callback_param->context))) {
break;
}
}
return module_count;
}
// Check if the address is a valid virtual address.
// If the address is in any of the mapped modules, we take it as valid.
// Otherwise it is invalid.
bool SolarisLwp::IsAddressMapped(uintptr_t address) const {
AddressValidatingContext addr;
addr.address = address;
CallbackParam<ModuleCallback> callback_param(AddressNotInModuleCallback,
&addr);
ListModules(&callback_param);
return addr.is_mapped;
}
// We're looking for a ucontext_t as the second parameter
// to a signal handler function call. Luckily, the ucontext_t
// has an ebp(fp on SPARC) member which should match the ebp(fp)
// pointed to by the ebp(fp) of the signal handler frame.
// The Solaris stack looks like this:
// http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libproc/common/Pstack.c#81
bool SolarisLwp::FindSigContext(uintptr_t sighandler_ebp,
ucontext_t **sig_ctx) {
uintptr_t previous_ebp;
uintptr_t sig_ebp;
const int MAX_STACK_DEPTH = 50;
int depth_counter = 0;
do {
#if TARGET_CPU_SPARC
previous_ebp = reinterpret_cast<uintptr_t>(GetNextFrame(
reinterpret_cast<void*>(sighandler_ebp)));
*sig_ctx = reinterpret_cast<ucontext_t*>(sighandler_ebp + sizeof (struct frame));
uintptr_t sig_esp = (*sig_ctx)->uc_mcontext.gregs[REG_O6];
if (sig_esp < previous_ebp && sig_esp > sighandler_ebp)
sig_ebp = (uintptr_t)(((struct frame *)sig_esp)->fr_savfp);
#elif TARGET_CPU_X86
previous_ebp = reinterpret_cast<uintptr_t>(GetNextFrame(
reinterpret_cast<void**>(sighandler_ebp)));
*sig_ctx = reinterpret_cast<ucontext_t*>(sighandler_ebp + sizeof (struct frame) +
3 * sizeof(uintptr_t));
sig_ebp = (*sig_ctx)->uc_mcontext.gregs[EBP];
#endif
sighandler_ebp = previous_ebp;
depth_counter++;
} while(previous_ebp != sig_ebp && sighandler_ebp != 0 &&
IsAddressMapped(sighandler_ebp) && depth_counter < MAX_STACK_DEPTH);
return previous_ebp == sig_ebp && previous_ebp != 0;
}
} // namespace google_breakpad

View File

@@ -0,0 +1,160 @@
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#ifndef CLIENT_SOLARIS_HANDLER_SOLARIS_LWP_H__
#define CLIENT_SOLARIS_HANDLER_SOLARIS_LWP_H__
#if defined(sparc) || defined(__sparc)
#define TARGET_CPU_SPARC 1
#elif defined(i386) || defined(__i386)
#define TARGET_CPU_X86 1
#else
#error "cannot determine cpu type"
#endif
#include <signal.h>
#include <stdint.h>
#include <sys/user.h>
#include <ucontext.h>
#ifndef _KERNEL
#define _KERNEL
#define MUST_UNDEF_KERNEL
#endif // _KERNEL
#include <sys/procfs.h>
#ifdef MUST_UNDEF_KERNEL
#undef _KERNEL
#undef MUST_UNDEF_KERNEL
#endif // MUST_UNDEF_KERNEL
namespace google_breakpad {
// Max module path name length.
static const int kMaxModuleNameLength = 256;
// Holding infomaton about a module in the process.
struct ModuleInfo {
char name[kMaxModuleNameLength];
uintptr_t start_addr;
int size;
};
// A callback to run when getting a lwp in the process.
// Return true will go on to the next lwp while return false will stop the
// iteration.
typedef bool (*LwpCallback)(lwpstatus_t* lsp, void *context);
// A callback to run when a new module is found in the process.
// Return true will go on to the next module while return false will stop the
// iteration.
typedef bool (*ModuleCallback)(const ModuleInfo &module_info, void *context);
// A callback to run when getting a lwpid in the process.
// Return true will go on to the next lwp while return false will stop the
// iteration.
typedef bool (*LwpidCallback)(int lwpid, void *context);
// Holding the callback information.
template<class CallbackFunc>
struct CallbackParam {
// Callback function address.
CallbackFunc call_back;
// Callback context;
void *context;
CallbackParam() : call_back(NULL), context(NULL) {
}
CallbackParam(CallbackFunc func, void *func_context) :
call_back(func), context(func_context) {
}
};
///////////////////////////////////////////////////////////////////////////////
//
// SolarisLwp
//
// Provides handy support for operation on Solaris lwps.
// It uses proc file system to get lwp information.
//
// TODO(Alfred): Currently it only supports x86. Add SPARC support.
//
class SolarisLwp {
public:
// Create a SolarisLwp instance to list all the lwps in a process.
explicit SolarisLwp(int pid);
~SolarisLwp();
int getpid() const { return this->pid_; }
// Control all the lwps in the process.
// Return the number of suspended/resumed lwps in the process.
// Return -1 means failed to control lwps.
int ControlAllLwps(bool suspend);
// Get the count of lwps in the process.
// Return -1 means error.
int GetLwpCount() const;
// Iterate the lwps of process.
// Whenever there is a lwp found, the callback will be invoked to process
// the information.
// Return the callback return value or -1 on error.
int Lwp_iter_all(int pid, CallbackParam<LwpCallback> *callback_param) const;
// Get the module count of the current process.
int GetModuleCount() const;
// Get the mapped modules in the address space.
// Whenever a module is found, the callback will be invoked to process the
// information.
// Return how may modules are found.
int ListModules(CallbackParam<ModuleCallback> *callback_param) const;
// Get the bottom of the stack from esp.
uintptr_t GetLwpStackBottom(uintptr_t current_esp) const;
// Finds a signal context on the stack given the ebp of our signal handler.
bool FindSigContext(uintptr_t sighandler_ebp, ucontext_t **sig_ctx);
private:
// Check if the address is a valid virtual address.
bool IsAddressMapped(uintptr_t address) const;
private:
// The pid of the process we are listing lwps.
int pid_;
};
} // namespace google_breakpad
#endif // CLIENT_SOLARIS_HANDLER_SOLARIS_LWP_H__