SlideShare a Scribd company logo
1 of 27
Linux Kernel Debugging



 Dongdong Deng <LibFetion@gmail.com>




                              KGDB.info
Overview of Talks
• Kernel Problems

• Collect System Info

• Handling Failures

• Debugging Techniques

• Crash Analyse

• Debugging Process

• Debugging Tricks




                                          KGDB.info
Kernel Problems

• Root cause of problems
      –   self problem (Logic Implementation)
      –   cooperating problem (incorrect API/Function usage)
      –   platform problem (hardware)



• Phenomenon
      –   system behave incorrectly
      –   oops/panic
      –   system hang




                                                               KGDB.info
Collect System Info

• System error logs
      –     dmesg       #dmesg | tail
      –     /var/log/   #ls /var/log/




• Console
      –     local console
      –     remote console



• Others
      –     log by programer



                                              KGDB.info
Handling Failures


• System behave incorrectly
      –    compare with normal behavior
      –    analyze and fix


• System Crash
      –    collect and analyze oops/painc data
      –    collect and analyze dump data


• System Hang
      –    look at the hang using ICE/JTAG
      –    trigger magic sysreq keys
      –    look at the hang using kgdb/kdb(If possible)
      –    hacking codes to use NMI features (if support)

                                                            KGDB.info
Debugging Techniques
• Basic
         –   Printk()

• Best
         –   JTAG, ICE,

• Better
         –   Virtual Machine backend debugger
         –   Kdump/Kexec


• Good
         –   KGDB / KDB

• Others
         –   Kprobe
         –   Perf
         –   Ftrace.. so on..
                                                KGDB.info
printk()
• Works like printf()
   – printk(KERN_DEBUG ”Get printk: %s:%in”, __FILE__, __LINE__);
   – printk(KERN_CRIT "OOO at %pn", pointer);

• Output with priorities
   – KERN_ERR, KERN_WARNING, KERN_INFO, so on…
   – pr_err().pr_warning(),pr_info()…

   #define pr_err(fmt, …) 
   printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS_)


• Increase Log buffer
   – CONFIG_LOG_BUF_SHIFT

• Modify the console printk level
   – #echo 8 > /proc/sys/kernel/printk or #dmesg -n 8
   – integers range from 0 to 7, with smaller values representing higher priorities.

                                                                           KGDB.info
How printk() work
printk() can be called from any context.   Why?
void printk() {
spin_lock(&logbuf_lock);

emit_log_char() --> add data to logbuf

if (!down_trylock(&console_sem)) {
    spin_unlock(&logbuf_lock);
    return;
}
                                     console --> output device
                                     logbuf --> a store buffer for printk data
spin_unlock(&logbuf_lock);
                                     logbuf_lock -> an spinlock for operating logbuf
                                     console_sem -> an semaphore for operating
release_console_sem();                                 console device
}



                                                                      KGDB.info
How printk() work

void release_console_sem() {

for (; ;) {
spin_lock(&logbuf_lock);
if (logbuf_start == logbuf_end)
       break;

out_start = logbuf_start; out_end = logbuf_end;
spin_unlock(&logbuf_lock);

call_console_device (out_start, out_end);
}

up(&console_sem);
spin_unlock (&logbuf_lock);
}


                                                  KGDB.info
How printk() work

printk() {

spin_lock(&logbuf_lock);
emit_log_char(logbuf);
spin_unlock(&logbuf_lock);

down (&console_sem));

spin_lock(&logbuf_lock);
call_console_device (logbuf); à write output device…
spin_unlock(&logbuf_lock);

up(&console_sem);
}



                                                        KGDB.info
printk()
• advantages
   – easy using
   – not need any other system support


• disadvantages
   –   have to modify/rebuild source
   –   cann't debug online Interactively
   –   affect time / behavior
   –   working linear



• Do we need a debugger?




                                            KGDB.info
Debugger
• How debugger works

• Interrupt
   – hardware interrupt
   – exception ---->debug exception
   – software interrupt

• Key components of debugger
   – take over the debug exception
   – pick and poke system info (registers, memory)
   – communicable ----> could receive and deliver data with others




                                                                     KGDB.info
KGDB




       KGDB.info
KGDB using

• KGDB was merged to kernel since 2.6.28

• KGDB Config make menuconfig
     –   CONFIG_KGDB
     –   CONFIG_KGDB_SERIAL_CONSOLE
     –   CONFIG_DEBUG_INFO
     –   CONFIG_FRAME_POINTER
     –   CONFIG_MAGIC_SYSRQ

     –   CONFIG_DEBUG_RODATA = n



                                           KGDB.info
