• debian下使用dynamic printk分析usb转串口驱动执行流程


    看了一篇文章《debug by printing》,文中提到了多种通过printk来调试驱动的方法,其中最有用的就是"Dynamic debugging"。

    “Dynamic debugging"的官方文档:http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/dynamic-debug-howto.txt?id=HEAD

    "Dyanmic debugging"的功能适用于所有的linux内核,所以也可以用于嵌入式开发板中调试驱动程序。

    在使用"Dynamic debugging"前需要在内核中使能Dynamic printk选项(可用于控制代码代码中的pr_debug和dev_dbg的信息是否输出)。

    默认官方debian内核中没有使能这个选项,可以参考我的博文《debian下配置dynamic printk以及重新编译内核 》重新编译和配置内核才能使用该功能。

    本文用一个usb转串口线(核心芯片是pl2303)的例子来说明如何使用"Dynamic debugging",内核版本是linux-3.2.57。

    先插入usb转串口线,得到下面的信息:

    [    8.728526] usbcore: registered new interface driver usbserial
    [    8.728669] USB Serial support registered for generic
    [    8.728720] usbcore: registered new interface driver usbserial_generic
    [    8.728722] usbserial: USB Serial Driver core
    [    8.737942] USB Serial support registered for pl2303
    [    8.737963] pl2303 5-2:1.0: pl2303 converter detected
    [    8.750530] usb 5-2: pl2303 converter now attached to ttyUSB0
    [    8.750565] usbcore: registered new interface driver pl2303
    [    8.750568] pl2303: Prolific PL2303 USB to serial adaptor driver

    pl2303驱动中用到了pl2303的驱动,查看pl2303驱动信息:

    $ sudo modinfo pl2303
    filename:       /lib/modules/3.2.57/kernel/drivers/usb/serial/pl2303.ko
    license:        GPL
    description:    Prolific PL2303 USB to serial adaptor driver
    alias:          usb:v0B8Cp2303d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0B63p6530d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11ADp0001d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v054Cp0437d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v04B8p0522d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v04B8p0521d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v03F0p3524d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v5372p2303d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v05ADp0FBAd*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v07AAp002Ad*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11F6p2001d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v058Fp9720d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v050Dp0257d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0731p2003d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0E55p110Bd*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0413p2101d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v079Bp0027d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v10B5pAC70d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v078Bp1234d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0745p0001d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v04A5p4027d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11F5p0005d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11F5p0004d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11F5p0003d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11F5p0001d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v04E8p8001d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v11F7p02DFd*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v6189p2068d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0731p0528d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v1453p4026d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v2478p2008d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0584pB000d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0DF7p0620d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0EBAp2080d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0EBAp1080d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v056Ep5004d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v056Ep5003d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0547p2008d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v0557p2008d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v04BBp0A0Ed*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v04BBp0A03d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp0307d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp331Ad*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp0609d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp0612d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp0611d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067BpAAA0d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067BpAAA2d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp1234d*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp04BBd*dc*dsc*dp*ic*isc*ip*
    alias:          usb:v067Bp2303d*dc*dsc*dp*ic*isc*ip*
    depends:        usbserial,usbcore
    intree:         Y
    vermagic:       3.2.57 SMP mod_unload modversions 686 
    parm:           debug:Debug enabled or not (bool)

    可以看到pl2303又依赖于usbserial和usbcore.

    下载linux内核源码(可以参考我的博文《debian下配置dynamic printk以及重新编译内核》),解压缩后进入该文件夹。

    $ grep 'pl2303' drivers/usb/serial/Makefile
    obj-$(CONFIG_USB_SERIAL_PL2303)            += pl2303.o
    $ grep 'usbserial' drivers/usb/serial/Makefile
    obj-$(CONFIG_USB_SERIAL)            += usbserial.o
    usbserial-y := usb-serial.o generic.o bus.o
    usbserial-$(CONFIG_USB_SERIAL_CONSOLE)    += console.o
    usbserial-$(CONFIG_USB_EZUSB)        += ezusb.o
    $ grep 'usbcore' drivers/usb/core/Makefile
    usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
    usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
    usbcore-y += devio.o notify.o generic.o quirks.o devices.o
    usbcore-$(CONFIG_PCI)        += hcd-pci.o
    usbcore-$(CONFIG_USB_DEVICEFS)    += inode.o
    obj-$(CONFIG_USB)        += usbcore.o

    从搜索结果可以看出:

    pl2303驱动由drivers/usb/serial/pl2303.c文件来生成的。

    usbserial驱动是由drivers/usb/serial下的usb-serial.c generic.c bus.c这三个文件来生成的。

    usbcore是由drivers/usb/core/下面的usb.c hub.c hcd.c urb.c message.c driver.c config.c file.c buffer.c sysfs.c endpoint.c

    devio.c notify.c generic.c quirks.c devices.c hcd-pci.c inode.c这些文件生成的。

    切换到root用户:

    su

    挂载debugfs:

    # mount -t debugfs none /sys/kernel/debug/

    对上面提到的所有文件使能"Dynamic debugging":

    root@debian:~# echo 'file drivers/usb/serial/pl2303.c +p' > /sys/kernel/debug/dynamic_debug/control 
    root@debian:~# echo 'file drivers/usb/serial/usb-serial.c +p' > /sys/kernel/debug/dynamic_debug/control 
    root@debian:~# echo 'file drivers/usb/serial/generic.c +p' > /sys/kernel/debug/dynamic_debug/control 
    root@debian:~# echo 'file drivers/usb/serial/bus.c +p' > /sys/kernel/debug/dynamic_debug/control 
    root@debian:~# echo 'file drivers/usb/core/usb.c +p' > /sys/kernel/debug/dynamic_debug/control^C
    root@debian:~# echo 'file drivers/usb/core/hub.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/hcd.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/urb.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/message.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/driver.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/config.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/file.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/buffer.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/sysfs.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/endpoint.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/device.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/notify.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/generic.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/quirks.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/devices.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/hci-pci.c +p' > /sys/kernel/debug/dynamic_debug/control
    root@debian:~# echo 'file drivers/usb/core/inode.c +p' > /sys/kernel/debug/dynamic_debug/control

    先拔掉usb转串口线,然后删除pl2303驱动(因为之前已经插入过usb转串口,pl2303已经加载):

    sudo rmmod pl2303

    再插入usb转串口线,在另一个控制台下执行命令:

    $ dmesg | tail -n 35
    [ 3405.314567] hub 1-0:1.0: state 7 ports 8 chg 0000 evt 0100
    [ 3405.314587] hub 1-0:1.0: port 8, status 0501, change 0001, 480 Mb/s
    [ 3405.472015] hub 1-0:1.0: debounce: port 8: total 125ms stable 100ms status 0x501
    [ 3405.528178] hub 1-0:1.0: port 8 not reset yet, waiting 50ms
    [ 3405.528197] usb usb5: usb wakeup-resume
    [ 3405.528203] usb usb5: usb auto-resume
    [ 3405.568011] hub 5-0:1.0: hub_resume
    [ 3405.568027] hub 5-0:1.0: port 2: status 0101 change 0001
    [ 3405.584034] hub 1-0:1.0: state 7 ports 8 chg 0000 evt 0100
    [ 3405.672026] hub 5-0:1.0: state 7 ports 2 chg 0004 evt 0000
    [ 3405.672041] hub 5-0:1.0: port 2, status 0101, change 0000, 12 Mb/s
    [ 3405.784018] usb 5-2: new full-speed USB device number 4 using uhci_hcd
    [ 3405.936037] usb 5-2: default language 0x0409
    [ 3405.946039] usb 5-2: udev 4, busnum 5, minor = 515
    [ 3405.946044] usb 5-2: New USB device found, idVendor=067b, idProduct=2303
    [ 3405.946049] usb 5-2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
    [ 3405.946053] usb 5-2: Product: USB-Serial Controller
    [ 3405.946057] usb 5-2: Manufacturer: Prolific Technology Inc.
    [ 3405.946151] bus: 'usb': add device 5-2
    [ 3405.946228] usb 5-2: usb_probe_device
    [ 3405.946234] usb 5-2: configuration #1 chosen from 1 choice
    [ 3405.949040] usb 5-2: adding 5-2:1.0 (config #1, interface 0)
    [ 3405.949061] bus: 'usb': add device 5-2:1.0
    [ 3405.949110] usbserial_generic 5-2:1.0: usb_probe_interface
    [ 3405.949115] usbserial_generic 5-2:1.0: usb_probe_interface - got id
    [ 3406.014058] bus: 'usb-serial': add driver pl2303
    [ 3406.014092] USB Serial support registered for pl2303
    [ 3406.014096] bus: 'usb': add driver pl2303
    [ 3406.027529] pl2303 5-2:1.0: usb_probe_interface
    [ 3406.027533] pl2303 5-2:1.0: usb_probe_interface - got id
    [ 3406.027539] pl2303 5-2:1.0: pl2303 converter detected
    [ 3406.039051] bus: 'usb-serial': add device ttyUSB0
    [ 3406.039201] usb 5-2: pl2303 converter now attached to ttyUSB0
    [ 3406.039221] usbcore: registered new interface driver pl2303
    [ 3406.039223] pl2303: Prolific PL2303 USB to serial adaptor driver

    明显多了很多调试信息,更便于跟踪实际代码的执行情况。

    下面分析插入usb转串口线时的执行情况.

    考虑“[ 3405.314567] hub 1-0:1.0: state 7 ports 8 chg 0000 evt 0100”这一行输出信息。

    我们只考虑driver/usb下执行的代码,所以只在该文件夹下搜索源代码。

    $ grep -rnHi 'chg.*evt'  drivers/usb/
    drivers/usb/core/hub.c:3586:        dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x
    ", 

     这一行代码位于hub_events函数,该函数被hub_thread调用:

    static int hub_thread(void *__unused)
    {   
         /* khubd needs to be freezable to avoid intefering with USB-PERSIST
          * port handover.  Otherwise it might see that a full-speed device
          * was gone before the EHCI controller had handed its port over to
          * the companion full-speed controller.
          */
         set_freezable();
         
         do {
             hub_events();
             wait_event_freezable(khubd_wait,
                     !list_empty(&hub_event_list) ||
                     kthread_should_stop());
         } while (!kthread_should_stop() || !list_empty(&hub_event_list));
         
         pr_debug("%s: khubd exiting
    ", usbcore_name);
         return 0;
     }   

    hub_thread是在usb_hub_init中创建的一个内核线程khubd的执行函数,这个内核线程开机后就会一直处于运行状态。

    考虑“[ 3405.314587] hub 1-0:1.0: port 8, status 0501, change 0001, 480 Mb/s”这一行输出信息:

    $ grep -rnHi 'port.*status.*change.*\n'  drivers/usb/
    drivers/usb/core/hub.c:859:                    "port %d: status %04x change %04x
    ",
    drivers/usb/core/hub.c:3329:        "port %d, status %04x, change %04x, %s
    ",
    drivers/usb/gadget/dummy_hcd.c:1753:        dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes
    ",
    drivers/usb/host/xhci-ring.c:1565:    xhci_dbg(xhci, "Port Status Change Event for port %d
    ", port_id);

    可以看出只有第两行输出信息可能和当前的输出有关系,此行代码位于hub_port_connect_change函数,

    而hub_port_connect_change被hub_events函数调用。

    考虑下一行输出“[ 3405.472015] hub 1-0:1.0: debounce: port 8: total 125ms stable 100ms status 0x501”。

    $ grep -rnHi 'debounce:'  drivers/usb/
    drivers/usb/core/hub.c:2898:        "debounce: port %d: total %dms stable %dms status 0x%x
    ",

    该行输出信息位于hub_port_debounce函数,hub_port_debounce函数又被hub_port_connect_change所调用。

    考虑下一行输出“[ 3405.528178] hub 1-0:1.0: port 8 not reset yet, waiting 50ms”。

    $ grep -rnHi 'reset yet,'  drivers/usb/
    drivers/usb/core/hub.c:2203:            "port %d not %sreset yet, waiting %dms
    ",

    这行代码位于hub_port_wait_reset函数,hub_port_wait_reset又被hub_port_reset函数调用,hub_port_reset又被hub_port_init调用,

    hub_port_init又被hub_port_connect_change函数调用。

    下一行输出“[ 3405.528197] usb usb5: usb wakeup-resume”。

    $ grep -rnHi '%sresume'  drivers/usb/core
    drivers/usb/core/hcd.c:2012:    dev_dbg(&rhdev->dev, "usb %sresume
    ",
    drivers/usb/core/hub.c:2686:        dev_dbg(&udev->dev, "usb %sresume
    ",
    drivers/usb/core/hub.c:2737:        dev_dbg(&udev->dev, "usb %sresume
    ", "wakeup-");

    查看代码后得知,与之匹配的是hub.c中2737行代码,位于usb_remote_wakeup中.

    usb_remote_wakeup在好几个地方被调用,不清楚是哪个函数调用引起的。

    下一行输出信息是:[ 3405.528203] usb usb5: usb auto-resume

    根据上面的查新结果,很可能是被usb_port_resume或者hcd_bus_resume函数调用,而这两个函数都是被generic_resume函数所调用。

    下一行输出信息:[ 3405.568011] hub 5-0:1.0: hub_resume

    没有用grep搜索到有用信息,但是很可能是执行hub_resume函数输出的,查看hub_resume定义:

     static int hub_resume(struct usb_interface *intf)
     {
         struct usb_hub *hub = usb_get_intfdata(intf);
     
         dev_dbg(&intf->dev, "%s
    ", __func__);
         hub_activate(hub, HUB_RESUME);
         return 0;
     }

    果然代码中有__func__,就是hub_resume。hub_resume没有被直接调用,它在hub_driver.resume=hub_resume中被定义。

    下一行输出信息是:[ 3405.568027] hub 5-0:1.0: port 2: status 0101 change 0001

    $ grep -rnHi 'port.*status.*change.*\n'  drivers/usb/
    drivers/usb/core/hub.c:859:                    "port %d: status %04x change %04x
    ",
    drivers/usb/core/hub.c:3329:        "port %d, status %04x, change %04x, %s
    ",
    drivers/usb/gadget/dummy_hcd.c:1753:        dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes
    ",
    drivers/usb/host/xhci-ring.c:1565:    xhci_dbg(xhci, "Port Status Change Event for port %d
    ", port_id);

    第一行搜索到的信息格式和输出信息相匹配。此行代码位于hub_activate中。而上一行中hub_resume恰好调用了hub_activate函数。

    接下来三行输出信息:

    [ 3405.584034] hub 1-0:1.0: state 7 ports 8 chg 0000 evt 0100
    [ 3405.672026] hub 5-0:1.0: state 7 ports 2 chg 0004 evt 0000
    [ 3405.672041] hub 5-0:1.0: port 2, status 0101, change 0000, 12 Mb/s

    这三行信息和最前面的两行输出信息类似,不再重复分析了。 但是这里又重新从hub_event的开头部分开始执行程序。

    下一行输出信息:[ 3405.784018] usb 5-2: new full-speed USB device number 4 using uhci_hcd

    $ grep -rnHi 'USB device number'  drivers/usb/
    drivers/usb/core/hub.c:3035:                "%s %s USB device number %d using %s
    ",
    drivers/usb/core/hub.c:3149:                        "%s SuperSpeed USB device number %d using %s
    ",

    第一行搜索到内容与输出信息完全匹配,该代码位于hub_port_init中。hub_port_init又被hub_port_connect_change调用。

    下一行输出信息:[ 3405.936037] usb 5-2: default language 0x0409

    $ grep -rnHi 'default language'  drivers/usb/
    drivers/usb/core/message.c:790:    dev_dbg(&dev->dev, "default language 0x%04x
    ",

    该行代码位于usb_get_langid中,usb_get_langid又被usb_string函数调用。调用usb_string的函数不好分析,留待以后再看。

     下一行输出信息:[ 3405.946039] usb 5-2: udev 4, busnum 5, minor = 515

    $ grep -rnHi 'udev.*busnum.*minor'  drivers/usb/
    drivers/usb/core/hub.c:1971:    dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d
    ",

    该代码位于usb_new_device中,而usb_new_device又被usb_port_connect_change函数所调用。

    而查看该行代码前,执行了usb_enumerate_device函数。该函数调用了usb_cache_string,而usb_cache_string又调用了usb_string.

    所以上一行的输出信息就是在调用usb_enumerate_device时产生的。

    接下来四行信息:

    [ 3405.946044] usb 5-2: New USB device found, idVendor=067b, idProduct=2303
    [ 3405.946049] usb 5-2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
    [ 3405.946053] usb 5-2: Product: USB-Serial Controller
    [ 3405.946057] usb 5-2: Manufacturer: Prolific Technology Inc.

     之前在我的博文《linux下无线鼠标驱动执行流程》中已经找到了执行这几行代码的函数,其名称是announce_device,该函数也被usb_new_device调用。

    下一行输出信息:[ 3405.946151] bus: 'usb': add device 5-2

    这行输出信息在函数bus_add_device(drivers/base/bus.c)被执行。这是因为我在配置"dynamic debugging"时执行了下面的命令:

    echo 'file bus.c +p' > /sys/kernel/debug/dynamic_debug/control 

    后来看没有只是打开了drivers/base/bus.c,而不是打开drivers/usb/core/bus.c的"dynamic debugging"功能,所以才添加了路径名称来保证唯一性。

    但是以前打开的drivers/base/bus.c中的"dynamic debugging"功能并没有关闭,所以这里有相关的信息显示。

    bus_add_device在device_add函数中被调用,device_add又被usb_new_device调用。

    下一行输出信息:[ 3405.946228] usb 5-2: usb_probe_device

    猜想是执行usb_probe_device时显示的信息,查看usb_probe_device函数代码发现果然有下面一行代码:

    dev_dbg(dev, "%s
    ", __func__);

    usb_probe_device可能是在device_add调用bus_probe_device时调用的(过程比较复杂,不再详述)。

     下一行输出信息:[ 3405.946234] usb 5-2: configuration #1 chosen from 1 choice

    $ grep -rnHi 'chosen from'  drivers/usb/
    drivers/usb/core/generic.c:145:            "configuration #%d chosen from %d choice%s
    ",
    drivers/usb/core/generic.c:150:            "no configuration chosen from %d choice%s
    ",

    只有第一行搜索信息与实际输出信息格式相匹配。

    该行代码位于usb_choose_configuration函数中,usb_choose_configuration函数又被generic_probe函数调用。

    代码中显示usb_generic_driver和usb_serial_generic_driver这两个驱动都使用了generic_probe函数执行probe功能。

    此处应该使用的是usb_generic_driver的generic_probe函数。

     下一行输出信息:[ 3405.949040] usb 5-2: adding 5-2:1.0 (config #1, interface 0)

    $ grep -rnHi 'adding.*config.*interface'  drivers/usb/
    drivers/usb/core/message.c:1861:            "adding %s (config #%d, interface %d)
    ",

    该代码位于usb_set_configuration中,usb_set_configuration函数被generic_probe函数所调用。

    下一行输出信息:[ 3405.949061] bus: 'usb': add device 5-2:1.0

    跟前面其中一行的情况相同,执行bus_add_device时调用了这个函数,bus_add_device又被device_add函数所调用。

    下一行输出信息:[ 3405.949110] usbserial_generic 5-2:1.0: usb_probe_interface

    $ grep -rnHi 'usbserial_generic'  drivers/usb/
    drivers/usb/serial/generic.c:53:    .name =        "usbserial_generic",

    看起来很像是执行usb_probe_interface时给出的输出信息,查看函数源码,果然有下一行代码:

     dev_dbg(dev, "%s
    ", __func__);

    下一行输出信息:[ 3405.949115] usbserial_generic 5-2:1.0: usb_probe_interface - got id

    在查看usb_probe_interface函数源码时看到有类似于"got id"之类的输出,所以是在usb_probe_interface中被调用。

    下一行输出信息:[ 3406.014058] bus: 'usb-serial': add driver pl2303

    $ grep -rnHi 'add driver' drivers/base
    drivers/base/bus.c:635:    pr_debug("bus: '%s': add driver %s
    ", bus->name, drv->name);

    看输出信息中第一个冒号前是usb,再拿前面的例子作对比,可以知道应该是在drivers/base下执行代码产生类似的输出。

     该代码位于bus_add_driver中,bus_add_driver又被driver_register函数所调用。

    下一行输出信息:[ 3406.014092] USB Serial support registered for pl2303

    $ grep -rnHi 'registered for' drivers/usb
    drivers/usb/serial/usb-serial.c:1389:        printk(KERN_INFO "USB Serial support registered for %s
    ",
    drivers/usb/gadget/s3c-hsotg.c:3121: * registered for each available).

    搜索到的第一行信息与输出信息格式一致。

    该代码位于usb_serial_register中,usb_serial_register又被pl2303_init所调用。

    下一行输出信息:[ 3406.039051] bus: 'usb-serial': add device ttyUSB0

    跟前面一些输出信息类似,也是被bus_add_device调用,bus_add_device又被device_add调用。

    下一行输出信息:[ 3406.039201] usb 5-2: pl2303 converter now attached to ttyUSB0

    $ grep -rnHi 'attached to ttyUSB' drivers/usb
    drivers/usb/serial/bus.c:83:         "%s converter now attached to ttyUSB%d
    ",

    此代码位于usb_serial_device_probe中。代码中还有这样的赋值:usb_serial_bus_type.probe=usb_serial_device_probe

    下一行信息输出:[ 3406.039221] usbcore: registered new interface driver pl2303

    $ grep -rnHi 'registered new interface' drivers/usb
    drivers/usb/core/driver.c:888:    pr_info("%s: registered new interface driver %s
    ",

    该代码位于usb_register_driver函数中。

    文件include/linux/usb.h中有下面的定义:

    #define usb_register(driver) 
          usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)

    所以有可能是调用usb_register时输出了这行信息。 pl2303_init也调用了usb_register。

    最后一行输出信息:[ 3406.039223] pl2303: Prolific PL2303 USB to serial adaptor driver

    这行输出信息对应pl2303_init中的下列代码:

    printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "
    ");

     从上面的分析可以看出,执行程序时会运行drivers/base下的一些代码。

    拔掉usb转串口线,然后使能drivers/base下所有文件的"dynamic debugging"功能。

    从帮助文件中查到下面的命令:

    echo 'file drivers/base/* +p' > /sys/kernel/debug/dynamic_debug/control 

    但是执行后却没有作用,貌似还只能一个一个文件的使能"dynamic debugging"。

    另外,使能命令中的+p可以修改成+flmpt,这样会显示出了模块名称和函数名称、行号、线程信息等。

    后注:

    上面提到使用drivers/base/下的文件,关联文件:

    $ head -n 7 drivers/base/Makefile 
    # Makefile for the Linux device tree
    
    obj-y            := core.o sys.o bus.o dd.o syscore.o 
                   driver.o class.o platform.o 
                   cpu.o firmware.o init.o map.o devres.o 
                   attribute_container.o transport_class.o 
                   topology.o

    周末在我自己电脑上又想执行上面的配置,但是一个一个的输入命令太麻烦了,我写了一个小脚本,内容如下:

    #!/bin/sh
    
    DYNAMIC_CONTROL=/sys/kernel/debug/dynamic_debug/control
    echo 'file drivers/usb/serial/usb-serial.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/serial/genemic.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/serial/bus.c +flmpt' > $DYNAMIC_CONTROL
    
    echo 'file drivers/usb/core/usb.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/hub.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/hcd.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/urb.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/message.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/driver.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/config.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/file.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/buffer.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/sysfs.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/endpoint.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/device.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/notify.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/generic.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/quirks.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/devices.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/hci-pci.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/usb/core/inode.c +flmpt' > $DYNAMIC_CONTROL
    
    echo 'file drivers/base/core.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/sys.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/bus.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/dd.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/syscore.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/driver.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/class.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/platform.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/cpu.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/firmware.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/init.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/map.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/devres.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/attribute_container.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/transport_class.c +flmpt' > $DYNAMIC_CONTROL
    echo 'file drivers/base/topology.c +flmpt' > $DYNAMIC_CONTROL

    挂载debugfs,执行这个脚本,然后插入和移除usb转串口得到下面的信息:

    [24923.523302] [17335] usbcore:usb_remote_wakeup:2737: usb usb1: usb wakeup-resume
    [24923.523314] [17335] usbcore:hcd_bus_resume:2013: usb usb1: usb auto-resume
    [24923.536972] [17335] usbcore:hub_resume:2810: hub 1-0:1.0: hub_resume
    [24923.536995] [17335] usbcore:hub_activate:860: hub 1-0:1.0: port 1: status 0101 change 0001
    [24923.537013] [17335] usbcore:hub_activate:860: hub 1-0:1.0: port 4: status 0507 change 0000
    [24923.640857] [164] usbcore:hub_events:3592: hub 1-0:1.0: state 7 ports 4 chg 0002 evt 0000
    [24923.640884] [164] usbcore:hub_port_connect_change:3330: hub 1-0:1.0: port 1, status 0101, change 0000, 12 Mb/s
    [24923.752690] usb 1-1: new full-speed USB device number 26 using xhci_hcd
    [24923.769191] [164] usbcore:usb_get_langid:791: usb 1-1: default language 0x0409
    [24923.769490] [164] usbcore:usb_new_device:1973: usb 1-1: udev 26, busnum 1, minor = 25
    [24923.769498] usb 1-1: New USB device found, idVendor=067b, idProduct=2303
    [24923.769502] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
    [24923.769507] usb 1-1: Product: USB-Serial Controller D
    [24923.769510] usb 1-1: Manufacturer: Prolific Technology Inc. 
    [24923.769520] [164] core:device_add:973: device: '1-1': device_add
    [24923.769685] [164] bus:bus_add_device:502: bus: 'usb': add device 1-1
    [24923.769761] [164] dd:driver_probe_device:211: bus: 'usb': driver_probe_device: matched device 1-1 with driver usb
    [24923.769772] [164] dd:really_probe:114: bus: 'usb': really_probe: probing driver usb with device 1-1
    [24923.769793] [164] usbcore:usb_probe_device:228: usb 1-1: usb_probe_device
    [24923.769805] [164] usbcore:usb_choose_configuration:146: usb 1-1: configuration #1 chosen from 1 choice
    [24923.769984] [164] usbcore:usb_set_configuration:1863: usb 1-1: adding 1-1:1.0 (config #1, interface 0)
    [24923.769993] [164] core:device_add:973: device: '1-1:1.0': device_add
    [24923.770019] [164] bus:bus_add_device:502: bus: 'usb': add device 1-1:1.0
    [24923.770082] [164] dd:driver_probe_device:211: bus: 'usb': driver_probe_device: matched device 1-1:1.0 with driver usbserial_generic
    [24923.770096] [164] dd:really_probe:114: bus: 'usb': really_probe: probing driver usbserial_generic with device 1-1:1.0
    [24923.770119] [164] usbcore:usb_probe_interface:279: usbserial_generic 1-1:1.0: usb_probe_interface
    [24923.770130] [164] usbcore:usb_probe_interface:297: usbserial_generic 1-1:1.0: usb_probe_interface - got id
    [24923.770157] [164] dd:really_probe:152: usbserial_generic: probe of 1-1:1.0 rejects match -19
    [24923.770181] [164] core:device_add:973: device: 'ep_81': device_add
    [24923.770228] [164] core:device_add:973: device: 'ep_02': device_add
    [24923.770258] [164] core:device_add:973: device: 'ep_83': device_add
    [24923.770299] [164] dd:driver_bound:41: driver: '1-1': driver_bound: bound to device 'usb'
    [24923.770306] [164] dd:really_probe:137: bus: 'usb': really_probe: bound device 1-1 to driver usb
    [24923.770317] [164] core:device_add:973: device: 'ep_00': device_add
    [24923.796744] [17709] bus:bus_add_driver:702: bus: 'usb-serial': add driver pl2303
    [24923.796779] USB Serial support registered for pl2303
    [24923.796787] [17709] bus:bus_add_driver:702: bus: 'usb': add driver pl2303
    [24923.796814] [17709] dd:driver_probe_device:211: bus: 'usb': driver_probe_device: matched device 1-1:1.0 with driver pl2303
    [24923.796820] [17709] dd:really_probe:114: bus: 'usb': really_probe: probing driver pl2303 with device 1-1:1.0
    [24923.796831] [17709] usbcore:usb_probe_interface:279: pl2303 1-1:1.0: usb_probe_interface
    [24923.796837] [17709] usbcore:usb_probe_interface:297: pl2303 1-1:1.0: usb_probe_interface - got id
    [24923.796845] pl2303 1-1:1.0: pl2303 converter detected
    [24923.797497] [17709] core:device_add:973: device: 'ttyUSB0': device_add
    [24923.797518] [17709] bus:bus_add_device:502: bus: 'usb-serial': add device ttyUSB0
    [24923.797573] [17709] dd:driver_probe_device:211: bus: 'usb-serial': driver_probe_device: matched device ttyUSB0 with driver pl2303
    [24923.797582] [17709] dd:really_probe:114: bus: 'usb-serial': really_probe: probing driver pl2303 with device ttyUSB0
    [24923.797601] [17709] core:device_add:973: device: 'ttyUSB0': device_add
    [24923.797753] usb 1-1: pl2303 converter now attached to ttyUSB0
    [24923.797760] [17709] dd:driver_bound:41: driver: 'ttyUSB0': driver_bound: bound to device 'pl2303'
    [24923.797768] [17709] dd:really_probe:137: bus: 'usb-serial': really_probe: bound device ttyUSB0 to driver pl2303
    [24923.797778] [17709] dd:driver_bound:41: driver: '1-1:1.0': driver_bound: bound to device 'pl2303'
    [24923.797786] [17709] dd:really_probe:137: bus: 'usb': really_probe: bound device 1-1:1.0 to driver pl2303
    [24923.797816] usbcore: registered new interface driver pl2303
    [24923.797820] pl2303: Prolific PL2303 USB to serial adaptor driver
    [24933.777560] [164] usbcore:hub_events:3592: hub 1-0:1.0: state 7 ports 4 chg 0000 evt 0002
    [24933.777599] [164] usbcore:hub_port_connect_change:3330: hub 1-0:1.0: port 1, status 0100, change 0001, 12 Mb/s
    [24933.777608] usb 1-1: USB disconnect, device number 26
    [24933.777615] [164] usbcore:usb_disconnect:1745: usb 1-1: unregistering device
    [24933.777622] [164] usbcore:usb_disable_device:1165: usb 1-1: unregistering interface 1-1:1.0
    [24933.777631] [164] core:device_unregister:1208: device: 'ep_81': device_unregister
    [24933.777696] [164] core:device_unregister:1208: device: 'ep_02': device_unregister
    [24933.777723] [164] core:device_unregister:1208: device: 'ep_83': device_unregister
    [24933.777771] [164] bus:bus_remove_device:586: bus: 'usb': remove device 1-1:1.0
    [24933.777803] [164] bus:bus_remove_device:586: bus: 'usb-serial': remove device ttyUSB0
    [24933.777829] [164] core:device_unregister:1208: device: 'ttyUSB0': device_unregister
    [24933.778102] [164] core:device_create_release:1469: device: 'ttyUSB0': device_create_release
    [24933.778108] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
    [24933.778127] pl2303 1-1:1.0: device disconnected
    [24933.778158] [164] usbcore:usb_disable_device:1183: usb 1-1: usb_disable_device nuking all URBs
    [24933.778244] [164] core:device_unregister:1208: device: 'ep_00': device_unregister
    [24933.778359] [164] bus:bus_remove_device:586: bus: 'usb': remove device 1-1
    [24933.905540] [164] usbcore:hub_port_debounce:2899: hub 1-0:1.0: debounce: port 1: total 100ms stable 100ms status 0x100
    [24935.902567] [17602] usbcore:hub_suspend:2799: hub 1-0:1.0: hub_suspend
    [24935.902585] [17602] usbcore:hcd_bus_suspend:1977: usb usb1: bus auto-suspend, wakeup 1

    从这个输出信息来看就比较清楚了,哪个模块的那个函数的某一行执行的什么功能,不需要搜索代码就可以看的一清二楚。

    代码执行流程和usb网卡的执行过程很类似,可以参看《debian下使用dynamic printk分析usb网卡驱动》有更详细的介绍。

    还可以将脚本中所有+flmpt替换成-flmpt号,就能去除上面所有文件中的动态调试信息。

  • 相关阅读:
    [USACO08MAR]土地征用Land Acquisition
    树链剖分
    [AHOI2008]紧急集合 / 聚会
    P1852 [国家集训队]跳跳棋
    Ant Trip(欧拉回路+并查集)
    单词游戏
    POJ3694 Network
    C++ STL小总结
    2-Sat专题
    6大名家带你穿越大半个中国去深思
  • 原文地址:https://www.cnblogs.com/qiaoqiao2003/p/3798976.html
Copyright © 2020-2023  润新知