在android系统中C++层可通过以下接口输出调用栈:
android::CallStack stack;
stack.update();
stack.dump("");
但是如果在调用C层(linux用户空间)代码,如果也想使用C++层CallStack类的接口来输出调用栈,则需要做出以下修改:
1:修改CallStack源文件
主要是将相关接口封装成一个C程序可调用的函数,下面将其封装成了void c_dump_statck(char * tag)
frameworks/native/libs/utils/CallStack.cpp
index 18fd84f..5ad2455 100644
--- a/libs/utils/CallStack.cpp
+++ b/libs/utils/CallStack.cpp
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
+#ifdef __cplusplus
#define LOG_TAG "CallStack"
#include <string.h>
@@ -104,7 +104,7 @@ void CallStack::dump(const char* prefix) const {
char line[MAX_BACKTRACE_LINE_LENGTH];
format_backtrace_line(i, &mStack[i], &symbols[i],
line, MAX_BACKTRACE_LINE_LENGTH);
- ALOGD("%s%s", prefix, line);
+ ALOGE("%s%s", prefix, line);
}
free_backtrace_symbols(symbols, mCount);
}
@@ -127,3 +127,9 @@ String8 CallStack::toString(const char* prefix) const {
}
}; // namespace android
+#endif
+extern "C" void c_dump_statck(char* tag){
+ android::CallStack stack;
+ stack.update();
+ stack.dump(tag);
+}
2:修改头文件,防止在编译C程序时,编译不过,因此使用__cplusplus来隔离C++内容
frameworks/native/include/utils/CallStack.h
diff --git a/include/utils/CallStack.h b/include/utils/CallStack.h
index 079e20c..d23d5ed 100644
--- a/include/utils/CallStack.h
+++ b/include/utils/CallStack.h
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
+#ifdef __cplusplus
#ifndef ANDROID_CALLSTACK_H
#define ANDROID_CALLSTACK_H
@@ -69,6 +69,7 @@ private:
}; // namespace android
-// ---------------------------------------------------------------------------
+// --------------------------------------------------------------------------
#endif // ANDROID_CALLSTACK_H
+#endif
3:重新编译CallStack所在的动态库,这里是libutils.so,产生新库后,将其push下系统中。
4:使用void c_dump_statck(char * tag)
现在假设要在external/bluetooth/bluedroid/ 目录下的文件中使用访函数
a:修改Android.mk
diff --git a/main/Android.mk b/main/Android.mk
index 8ecb3c3..cd200ec 100644
--- a/main/Android.mk
+++ b/main/Android.mk
@@ -115,8 +115,8 @@ LOCAL_SHARED_LIBRARIES := \
libcutils \
libpower \
libbt-hci \
libbt-utils
+ libutils
b:调用
在相关的C源文件中,使用以下方式调用:
extern void c_dump_statck(char *);
c_dump_statck("uyiwfn");