- 论坛徽章:
- 0
|
在kernel panic的时候总会输出类似于:
Kernel panic - not syncing: print to file test! XXX
XXX部分一般是具体哪个模块crash了或者指示crash的类型。所以这句信息对判断panic的定位就很关键。
那么怎么导出这行信息?
有一种做法是导出到文件,因为现在kernel中也可以发生文件操作,就是通过filp_open(),get_fs(),set_fs()等函数来完成。关于这个的测试程序网上有很多,可以参考。
于是我在panic()函数加入了对文件读写的操作,代码如下:
void panic(const char *fmt, ...)
{
static DEFINE_SPINLOCK(panic_lock);
static char buf[1024];
va_list args;
long i, i_next = 0;
int state = 0;
//for printing panic to file
char sbuf[512];
struct file *fp = NULL;
mm_segment_t old_fs;
//print panic to file
sprintf(sbuf,"Kernel panic - not syncing: %s\n",buf);
printk("kern panic - filp_open\n");
if(fp == NULL)
fp = filp_open(INFO_FILE, O_RDWR | O_CREAT, 0644);
if (IS_ERR(fp)) {
printk(KERN_EMERG "error occured while opening file %s, exiting...\n", INFO_FILE);
fp = NULL;
goto next;
}
printk("kern panic - open file success, fp: %p\n",fp);
printk("kern panic - write info to file\n");
old_fs = get_fs();
set_fs(KERNEL_DS);
fp->f_op->write(fp, (char *)sbuf, sizeof(sbuf), &fp->f_pos);
set_fs(old_fs);
filp_close(fp, NULL);
printk("kern panic - filp_close\n");
next:
trace_kernel_panic(0);
/*
* Disable local interrupts. This will prevent panic_smp_self_stop
* from deadlocking the first cpu that invokes the panic, since
* there is nothing to prevent an interrupt handler (that runs
* after the panic_lock is acquired) from invoking panic again.
*/
local_irq_disable();
/*
* It's possible to come here directly from a panic-assertion and
* not have preempt disabled. Some functions called from here want
* preempt to be disabled. No point enabling it later though...
*
* Only one CPU is allowed to execute the panic code from here. For
* multiple parallel invocations of panic, all other CPUs either
* stop themself or will wait until they are stopped by the 1st CPU
* with smp_send_stop().
*/
if (!spin_trylock(&panic_lock))
panic_smp_self_stop();
console_verbose();
bust_spinlocks(1);
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
pr_emerg("Kernel panic - not syncing: %s\n", buf);
...
}
上述红色部分是我添加的,其中INFO_FILE是"/data/log-panic"。经过测试发现整个调试信息打印都正常,但是在/data目录下就是没有生成log-panic文件。
有一种可能就是在读写文件之前,data分区没有挂载好。于是我写了一个驱动模块,在驱动中panic,手动加载模块,这样系统就能完全启动好。但是在/data目录下还是没能生成log-panic文件。
测试的log如下:
[ 69.383373] kern panic - filp_open
[ 69.386312] kern panic - open file success, fp: ffffffc0b8354480
[ 69.386325] kern panic - write info to file
[ 69.390874] kern panic - filp_close
[ 69.393521] Kernel panic - not syncing: print to file test!
问题: 如果分区加载都正常,文件的打开、读写、关闭都顺利,为什么分区中没有生成相应的文件呢? |
|