Skip to content

Commit

Permalink
base/backtrace: Add a generic backtrace generation library
Browse files Browse the repository at this point in the history
Don't print the backtrace function in the backtrace
  • Loading branch information
cmaloney committed Aug 20, 2014
1 parent 90bef82 commit a271d5d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
40 changes: 40 additions & 0 deletions base/backtrace.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* <base/backtrace.cc>
Copyright 2010-2014 OrlyAtomics, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <base/backtrace.h>

#include <execinfo.h>

#include <base/as_str.h>

using namespace Base;

void Base::GenBacktrace(int max_frame_count, const std::function<bool (const std::string &)> &cb) {
void *frames[max_frame_count+1];
int frame_count = backtrace(frames, max_frame_count+1);
char **symbols = backtrace_symbols(frames, frame_count);
for (int frame_idx = 1; frame_idx < frame_count; ++frame_idx) {
auto frame_print = AsStr('[', frame_idx, '/', frame_count-1, "][");
if (symbols) {
frame_print += symbols[frame_idx];
} else {
frame_print += AsStr(frames[frame_idx]);
}
if (!cb(frame_print)) {
return;
}
}
}
29 changes: 29 additions & 0 deletions base/backtrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* <base/backtrace.h>
Print a backtrace
Copyright 2010-2014 OrlyAtomics, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include <functional>
#include <string>

namespace Base {

/* Generate a backtrace one line at a time, calling the callback once for each backtrace frame. */
void GenBacktrace(int max_frame_count, const std::function<bool (const std::string &)> &cb);

} // Base
4 changes: 2 additions & 2 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $CC -o tools/jhm
jhm/env.cc jhm/jobs/compile_c_family.cc util/error.cc jhm/status_line.cc io/input_producer.cc \
jhm/work_finder.cc base/fd.cc base/pump.cc util/io.cc base/split.cc jhm/config.cc jhm/jobs/link.cc \
jhm/jobs/bison.cc strm/syntax_error.cc jhm/jobs/nycr.cc base/dir_walker.cc jhm/jobs/dep.cc util/time.cc \
strm/out.cc base/event_semaphore.cc strm/in.cc strm/past_end.cc base/unreachable.cc base/path.cc \
strm/out.cc base/event_semaphore.cc strm/in.cc strm/past_end.cc base/unreachable.cc base/path.cc base/backtrace.cc \
-I./ -DSRC_ROOT=\"`pwd`\" \
-msse2 -pthread

Expand All @@ -44,7 +44,7 @@ $CC -o tools/make_dep_file
"${common_flags[@]}" \
strm/bin/var_int.cc strm/syntax_error.cc strm/out.cc strm/past_end.cc strm/in.cc strm/bin/in.cc \
util/io.cc base/demangle.cc base/code_location.cc base/event_semaphore.cc util/error.cc base/unreachable.cc \
jhm/make_dep_file.cc base/thrower.cc base/fd.cc base/split.cc base/subprocess.cc base/pump.cc \
jhm/make_dep_file.cc base/thrower.cc base/fd.cc base/split.cc base/subprocess.cc base/pump.cc base/backtrace.cc \
server/daemonize.cc \
-I./ -DSRC_ROOT=\"`pwd`\" \
-msse2 -pthread
Expand Down
18 changes: 5 additions & 13 deletions server/daemonize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
#include <initializer_list>

#include <signal.h>
#include <execinfo.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/stat.h>

#include <base/backtrace.h>
#include <util/error.h>

using namespace std;
Expand Down Expand Up @@ -61,18 +61,10 @@ static void InstallSignalHandlers(initializer_list<int> signals, void (*handler)
}

void Server::BacktraceToLog() {
static const int max_frame_count = 100;
void *frames[max_frame_count];
int frame_count = backtrace(frames, max_frame_count);
char **symbols = backtrace_symbols(frames, frame_count);
if (symbols) {
for (int frame_idx = 0; frame_idx < frame_count; ++frame_idx) {
syslog(LOG_ERR, "[backtrace][frame %d of %d][%s]", frame_idx + 1, frame_count, symbols[frame_idx]);
}
free(symbols);
} else {
syslog(LOG_ERR, "[backtrace][failed to get %d frames]", frame_count);
}
Base::GenBacktrace(100, [](const std::string &msg) {
syslog(LOG_ERR, "[backtrace]%s", msg.c_str());
return true;
});
}

pid_t Server::Daemonize() {
Expand Down

0 comments on commit a271d5d

Please sign in to comment.