KGDB using

• Kgdboc
        –    build in kernel
        echo "ttyS0,115200" >/sys/module/kgdboc/parameters/kgdboc
        –    module
        Insmod kgdboc.ko kgdboc="ttyS0,115200"
• Gdb
       –     gdb /usr/src/work/vmlinux
       –     (gdb) set remotebaud 115200
       –     (gdb) target remote /dev/ttyS0
       Remote debugging using /dev/ttyS0
       kgdb_breakpoint () at kernel/debug/debug_core.c:983
       983 wmb(); /* Sync point after breakpoint */
       (gdb)
• Trap to kgdb by magic key ----> echo "g" >/proc/sysrq-trigger

                                                                  KGDB.info
KGDB using

• Gdb
    –   (gdb) b address/functions
    –   (gdb) s / si / n / c
    –   (gdb) bt
    –   (gdb) info register/break/threads
    –   (gdb) watch/rwatch (currently only x86 support)
    –   (gdb) m addr
    –   (gdb) set val=abc
    –   (gdb) l* function+0x16




                                                          KGDB.info
KGDB arch




            KGDB.info
Unoptimized debugging
• un-optimize single file
     CFLAGS_filename.o += -O0


• un-optimize entire directory of files
     EXTRA_CFLAGS += -O0

• un-optimize kernel module
       –    make -C build linux.modules COPTIMIZE=-O0 M=path_to_source

• DO NOT UN_OPTIMEZ the whole kernel
       –     some codes were hacked as compiler specific.




                                                            KGDB.info
Got a timing problem? Use variables

• Use a conditional variable to control a printk()
      –    If (dbg_con) { printk(“state info ...”); }

• Use a conditional to execute a variable++
      –    If (dbg_con) { var++; }

• Use a conditional to execute a function
      –    If (dbg_con) { xxx_function(); }

• Debugger set conditional counter
      –   (gdb) set dbg_con=1



                                                        KGDB.info
Questions of Debugger

• How kernel debugger works on multi-cpus (SMP)
   – Before enter debugger core route ---
     hold on the others slave cpu through IPI

   – Before quit debugger route ---
    release the slave cpus
     (tips: run flag à atomic variable, spinlock, row_spinlock)


• How kernel debugger works on multi-processes
   – Have problems?
   single step on specified process,
   schedule

• Other debugger questions?



                                                                   KGDB.info
Crash Analyse

• Where are the crash coming
   – BUG
   – Oops
   – Panic


• Other info
   – Linux/Documentation/oops-tracing.txt




                                            KGDB.info
