1. How to print the call stack at the Java layer?
When optimizing the android startup process, colleagues provided a function to print the call stack. Share
Java. util. Map <thread, stacktraceelement []> TS = thread. getallstacktraces ();
Stacktraceelement [] ste = ts. Get (thread. currentthread ());
For (stacktraceelement S: STE ){
Android. util. slog. E ("SS", S. tostring (); // This is built-in to Android. If not, use other print functions.
}
To print all Java applications started by zygote at Android startup. In
// Frameworks/base/services/Java/COM/Android/Server/AM/activitymanagerservice. Java
Private final void startprocesslocked (processrecord app, string hostingtype, string hostingnamestr ){
// Debug add
Java. util. Map <thread, stacktraceelement []> TS = thread. getallstacktraces ();
Stacktraceelement [] ste = ts. Get (thread. currentthread ());
For (stacktraceelement S: STE ){
Android. util. slog. E ("SS", S. tostring ());
}
}
You can use the logcat command in the terminal to view the printed call stack.
2. How do I print the call stack on the C/C ++ layer?
Java can easily print the function call stack. C/C ++.
To show in VM/Misc. cpp who calls the dvmallocregion function, add the following red color to this functionCode:
# Include <utils/callstack. h>
...
Void * dvmallocregion (size_t bytecount, int Prot, const char * name)
{
...
# Ifdef _ arm _
Logw ("name = % s", name );
Android: callstack stack;
Stack. Update (1,100 );
Stack. Dump ("");
# Endif
...
}
In VM/DVM. mk, add:
Local_cflags + =-d_arm _
Local_shared_libraries + = libutils
"Mmm Dalvik" to obtain libdvm. So. Then
ADB Root
ADB remount
ADB push libdvm. So/system/lib
ADB reboot
After the system is restarted, the new libdvm. So will be put into use.