views:

50

answers:

3

Simple question.. how to compile driver to be profiled with Gprof?

My current 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

Works just fine ( I can load the driver etc. ), but if I try to add -pg options to the file then I get an error.

Makefile:

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

I get error:

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
A: 

-pg is a compilation flag, not a linkage flag. Remove it from LDFLAGS (and obviously leave it in EXTRA_CFLAGS).

Yuval A
It removes the ld problem, but the mcount is still undefined. According to gprof documentation -pg flag must be set to linker also.
Teemu Piiroinen
See @llasram's answer.
Yuval A
A: 

You can't profile a kernel module with gprof. You'll need to compile a kernel with profiling support enabled and use the readprofile tool. See the the Linux kernel documentation for more details.

llasram
Ok.. thank you.
Teemu Piiroinen
A: 

Even if you can use gprof on a kernel module, it was never advertised as being able to help you locate bottlenecks. More on that.

Mike Dunlavey