2
votes

I'm new to device drivers in Linux. And my first day task is to debug driver using GDB in Linux.

  • I need to debug some XYZ (PCIe device driver supports ethernet) device driver to know about the flow and what is going on device's registers and all.

I have installed the driver with patch file and insmod command.

The device is working properly. But am not getting any solution to debug the device driver.

All I know is that how to debug C program using GDB in Linux(fedora20). I got one link similar to my Problem but from that also I have not got any knowledge.

Can anybody please share your thoughts that how can I start from scratch. I am very specific to learn about Debugging device drivers in Linux. Especially that init or probe function inside my driver I need to know the flow.

2
What kind of device are you coding your Linux driver for? Please edit your question to improve it, if possible show some minimal reproducible example (or at least give a link to your code).Basile Starynkevitch

2 Answers

4
votes

The gdb debugger is useful to debug user-space application level programs (since it uses ptrace(2)).

For kernel code, things are different. Consider using kgdb (I don't know the details). You might also add debug prints ....

I recommend at least reading more about operating systems, e.g. Operating Systems: Three Easy Pieces (freely downloadable), and reading something about Linux programming (perhaps the old ALP, and also intro(2), syscalls(2) and related stuff). Don't dare coding Linux loadable kernel modules without good familiarity with Linux programming (in user-land). See also kernelnewbies.

BTW, you should prefer writing user-land code than kernel modules. A very simple rule of thumb is to avoid writing kernel code when possible.

2
votes

To begin with, you may need to understand basics of device driver and kernel in linux. Subsequently focus as per the type of driver in-hand. You also need to understand the functionality (specification / manual / datasheet) of the device you are working on.

The very basic approach for debugging can be using printk. Normally there will be debug logs that can be enabled through compilation flags. If it is present, enable it so that it can give important pointers else you might need to add it on your own.

Start with verification of driver registration and verification of loading of your driver (static or loadable module as per your requirement). Check whether it is getting listed as part of sysfs or proc as applicable. Check whether probe is successful and subsequently the appropriate read/write/open/close/other calls as per your driver / device functionality.

The dmesg shall be very helpfull for viewing the kernel messages. There are also tools like kdb, LTT, strace that can be useful as per the scenario.