Posts

Linux USB Device Driver Part 3 - Registering a USB Driver with Linux

#include <linux/kernel.h> #include <linux/module.h> #include <linux/usb.h> MODULE_LICENSE("GPL"); #define USB_VENDOR_ID  0x18ec #define USB_PRODUCT_ID 0x3299 static const struct usb_device_id usb_table[] = {     { USB_DEVICE(USB_VENDOR_ID, USB_PRODUCT_ID) },     { } }; MODULE_DEVICE_TABLE(usb, usb_table); static int usb_probe(struct usb_interface *interface,                         const struct usb_device_id *id) {     dev_info(&interface->dev, "USB Driver Probed: Vendor ID:%02x\t"              "Product ID:%02x\n", id->idVendor, id->idProduct);     return 0; } static void usb_disconnect(struct usb_interface *interface) {     dev_info(&interface->dev, "USB Driver Disconected\n"); } static struct usb_driver usb_hello_driver = {     .name = "hello",     ....

Debugging Linux Kernel Using SystemTap Part-4 - Wildcards

Image
Syntax to probe for all function entries and exits of a single file: probe kernel.function("*@filename").call { .... } probe kernel.function("*@filename").return { ... } Example: Let's probe all the functions of kernel/signal.c probe kernel.function("*@kernel/signal.c").call {     printf("%s\n", ppfunc()); } probe kernel.function("*@kernel/signal.c").return {     printf("%s\n", ppfunc()); } ppfunc() prints the function name which is probed. Partial O/P: Wildcards in filename: We can also put wildcards in the file name: E.g.: probe kernel.function("*@kernel/sys*.c") { printf("%s\n", ppfunc()) } will probe for all  the functions present in kernel/sys.c, kernel/sysctl.c, kernel/sysctl_binary.c Wildcard in Function Names: probe kernel.function("*open*) { printf("%s\n", ppfunc()) } will probe for all the open functions.

Debugging Linux Kernel using SystemTap Part5 - Writing Probes for Module Functions

Image
In the previous posts, we wrote probes for the kernel functions, what if your probe functions are inside a loadable module. Syntax for probing a function inside module: probe module("module_name").function("function_name") { ..... } Example of probing all the get functions inside e1000 module: probe module("e1000").function("e1000_get*") {     printf("%s\n", ppfunc()) } probe module("e1000").function("e1000_get*").return {     printf("%s \n", ppfunc()) }

Debugging Linux Kernel Using SystemTap Part3 - call and return

Image
To probe a function at the entrance ( call the handler ) add the .call at the end of the probe , and to probe the function at the exit add the .return at the end of the probe. Syntax: probe kernel.function("function_name").call { ... } probe kernel.function("function_name").return { ... } Example: probe kernel.function("do_sys_open").call{     printf("File %s\n", user_string($filename)) } probe kernel.function("do_sys_open").return{     printf("Return %d\n", $return) } O/P:

Debugging Linux Kernel using SystemTap Part-2 - Writing Probes for Kernel Functions

Image
What is Probe in SystemTap? Probe means to aggregate or print debug information at specific points in the executable code. Syntax of probing a function in the kernel: probe kernel.function("function_name") { ..... } E.g. Let's probe open function and print the name of the file opened probe kernel.function("do_sys_open") {     printf("File Name:%s\n", user_string($filename)); } To find out the list of all commands, run the following command: $ stap -l 'kernel.function("*")' | sort To find out the arguments of a kernel function $ stap -L 'kernel.function("*")' | grep do_sys_open You can see in the figure, it lists all the local variables of the do_sys_function dfd -> int, $filename-> char const*, $flags->int, $mode-> umode_t, $op -> struct open_flags. You can print each of these values in the probe function

Debugging Linux Kernel using SystemTap Part1 - What, Purpose, Installation, Working, Hello World

Image
What is SystemTap? SystemTap is command line utility which allows kernel developers to investigate the behavior of the kernel and user-space applications. In order to debug or analyze kernel space, developers need to modify, compile, install and reboot. With SystemTap, you can debug the kernel without the need to rebuild and install. What is the purpose of SystemTap? SystemTap provides a powerful way for understanding what logic is happening and what variables and memory locations are changing. Used when there is no interactive debugger. Main purpose of SystemTap is to trace events. Examples of events: Function Entering and Exiting Timer Expiring Network Packet Reception Whenever any registered events occur, SystemTap calls the user handler, which can collect and display Function Parameters Values of Variables Contents of Memory Locations etc Installation of SystemTap: It's better we install SystemTap from source code, as I faced a lot of issues whe...

Setting non-standard baud rate to Serial Port

Using stty, we can only set standard baud rates such as 9600, 19200, 38400, 57600 and so on using the following command: stty -F /dev/ttyUSB0 115200 stty does not allow us to set non-standard baud rate , for example 7667. stty -F /dev/ttyUSB0 7667 We will get the following error: "stty: invalid argument '7667' We can set a custom baud rate using new style termios2 struct with setting BOTHER flag in c_flag member of termios2 struct. Sample Code for setting custom baud rate #include <stdio.h> #include <fcntl.h> #include <errno.h> #include <asm/termios.h> int main() {     int fd, ret;     struct termios2 config;     fd = open("/dev/ttyUSB0", O_RDWR);     if (fd < 0)         printf("Failed to open /dev/ttyUSB0 - fd = %d\n", fd);     // Set custom buad rate     ret = ioctl(fd, T...