如何编译要使用 Gprof 分析的驱动程序

Posted

技术标签:

【中文标题】如何编译要使用 Gprof 分析的驱动程序【英文标题】:How to compile driver to be profiled with Gprof 【发布时间】:2010-09-10 13:16:50 【问题描述】:

简单的问题..如何编译驱动程序以使用 Gprof 进行分析?

我当前的 Makefile:

obj-m += hello-2.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

工作正常(我可以加载驱动程序等),但如果我尝试将 -pg 选项添加到文件中,则会出现错误。

生成文件:

obj-m += hello-2.o

EXTRA_CFLAGS += -pg
LDFLAGS += -pg

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

我得到错误:

make -C /lib/modules/2.6.31/build M=/home/I/drivertest modules
make[1]: Entering directory `/home/I/linux-2.6.31'
  CC [M]  /home/I/drivertest/hello-2.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "mcount" [/home/I/drivertest/hello-2.ko] undefined!
  CC      /home/I/drivertest/hello-2.mod.o
  LD [M]  /home/I/drivertest/hello-2.ko
ld: unrecognized option '-pg'
ld: use the --help option for usage information
make[2]: *** [/home/I/drivertest/hello-2.ko] Error 1
make[1]: *** [modules] Error 2
make[1]: Leaving directory `/home/I/linux-2.6.31'
make: *** [all] Error 2

【问题讨论】:

【参考方案1】:

您不能使用 gprof 分析内核模块。您需要编译启用了分析支持的内核并使用readprofile 工具。有关详细信息,请参阅the Linux kernel documentation。

【讨论】:

【参考方案2】:

即使您可以在内核模块上使用 gprof,它也从未被宣传为能够帮助您定位瓶颈。 More on that.

【讨论】:

【参考方案3】:

-pg 是编译标志,而不是链接标志。从LDFLAGS 中删除它(显然将其留在EXTRA_CFLAGS 中)。

【讨论】:

它消除了 ld 问题,但 mcount 仍然未定义。根据 gprof 文档 -pg 标志也必须设置为链接器。 链接时也需要:sourceware.org/binutils/docs-2.16/gprof/…

以上是关于如何编译要使用 Gprof 分析的驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

在 g++ 中为 gprof 编译

[转]程序进行性能分析工具gprof使用入门

使用 gprof 进行奇怪的分析输出

让 gprof 从 Qt 调试应用程序读取 gmon.out 输出时出现问题

GNU profiler(gprof)使用

GNU profiler(gprof)使用