0

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

3 Answers3

0

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
  • 4,417
  • 28
  • 28
0

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.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
-1

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

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • 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 Sep 10 '10 at 13:24
  • You need at linking time as well: https://sourceware.org/binutils/docs-2.16/gprof/Compiling.html#Compiling – brokenfoot Apr 22 '18 at 17:37