Allow calling GetFunctionName before unwinding.
Some extra initialization needs to occur before calling the local/remote
GetFunctioneName.
Also, cleanup the test code a bit:
- Make all local functions static.
- Create a common function to create and finish remote processes.
- Remove unused function.
- Fix the formatting a bit by making it match clang format.
Test: Ran the unit tests.
Change-Id: I998bed1378d582df59fdf9263df6f208db5d795a
diff --git a/libbacktrace/UnwindPtrace.cpp b/libbacktrace/UnwindPtrace.cpp
index 306d2ac..5c73bd6 100644
--- a/libbacktrace/UnwindPtrace.cpp
+++ b/libbacktrace/UnwindPtrace.cpp
@@ -37,6 +37,7 @@
_UPT_destroy(upt_info_);
upt_info_ = nullptr;
}
+
if (addr_space_) {
// Remove the map from the address space before destroying it.
// It will be freed in the UnwindMap destructor.
@@ -47,18 +48,14 @@
}
}
-bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
- if (GetMap() == nullptr) {
- // Without a map object, we can't do anything.
- error_ = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
- return false;
+bool UnwindPtrace::Init() {
+ if (upt_info_) {
+ return true;
}
- error_ = BACKTRACE_UNWIND_NO_ERROR;
-
- if (ucontext) {
- BACK_LOGW("Unwinding from a specified context not supported yet.");
- error_ = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
+ if (addr_space_) {
+ // If somehow the addr_space_ gets initialized but upt_info_ doesn't,
+ // then that indicates there is some kind of failure.
return false;
}
@@ -79,6 +76,28 @@
return false;
}
+ return true;
+}
+
+bool UnwindPtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
+ if (GetMap() == nullptr) {
+ // Without a map object, we can't do anything.
+ error_ = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
+ return false;
+ }
+
+ error_ = BACKTRACE_UNWIND_NO_ERROR;
+
+ if (ucontext) {
+ BACK_LOGW("Unwinding from a specified context not supported yet.");
+ error_ = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
+ return false;
+ }
+
+ if (!Init()) {
+ return false;
+ }
+
unw_cursor_t cursor;
int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
if (ret < 0) {
@@ -115,10 +134,10 @@
prev->stack_size = frame->sp - prev->sp;
}
- frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
-
FillInMap(frame->pc, &frame->map);
+ frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
+
num_frames++;
} else {
num_ignore_frames--;
@@ -130,6 +149,10 @@
}
std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
+ if (!Init()) {
+ return "";
+ }
+
*offset = 0;
char buf[512];
unw_word_t value;