addr2line

Posted Li-Yongjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了addr2line相关的知识,希望对你有一定的参考价值。

示例

file.c

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#define FILENAME "/tmp/kernel_file"

static char buf[] = "5555\\n";
static char buf1[10] = 0;

int hello_init(void)

	struct file *filp;
	loff_t pos;
	int *a = NULL;

	printk("hello enter\\n");

	*a = 100;

	filp = filp_open(FILENAME, O_RDWR | O_CREAT, 0644);
	if (IS_ERR(filp)) 
		printk("create file error\\n");
		return -1;
	

	pos = 0;
	kernel_write(filp, buf, sizeof(buf), &pos);

	pos = 0;
	kernel_read(filp, buf1, sizeof(buf1) - 1, &pos);
	printk("read: %s", buf1);

	filp_close(filp, NULL);

	return 0;

void hello_exit(void)

	printk("hello exit\\n");


module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

Makefile

obj-m:=file.o  
  
KDIR=/home/liyongjun/project/board/buildroot/Vexpress_4.15/build/linux-4.15.1/
CROSS_COMPILE=/home/liyongjun/project/board/buildroot/Vexpress_4.15/host/bin/arm-linux-

all:
	make -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) modules

clean:
	make -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) clean

安装

# insmod file.ko 
file: loading out-of-tree module taints kernel.
hello enter
Unable to handle kernel NULL pointer dereference at virtual address 00000000
pgd = 173dcbfe
[00000000] *pgd=00000000
Internal error: Oops: 805 [#1] SMP ARM
Modules linked in: file(O+)
CPU: 0 PID: 843 Comm: insmod Tainted: G           O     4.15.1 #1
Hardware name: ARM-Versatile Express
PC is at init_module+0x20/0xbc [file]
LR is at init_module+0x18/0xbc [file]
pc : [<7f000020>]    lr : [<7f000018>]    psr: 60080013
sp : 8faa1e88  ip : 00000000  fp : 76f3a000
r10: 00000000  r9 : 8faa0000  r8 : 801075e4
r7 : 00000000  r6 : 8fab1400  r5 : 7f000000  r4 : 00000000
r3 : 00000064  r2 : 000001a4  r1 : 0f452000  r0 : 0000000b
Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment none
Control: 10c5387d  Table: 6faf0059  DAC: 00000051
Process insmod (pid: 843, stack limit = 0x9b014135)
Stack: (0x8faa1e88 to 0x8faa2000)
1e80:                   7f002040 801938f0 00013f88 ffffe000 7f000000 8fab1400
1ea0: 00000000 80101ad8 8081cba9 807c0770 807c077c 80a03dc0 00013f88 00000000
1ec0: 00000000 8023ab24 8f9e6240 00000000 00006c65 00000000 00000000 00000000
1ee0: 00000000 00000000 00000000 00000000 7f002040 7f002040 00000003 8fab1400
1f00: 0000017b 801075e4 8faa0000 80194a0c 00000003 0053e190 0000017b 00000000
1f20: 00000003 0053e190 0000017b 8019740c 7fffffff 00000000 00000003 00000000
1f40: 8fb0f000 912e1000 00013f88 00000000 912e119a 912e1000 00013f88 912f4a24
1f60: 912f48b8 912f06d0 00003000 00003060 00000000 00000000 00000000 000004b8
1f80: 0000001f 00000020 0000000b 00000000 00000009 00000000 24f7d400 ffffffff
1fa0: 00000062 80107400 24f7d400 ffffffff 00000003 0053e190 00000000 7ef86f61
1fc0: 24f7d400 ffffffff 00000062 0000017b 00000000 00465020 76f39d20 76f3a000
1fe0: 7ef86ca0 7ef86c90 00480170 76e50de0 20080010 00000003 00000000 00000000
[<7f000020>] (init_module [file]) from [<80101ad8>] (do_one_initcall+0x44/0x174)
[<80101ad8>] (do_one_initcall) from [<80194a0c>] (do_init_module+0x64/0x290)
[<80194a0c>] (do_init_module) from [<8019740c>] (SyS_finit_module+0xa4/0xbc)
[<8019740c>] (SyS_finit_module) from [<80107400>] (ret_fast_syscall+0x0/0x54)
Code: e24dd00c eb458dd2 e3a03064 e3a02f69 (e5843000) 
---[ end trace a36b30dc06a470f0 ]---
Segmentation fault

报错,主要看这行信息

PC is at init_module+0x20/0xbc [file]

addr2line

使用 addr2line 定位代码位置

$ /home/liyongjun/project/board/buildroot/Vexpress_4.15/host/bin/arm-buildroot-linux-gnueabihf-addr2line -f -e file.ko 0x20
hello_init
/home/liyongjun/project/board/buildroot/override/Vexpress_4.15/file/file.c:19

file.c:19

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/uaccess.h>

#define FILENAME "/tmp/kernel_file"

static char buf[] = "5555\\n";
static char buf1[10] = 0;

int hello_init(void)

	struct file *filp;
	loff_t pos;
	int *a = NULL;

	printk("hello enter\\n");

	*a = 100;	// 出错位置

找到出错代码位置。

以上是关于addr2line的主要内容,如果未能解决你的问题,请参考以下文章