Skip to content

Commit

Permalink
Refactor OXT and fix some problems uncovered by AddressSanitizer. Thr…
Browse files Browse the repository at this point in the history
…ead interruption is now more stable: we work around some mysterious issues on OS X and we only interrupt signals when the thread is actually inside an oxt::syscall function.
  • Loading branch information
FooBarWidget committed Jul 21, 2012
1 parent 81101ee commit 45c11ef
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 479 deletions.
2 changes: 2 additions & 0 deletions ext/common/agents/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define _GNU_SOURCE
#endif

#include <oxt/initialize.hpp>
#include <oxt/system_calls.hpp>
#include <oxt/backtrace.hpp>
#include <sys/types.h>
Expand Down Expand Up @@ -660,6 +661,7 @@ initializeAgent(int argc, char *argv[], const char *processName) {
if (hasEnvOption("PASSENGER_ABORT_HANDLER", true)) {
installAbortHandler();
}
oxt::initialize();
setup_syscall_interruption_support();
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Expand Down
2 changes: 2 additions & 0 deletions ext/common/agents/HelperAgent/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ RequestHandler::getClientPointer(const ClientPtr &client) {
#include <MultiLibeio.cpp>
#include <iostream>
#include <agents/Base.h>
#include <oxt/initialize.hpp>

using namespace std;
using namespace Passenger;
Expand Down Expand Up @@ -228,6 +229,7 @@ ignoreSigpipe() {

int
main() {
oxt::initialize();
setup_syscall_interruption_support();
ignoreSigpipe();
//installAbortHandler();
Expand Down
163 changes: 0 additions & 163 deletions ext/oxt/backtrace.cpp

This file was deleted.

107 changes: 5 additions & 102 deletions ext/oxt/detail/backtrace_enabled.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OXT - OS eXtensions for boosT
* Provides important functionality necessary for writing robust server software.
*
* Copyright (c) 2010 Phusion
* Copyright (c) 2010, 2011, 2012 Phusion
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,32 +27,10 @@

#define OXT_BACKTRACE_IS_ENABLED

#include <boost/thread/mutex.hpp>
#include <boost/current_function.hpp>
#include <exception>
#include <string>
#include <list>
#include <vector>
#include "../spin_lock.hpp"
#include "../macros.hpp"

namespace oxt {

using namespace std;
using namespace boost;
struct trace_point;
class tracable_exception;
struct thread_registration;

extern boost::mutex _thread_registration_mutex;
extern list<thread_registration *> _registered_threads;

void _init_backtrace_tls();
void _finalize_backtrace_tls();
bool _get_backtrace_list_and_its_lock(vector<trace_point *> **backtrace_list, spin_lock **lock);
string _format_backtrace(const list<trace_point *> *backtrace_list);
string _format_backtrace(const vector<trace_point *> *backtrace_list);

/**
* A single point in a backtrace. Creating this object will cause it
* to push itself to the thread's backtrace list. This backtrace list
Expand All @@ -61,96 +39,21 @@ string _format_backtrace(const vector<trace_point *> *backtrace_list);
* backtrace list.
*
* Except if you set the 'detached' argument to true.
*
* Implementation detail - do not use directly!
* @internal
*/
struct trace_point {
const char *function;
const char *source;
unsigned int line;
bool m_detached;

trace_point(const char *function, const char *source, unsigned int line) {
this->function = function;
this->source = source;
this->line = line;
m_detached = false;

vector<trace_point *> *backtrace_list;
spin_lock *lock;
if (OXT_LIKELY(_get_backtrace_list_and_its_lock(&backtrace_list, &lock))) {
spin_lock::scoped_lock l(*lock);
backtrace_list->push_back(this);
}
}

trace_point(const char *function, const char *source, unsigned int line, bool detached) {
this->function = function;
this->source = source;
this->line = line;
m_detached = true;
}

~trace_point() {
if (OXT_LIKELY(!m_detached)) {
vector<trace_point *> *backtrace_list;
spin_lock *lock;
if (OXT_LIKELY(_get_backtrace_list_and_its_lock(&backtrace_list, &lock))) {
spin_lock::scoped_lock l(*lock);
backtrace_list->pop_back();
}
}
}

void update(const char *source, unsigned int line) {
this->source = source;
this->line = line;
}
trace_point(const char *function, const char *source, unsigned int line);
trace_point(const char *function, const char *source, unsigned int line, bool detached);
~trace_point();
void update(const char *source, unsigned int line);
};

#define TRACE_POINT() oxt::trace_point __p(BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)
#define TRACE_POINT_WITH_NAME(name) oxt::trace_point __p(name, __FILE__, __LINE__)
#define UPDATE_TRACE_POINT() __p.update(__FILE__, __LINE__)

/**
* @internal
*/
struct thread_registration {
string name;
spin_lock *backtrace_lock;
vector<trace_point *> *backtrace;
};

/**
* @internal
*/
struct initialize_backtrace_support_for_this_thread {
thread_registration *registration;
list<thread_registration *>::iterator it;

initialize_backtrace_support_for_this_thread(const string &name) {
_init_backtrace_tls();
registration = new thread_registration();
registration->name = name;
_get_backtrace_list_and_its_lock(&registration->backtrace,
&registration->backtrace_lock);

boost::mutex::scoped_lock l(_thread_registration_mutex);
_registered_threads.push_back(registration);
it = _registered_threads.end();
it--;
}

~initialize_backtrace_support_for_this_thread() {
{
boost::mutex::scoped_lock l(_thread_registration_mutex);
_registered_threads.erase(it);
delete registration;
}
_finalize_backtrace_tls();
}
};

} // namespace oxt

Loading

0 comments on commit 45c11ef

Please sign in to comment.