Crash Analyse
BUG: unable to handle kernel NULL pointer dereference at (null)
  IP: [<c01683c7>] proc_dowatchdog+0x7/0xd0
  *pde = 00000000
  Oops: 0002 [#2] PREEMPT
  Modules linked in:
  Pid: 1126, comm: sh Tainted: G    D 3.0.0-rc2-dirty #4 Bochs Bochs
  EIP: 0060:[<c01683c7>] EFLAGS: 00000286 CPU: 0 à Register Info
  EIP is at proc_dowatchdog+0x7/0xd0
  EAX: c069fcc4 EBX: 00000001 ECX: b7838000 EDX: 00000001
  ESI: c069fcc4 EDI: 00000004 EBP: b7838000 ESP: d7623f30
   DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
  Process sh (pid: 1126, ti=d7622000 task=d749f4a0 task.ti=d7622000)
  Stack:
   d749f4a0 c069f6c0 c069f6c0 c069fcc4 c0202c77 d7623f50 d7623f9c 00000000
   00000004 d7623f9c 00000004 b7838000 c0202cb0 c0202cc8 d7623f9c 00000001
   d7428e00 c01b4d50 d7623f9c 00000002 00000001 d7428e00 fffffff7 081d1300
  Call Trace:
   [<c0202c77>] ? proc_sys_call_handler+0x77/0xb0
   [<c0202cb0>] ? proc_sys_call_handler+0xb0/0xb0
   [<c0202cc8>] ? proc_sys_write+0x18/0x20
   [<c01b4d50>] ? vfs_write+0xa0/0x140
   [<c01b4ec1>] ? sys_write+0x41/0x80
   [<c0539d10>] ? sysenter_do_call+0x12/0x26
  Code: 75 0f c7 03 01 00 00 00 e8 57 69 fd ff 85 c0 74 db a1 48 c0 69
  c0 c7 00 00 00 00 00 31 c0 83 c4 04 5b c3 90 56 53 89 d3 83 ec 08 <c7>
  05 00 00 00 00 05 00 00 00 8b 54 24 18 89 54 24 04 8b 54 24
  EIP: [<c01683c7>] proc_dowatchdog+0x7/0xd0 SS:ESP 0068:d7623f30
  CR2: 0000000000000000
  ---[ end trace 8b37721a29dead5b ]--

                                                                             KGDB.info
Crash Analyse

(gdb) l* proc_dowatchdog+0x7
0xc01683c7 is in proc_dowatchdog (kernel/watchdog.c:522).
517                void __user *buffer, size_t *lenp, loff_t *ppos)
518 {
519      int ret;
520
521      int* xx = NULL;
522      *xx = 5;
523
524      ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
525      if (ret || !write)
526            goto out;
(gdb)




                                                                         KGDB.info
Debugging process
• Reproduce problem
      –   find/read all related documents of problem
      –   version back up/go forward
      –   reduce dependence


• Analyse problem
      –   do more experiments, no guess !!!

• Fix problem
      –   got real root cause?
      –   patch --- simple, clear
      –   enjoy and play kernel!




                                                       KGDB.info
Debugging tricks
Kernel Hacking config
• Get debugging information in case of kernel bugs
          –     CONFIG_FRAME_POINTER

•   Lockup (soft/hard) detector
          –     CONFIG_LOCKUP_DETECTOR

•   SpinLock detector
          –     CONFIG_DEBUG_SPINLOCK

•   RCU cpu stall detector
          –     CONFIG_RCU_CPU_STALL_DETECTOR

•   softlockup / time interrupt
          –     check hang system
          –     soft watchdog
          –     softlockup.c : softlockup_tick()

•   NMI
          –     check hang system
          –     hardware watchdog: nmi_watchdog=1

                                                     KGDB.info
Print Functions


• Some useful Print function for development
     –    BUG_ON()
     –    WARN_ON
     –    show_backtrace()
     –    panic()
     –    die()
     –    show_registers()
     –    print_symbol(pointer)
     –    get function caller:
     return_address() à gcc __builtin_return_address(0)




                                                       KGDB.info
Thanks

 Feedback to:     libfetion@gmail.com
 Or visiting:   http://www.kgdb.info




                                   KGDB.info

More Related Content

What's hot

Linux Crash Dump Capture and Analysis
Linux Crash Dump Capture and AnalysisLinux Crash Dump Capture and Analysis
Linux Crash Dump Capture and AnalysisPaul V. Novarese
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdfAdrian Huang
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Adrian Huang
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...Adrian Huang
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in LinuxAdrian Huang
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicJoseph Lu
 
Memory management in Linux kernel
Memory management in Linux kernelMemory management in Linux kernel
Memory management in Linux kernelVadim Nikitin
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Adrian Huang
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernelAdrian Huang
 
Introduction to DPDK
Introduction to DPDKIntroduction to DPDK
Introduction to DPDKKernel TLV
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux KernelAdrian Huang
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionGene Chang
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCUViller Hsiao
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareBrendan Gregg
 
Physical Memory Models.pdf
Physical Memory Models.pdfPhysical Memory Models.pdf
Physical Memory Models.pdfAdrian Huang
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 

What's hot (20)

Linux Crash Dump Capture and Analysis
Linux Crash Dump Capture and AnalysisLinux Crash Dump Capture and Analysis
Linux Crash Dump Capture and Analysis
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdf
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Memory management in Linux kernel
Memory management in Linux kernelMemory management in Linux kernel
Memory management in Linux kernel
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernel
 
Introduction to DPDK
Introduction to DPDKIntroduction to DPDK
Introduction to DPDK
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCU
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of Software
 
Physical Memory Models.pdf
Physical Memory Models.pdfPhysical Memory Models.pdf
Physical Memory Models.pdf
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
spinlock.pdf
spinlock.pdfspinlock.pdf
spinlock.pdf
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 

Similar to Linux Kernel Debugging Techniques

ELC-E Linux Awareness
ELC-E Linux AwarenessELC-E Linux Awareness
ELC-E Linux AwarenessPeter Griffin
 
SMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiSMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiTakuya ASADA
 
Lecture 6 Kernel Debugging + Ports Development
Lecture 6 Kernel Debugging + Ports DevelopmentLecture 6 Kernel Debugging + Ports Development
Lecture 6 Kernel Debugging + Ports DevelopmentMohammed Farrag
 
LAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLinaro
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness Peter Griffin
 
Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0Jarod Wang
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSESUSE Labs Taipei
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Introduction to cuda geek camp singapore 2011
Introduction to cuda   geek camp singapore 2011Introduction to cuda   geek camp singapore 2011
Introduction to cuda geek camp singapore 2011Raymond Tay
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Jian-Hong Pan
 
Java gpu computing
Java gpu computingJava gpu computing
Java gpu computingArjan Lamers
 
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...Mydbops
 
OSインストーラーの自作方法
OSインストーラーの自作方法OSインストーラーの自作方法
OSインストーラーの自作方法LINE Corporation
 
Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1
Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1
Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1Yukio Saito
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDARaymond Tay
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and InsightsGlobalLogic Ukraine
 
Kernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysisKernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysisAnne Nicolas
 
Anatomy of ROCgdb presentation at gcc cauldron 2022
Anatomy of ROCgdb presentation at gcc cauldron 2022Anatomy of ROCgdb presentation at gcc cauldron 2022
Anatomy of ROCgdb presentation at gcc cauldron 2022ssuser866937
 

Similar to Linux Kernel Debugging Techniques (20)

ELC-E Linux Awareness
ELC-E Linux AwarenessELC-E Linux Awareness
ELC-E Linux Awareness
 
Programar para GPUs
Programar para GPUsProgramar para GPUs
Programar para GPUs
 
SMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgiSMP implementation for OpenBSD/sgi
SMP implementation for OpenBSD/sgi
 
Lecture 6 Kernel Debugging + Ports Development
Lecture 6 Kernel Debugging + Ports DevelopmentLecture 6 Kernel Debugging + Ports Development
Lecture 6 Kernel Debugging + Ports Development
 
LAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel AwarenessLAS16-403: GDB Linux Kernel Awareness
LAS16-403: GDB Linux Kernel Awareness
 
LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness LAS16-403 - GDB Linux Kernel Awareness
LAS16-403 - GDB Linux Kernel Awareness
 
Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0Solaris Kernel Debugging V1.0
Solaris Kernel Debugging V1.0
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSE
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Introduction to cuda geek camp singapore 2011
Introduction to cuda   geek camp singapore 2011Introduction to cuda   geek camp singapore 2011
Introduction to cuda geek camp singapore 2011
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
Java gpu computing
Java gpu computingJava gpu computing
Java gpu computing
 
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
Analyze corefile and backtraces with GDB for Mysql/MariaDB on Linux - Nilanda...
 
OSインストーラーの自作方法
OSインストーラーの自作方法OSインストーラーの自作方法
OSインストーラーの自作方法
 
Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1
Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1
Nvidia® cuda™ 5.0 Sample Evaluation Result Part 1
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDA
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and Insights
 
Debug generic process
Debug generic processDebug generic process
Debug generic process
 
Kernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysisKernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysis
 
Anatomy of ROCgdb presentation at gcc cauldron 2022
Anatomy of ROCgdb presentation at gcc cauldron 2022Anatomy of ROCgdb presentation at gcc cauldron 2022
Anatomy of ROCgdb presentation at gcc cauldron 2022
 

Recently uploaded

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 

Recently uploaded (20)

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 

Linux Kernel Debugging Techniques

  • 1. Linux Kernel Debugging Dongdong Deng <LibFetion@gmail.com> KGDB.info
  • 2. Overview of Talks • Kernel Problems • Collect System Info • Handling Failures • Debugging Techniques • Crash Analyse • Debugging Process • Debugging Tricks KGDB.info
  • 3. Kernel Problems • Root cause of problems – self problem (Logic Implementation) – cooperating problem (incorrect API/Function usage) – platform problem (hardware) • Phenomenon – system behave incorrectly – oops/panic – system hang KGDB.info
  • 4. Collect System Info • System error logs – dmesg #dmesg | tail – /var/log/ #ls /var/log/ • Console – local console – remote console • Others – log by programer KGDB.info
  • 5. Handling Failures • System behave incorrectly – compare with normal behavior – analyze and fix • System Crash – collect and analyze oops/painc data – collect and analyze dump data • System Hang – look at the hang using ICE/JTAG – trigger magic sysreq keys – look at the hang using kgdb/kdb(If possible) – hacking codes to use NMI features (if support) KGDB.info
  • 6. Debugging Techniques • Basic – Printk() • Best – JTAG, ICE, • Better – Virtual Machine backend debugger – Kdump/Kexec • Good – KGDB / KDB • Others – Kprobe – Perf – Ftrace.. so on.. KGDB.info
  • 7. printk() • Works like printf() – printk(KERN_DEBUG ”Get printk: %s:%in”, __FILE__, __LINE__); – printk(KERN_CRIT "OOO at %pn", pointer); • Output with priorities – KERN_ERR, KERN_WARNING, KERN_INFO, so on… – pr_err().pr_warning(),pr_info()… #define pr_err(fmt, …) printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS_) • Increase Log buffer – CONFIG_LOG_BUF_SHIFT • Modify the console printk level – #echo 8 > /proc/sys/kernel/printk or #dmesg -n 8 – integers range from 0 to 7, with smaller values representing higher priorities. KGDB.info
  • 8. How printk() work printk() can be called from any context. Why? void printk() { spin_lock(&logbuf_lock); emit_log_char() --> add data to logbuf if (!down_trylock(&console_sem)) { spin_unlock(&logbuf_lock); return; } console --> output device logbuf --> a store buffer for printk data spin_unlock(&logbuf_lock); logbuf_lock -> an spinlock for operating logbuf console_sem -> an semaphore for operating release_console_sem(); console device } KGDB.info
  • 9. How printk() work void release_console_sem() { for (; ;) { spin_lock(&logbuf_lock); if (logbuf_start == logbuf_end) break; out_start = logbuf_start; out_end = logbuf_end; spin_unlock(&logbuf_lock); call_console_device (out_start, out_end); } up(&console_sem); spin_unlock (&logbuf_lock); } KGDB.info
  • 10. How printk() work printk() { spin_lock(&logbuf_lock); emit_log_char(logbuf); spin_unlock(&logbuf_lock); down (&console_sem)); spin_lock(&logbuf_lock); call_console_device (logbuf); à write output device… spin_unlock(&logbuf_lock); up(&console_sem); } KGDB.info
  • 11. printk() • advantages – easy using – not need any other system support • disadvantages – have to modify/rebuild source – cann't debug online Interactively – affect time / behavior – working linear • Do we need a debugger? KGDB.info
  • 12. Debugger • How debugger works • Interrupt – hardware interrupt – exception ---->debug exception – software interrupt • Key components of debugger – take over the debug exception – pick and poke system info (registers, memory) – communicable ----> could receive and deliver data with others KGDB.info
  • 13. KGDB KGDB.info
  • 14. KGDB using • KGDB was merged to kernel since 2.6.28 • KGDB Config make menuconfig – CONFIG_KGDB – CONFIG_KGDB_SERIAL_CONSOLE – CONFIG_DEBUG_INFO – CONFIG_FRAME_POINTER – CONFIG_MAGIC_SYSRQ – CONFIG_DEBUG_RODATA = n KGDB.info
  • 15. KGDB using • Kgdboc – build in kernel echo "ttyS0,115200" >/sys/module/kgdboc/parameters/kgdboc – module Insmod kgdboc.ko kgdboc="ttyS0,115200" • Gdb – gdb /usr/src/work/vmlinux – (gdb) set remotebaud 115200 – (gdb) target remote /dev/ttyS0 Remote debugging using /dev/ttyS0 kgdb_breakpoint () at kernel/debug/debug_core.c:983 983 wmb(); /* Sync point after breakpoint */ (gdb) • Trap to kgdb by magic key ----> echo "g" >/proc/sysrq-trigger KGDB.info
  • 16. KGDB using • Gdb – (gdb) b address/functions – (gdb) s / si / n / c – (gdb) bt – (gdb) info register/break/threads – (gdb) watch/rwatch (currently only x86 support) – (gdb) m addr – (gdb) set val=abc – (gdb) l* function+0x16 KGDB.info
  • 17. KGDB arch KGDB.info
  • 18. Unoptimized debugging • un-optimize single file CFLAGS_filename.o += -O0 • un-optimize entire directory of files EXTRA_CFLAGS += -O0 • un-optimize kernel module – make -C build linux.modules COPTIMIZE=-O0 M=path_to_source • DO NOT UN_OPTIMEZ the whole kernel – some codes were hacked as compiler specific. KGDB.info
  • 19. Got a timing problem? Use variables • Use a conditional variable to control a printk() – If (dbg_con) { printk(“state info ...”); } • Use a conditional to execute a variable++ – If (dbg_con) { var++; } • Use a conditional to execute a function – If (dbg_con) { xxx_function(); } • Debugger set conditional counter – (gdb) set dbg_con=1 KGDB.info
  • 20. Questions of Debugger • How kernel debugger works on multi-cpus (SMP) – Before enter debugger core route --- hold on the others slave cpu through IPI – Before quit debugger route --- release the slave cpus (tips: run flag à atomic variable, spinlock, row_spinlock) • How kernel debugger works on multi-processes – Have problems? single step on specified process, schedule • Other debugger questions? KGDB.info
  • 21. Crash Analyse • Where are the crash coming – BUG – Oops – Panic • Other info – Linux/Documentation/oops-tracing.txt KGDB.info
  • 22. Crash Analyse BUG: unable to handle kernel NULL pointer dereference at (null) IP: [<c01683c7>] proc_dowatchdog+0x7/0xd0 *pde = 00000000 Oops: 0002 [#2] PREEMPT Modules linked in: Pid: 1126, comm: sh Tainted: G D 3.0.0-rc2-dirty #4 Bochs Bochs EIP: 0060:[<c01683c7>] EFLAGS: 00000286 CPU: 0 à Register Info EIP is at proc_dowatchdog+0x7/0xd0 EAX: c069fcc4 EBX: 00000001 ECX: b7838000 EDX: 00000001 ESI: c069fcc4 EDI: 00000004 EBP: b7838000 ESP: d7623f30 DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068 Process sh (pid: 1126, ti=d7622000 task=d749f4a0 task.ti=d7622000) Stack: d749f4a0 c069f6c0 c069f6c0 c069fcc4 c0202c77 d7623f50 d7623f9c 00000000 00000004 d7623f9c 00000004 b7838000 c0202cb0 c0202cc8 d7623f9c 00000001 d7428e00 c01b4d50 d7623f9c 00000002 00000001 d7428e00 fffffff7 081d1300 Call Trace: [<c0202c77>] ? proc_sys_call_handler+0x77/0xb0 [<c0202cb0>] ? proc_sys_call_handler+0xb0/0xb0 [<c0202cc8>] ? proc_sys_write+0x18/0x20 [<c01b4d50>] ? vfs_write+0xa0/0x140 [<c01b4ec1>] ? sys_write+0x41/0x80 [<c0539d10>] ? sysenter_do_call+0x12/0x26 Code: 75 0f c7 03 01 00 00 00 e8 57 69 fd ff 85 c0 74 db a1 48 c0 69 c0 c7 00 00 00 00 00 31 c0 83 c4 04 5b c3 90 56 53 89 d3 83 ec 08 <c7> 05 00 00 00 00 05 00 00 00 8b 54 24 18 89 54 24 04 8b 54 24 EIP: [<c01683c7>] proc_dowatchdog+0x7/0xd0 SS:ESP 0068:d7623f30 CR2: 0000000000000000 ---[ end trace 8b37721a29dead5b ]-- KGDB.info
  • 23. Crash Analyse (gdb) l* proc_dowatchdog+0x7 0xc01683c7 is in proc_dowatchdog (kernel/watchdog.c:522). 517 void __user *buffer, size_t *lenp, loff_t *ppos) 518 { 519 int ret; 520 521 int* xx = NULL; 522 *xx = 5; 523 524 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 525 if (ret || !write) 526 goto out; (gdb) KGDB.info
  • 24. Debugging process • Reproduce problem – find/read all related documents of problem – version back up/go forward – reduce dependence • Analyse problem – do more experiments, no guess !!! • Fix problem – got real root cause? – patch --- simple, clear – enjoy and play kernel! KGDB.info
  • 25. Debugging tricks Kernel Hacking config • Get debugging information in case of kernel bugs – CONFIG_FRAME_POINTER • Lockup (soft/hard) detector – CONFIG_LOCKUP_DETECTOR • SpinLock detector – CONFIG_DEBUG_SPINLOCK • RCU cpu stall detector – CONFIG_RCU_CPU_STALL_DETECTOR • softlockup / time interrupt – check hang system – soft watchdog – softlockup.c : softlockup_tick() • NMI – check hang system – hardware watchdog: nmi_watchdog=1 KGDB.info
  • 26. Print Functions • Some useful Print function for development – BUG_ON() – WARN_ON – show_backtrace() – panic() – die() – show_registers() – print_symbol(pointer) – get function caller: return_address() à gcc __builtin_return_address(0) KGDB.info
  • 27. Thanks Feedback to: libfetion@gmail.com Or visiting: http://www.kgdb.info KGDB.info