我的Android应用程序(Java + NDK)报告由0xdeadd00d引起的许多崩溃(信号)。众所周知,它是Dalvik VM中止的简写。这些都不会发生在我的JNI调用中,但是信号处理发生在本地世界中。但是本机崩溃报告通常报告的内容(寄存器,堆栈)对于调试它们没有用处。
有没有办法从本机信号处理程序进入Java调用堆栈?或者转储一部分最近的logcat活动?
答案 0 :(得分:0)
解决方案:在信号处理程序中为deadd00d引入一个特殊条件。读取logcat的最后1024个字节,将其插入崩溃日志中。
我正以这种方式得到一些不错的诊断。
//n is the length of the file tail to read
//Returns # of byte read
size_t ReadLogcat(char *Buf, size_t n)
{
char s[sizeof(s_DataPath) + 30];
//s_DataPath contains the result of Context.getDir("Data")
strcpy(s, "logcat -d -f ");
char *fn = s + strlen(s);
strcat(s, s_DataPath);
strcat(s, "/logcat"); //Temp file for the logcat
size_t r = 0;
system(s); //Dump the snapshot of the logcat into the file called logcat
FILE *f = fopen(fn, "r");
if(f)
{
fseek(f, -n, SEEK_END); //We need the end of the file
r = fread(Buf, 1, n, f);
fclose(f);
}
unlink(fn); //Don't need the logcat file anymore
return r;
}
static void DumpLogcat(FILE *f)
{
char Buf[1024];
size_t r;
if(r = ReadLogcat(Buf, sizeof(Buf)))
{
fputs("\nLogcat:\n", f);
fwrite(Buf, 1, r, f);
}
}
void OnSignal(int signal, siginfo_t *si, void *p)
{
FILE *f = OpenCrashLog(); //opens a file in the data dir; details are irrelevant
//Lots of data dumping here... Version, stack, registers, etc.
//...
if((unsigned)si->si_addr == 0xdeadd00d) //Try and dump some Logcat
DumpLogcat(f);
}