-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
base/backtrace: Add a generic backtrace generation library
Don't print the backtrace function in the backtrace
- Loading branch information
Showing
4 changed files
with
76 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters