65
Debugging Embedded Devices using GDB Chris Simmonds Embedded Linux Conference Europe 2020 Debugging Embedded Devices using GDB 1 Copyright © 2011-2020, 2net Ltd

Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Debugging Embedded Devices using GDB

Chris Simmonds

Embedded Linux Conference Europe 2020

Debugging Embedded Devices using GDB 1 Copyright copy 2011-2020 2net Ltd

License

These slides are available under a Creative Commons Attribution-ShareAlike 40 license You can read the fulltext of the license herehttpcreativecommonsorglicensesby-sa40legalcode

You are free to

bull copy distribute display and perform the work

bull make derivative works

bull make commercial use of the work

Under the following conditions

bull Attribution you must give the original author credit

bull Share Alike if you alter transform or build upon this work you may distribute the resulting work onlyunder a license identical to this one (ie include this page exactly as it is)

bull For any reuse or distribution you must make clear to others the license terms of this work

Debugging Embedded Devices using GDB 2 Copyright copy 2011-2020 2net Ltd

About Chris Simmondsbull Consultant and trainerbull Author of Mastering Embedded Linux Programmingbull Working with embedded Linux since 1999bull Android since 2009bull Speaker at many conferences and workshops

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 3 Copyright copy 2011-2020 2net Ltd

Objectives

bull Show how to use GDB to debug devices running embedded Linux

bull How to attach to a running process

bull How to look at core dumps

bull Plus we will look at graphical interfaces for GDB

bull Reference Mastering Embedded Linux programming Chapter 14

Debugging is twice as hard as writing the code in the first place Therefore if you write thecode as cleverly as possible you are by definition not smart enough to debug it- Brian W Kernighan

Debugging Embedded Devices using GDB 4 Copyright copy 2011-2020 2net Ltd

Resourcesbull As part of this tutorial I will be running several live demos of the

various techniques

bull For the development machine I will be using a Linux laptop runningUbuntu 1804

bull And I will be using a Raspberry Pi 3B as the target running a YoctoProject Linux build

bull You can download a workbook containing full instructions for setting upand running the demos fromhttps2netcoukdownloads

debugging-with-gdb-csimmonds-elce-2020-workbookpdf

bull I encourage you to follow along with the video -)

Debugging Embedded Devices using GDB 5 Copyright copy 2011-2020 2net Ltd

The Raspberry Pi 3B

bull Popular dev boardhttpswwwraspberrypiorg

bull Low cost ($35)bull BCM2837 Soc 4 x Cortex-A53 ARMv8

64-bit 12GHzbull 1 GiB SDRAMbull Micro SD card slotbull 4 x full size USB 20 A hostbull 100 Mbit Ethernetbull HDMI video output

Debugging Embedded Devices using GDB 6 Copyright copy 2011-2020 2net Ltd

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 2: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

License

These slides are available under a Creative Commons Attribution-ShareAlike 40 license You can read the fulltext of the license herehttpcreativecommonsorglicensesby-sa40legalcode

You are free to

bull copy distribute display and perform the work

bull make derivative works

bull make commercial use of the work

Under the following conditions

bull Attribution you must give the original author credit

bull Share Alike if you alter transform or build upon this work you may distribute the resulting work onlyunder a license identical to this one (ie include this page exactly as it is)

bull For any reuse or distribution you must make clear to others the license terms of this work

Debugging Embedded Devices using GDB 2 Copyright copy 2011-2020 2net Ltd

About Chris Simmondsbull Consultant and trainerbull Author of Mastering Embedded Linux Programmingbull Working with embedded Linux since 1999bull Android since 2009bull Speaker at many conferences and workshops

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 3 Copyright copy 2011-2020 2net Ltd

Objectives

bull Show how to use GDB to debug devices running embedded Linux

bull How to attach to a running process

bull How to look at core dumps

bull Plus we will look at graphical interfaces for GDB

bull Reference Mastering Embedded Linux programming Chapter 14

Debugging is twice as hard as writing the code in the first place Therefore if you write thecode as cleverly as possible you are by definition not smart enough to debug it- Brian W Kernighan

Debugging Embedded Devices using GDB 4 Copyright copy 2011-2020 2net Ltd

Resourcesbull As part of this tutorial I will be running several live demos of the

various techniques

bull For the development machine I will be using a Linux laptop runningUbuntu 1804

bull And I will be using a Raspberry Pi 3B as the target running a YoctoProject Linux build

bull You can download a workbook containing full instructions for setting upand running the demos fromhttps2netcoukdownloads

debugging-with-gdb-csimmonds-elce-2020-workbookpdf

bull I encourage you to follow along with the video -)

Debugging Embedded Devices using GDB 5 Copyright copy 2011-2020 2net Ltd

The Raspberry Pi 3B

bull Popular dev boardhttpswwwraspberrypiorg

bull Low cost ($35)bull BCM2837 Soc 4 x Cortex-A53 ARMv8

64-bit 12GHzbull 1 GiB SDRAMbull Micro SD card slotbull 4 x full size USB 20 A hostbull 100 Mbit Ethernetbull HDMI video output

Debugging Embedded Devices using GDB 6 Copyright copy 2011-2020 2net Ltd

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 3: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

About Chris Simmondsbull Consultant and trainerbull Author of Mastering Embedded Linux Programmingbull Working with embedded Linux since 1999bull Android since 2009bull Speaker at many conferences and workshops

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 3 Copyright copy 2011-2020 2net Ltd

Objectives

bull Show how to use GDB to debug devices running embedded Linux

bull How to attach to a running process

bull How to look at core dumps

bull Plus we will look at graphical interfaces for GDB

bull Reference Mastering Embedded Linux programming Chapter 14

Debugging is twice as hard as writing the code in the first place Therefore if you write thecode as cleverly as possible you are by definition not smart enough to debug it- Brian W Kernighan

Debugging Embedded Devices using GDB 4 Copyright copy 2011-2020 2net Ltd

Resourcesbull As part of this tutorial I will be running several live demos of the

various techniques

bull For the development machine I will be using a Linux laptop runningUbuntu 1804

bull And I will be using a Raspberry Pi 3B as the target running a YoctoProject Linux build

bull You can download a workbook containing full instructions for setting upand running the demos fromhttps2netcoukdownloads

debugging-with-gdb-csimmonds-elce-2020-workbookpdf

bull I encourage you to follow along with the video -)

Debugging Embedded Devices using GDB 5 Copyright copy 2011-2020 2net Ltd

The Raspberry Pi 3B

bull Popular dev boardhttpswwwraspberrypiorg

bull Low cost ($35)bull BCM2837 Soc 4 x Cortex-A53 ARMv8

64-bit 12GHzbull 1 GiB SDRAMbull Micro SD card slotbull 4 x full size USB 20 A hostbull 100 Mbit Ethernetbull HDMI video output

Debugging Embedded Devices using GDB 6 Copyright copy 2011-2020 2net Ltd

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 4: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Objectives

bull Show how to use GDB to debug devices running embedded Linux

bull How to attach to a running process

bull How to look at core dumps

bull Plus we will look at graphical interfaces for GDB

bull Reference Mastering Embedded Linux programming Chapter 14

Debugging is twice as hard as writing the code in the first place Therefore if you write thecode as cleverly as possible you are by definition not smart enough to debug it- Brian W Kernighan

Debugging Embedded Devices using GDB 4 Copyright copy 2011-2020 2net Ltd

Resourcesbull As part of this tutorial I will be running several live demos of the

various techniques

bull For the development machine I will be using a Linux laptop runningUbuntu 1804

bull And I will be using a Raspberry Pi 3B as the target running a YoctoProject Linux build

bull You can download a workbook containing full instructions for setting upand running the demos fromhttps2netcoukdownloads

debugging-with-gdb-csimmonds-elce-2020-workbookpdf

bull I encourage you to follow along with the video -)

Debugging Embedded Devices using GDB 5 Copyright copy 2011-2020 2net Ltd

The Raspberry Pi 3B

bull Popular dev boardhttpswwwraspberrypiorg

bull Low cost ($35)bull BCM2837 Soc 4 x Cortex-A53 ARMv8

64-bit 12GHzbull 1 GiB SDRAMbull Micro SD card slotbull 4 x full size USB 20 A hostbull 100 Mbit Ethernetbull HDMI video output

Debugging Embedded Devices using GDB 6 Copyright copy 2011-2020 2net Ltd

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 5: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Resourcesbull As part of this tutorial I will be running several live demos of the

various techniques

bull For the development machine I will be using a Linux laptop runningUbuntu 1804

bull And I will be using a Raspberry Pi 3B as the target running a YoctoProject Linux build

bull You can download a workbook containing full instructions for setting upand running the demos fromhttps2netcoukdownloads

debugging-with-gdb-csimmonds-elce-2020-workbookpdf

bull I encourage you to follow along with the video -)

Debugging Embedded Devices using GDB 5 Copyright copy 2011-2020 2net Ltd

The Raspberry Pi 3B

bull Popular dev boardhttpswwwraspberrypiorg

bull Low cost ($35)bull BCM2837 Soc 4 x Cortex-A53 ARMv8

64-bit 12GHzbull 1 GiB SDRAMbull Micro SD card slotbull 4 x full size USB 20 A hostbull 100 Mbit Ethernetbull HDMI video output

Debugging Embedded Devices using GDB 6 Copyright copy 2011-2020 2net Ltd

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 6: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

The Raspberry Pi 3B

bull Popular dev boardhttpswwwraspberrypiorg

bull Low cost ($35)bull BCM2837 Soc 4 x Cortex-A53 ARMv8

64-bit 12GHzbull 1 GiB SDRAMbull Micro SD card slotbull 4 x full size USB 20 A hostbull 100 Mbit Ethernetbull HDMI video output

Debugging Embedded Devices using GDB 6 Copyright copy 2011-2020 2net Ltd

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 7: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Yocto Project

httpswwwyoctoprojectorg

bull Yocto Project is a build system that creates packages from sourcecode

bull It is based on the Bitbake job scheduler and OpenEmbedded metadata

bull They allow you to create your own tailor-made Linux distro

bull Yocto Project and OpenEmbedded have been used to create thesoftware running on many millions of devices

bull Instructions for setting up Yocto Project are in the workbook

Debugging Embedded Devices using GDB 7 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 8: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 8 Copyright copy 2011-2020 2net Ltd

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 9: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Toolchains

GNU toolchain = GCC + binutils + C library + GDB

GCC GNU Compiler Collection - C C++ Objective-C Go and otherlanguages

binutils assembler linker and utilities to manipulate object code

C library the POSIX API and interface to the Linux kernel

GDB the GNU debugger

Debugging Embedded Devices using GDB 9 Copyright copy 2011-2020 2net Ltd

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 10: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Native vs cross compiling

Native (develop on target run on target) eg

bull PC

bull Raspberry Pi running Raspbian

Cross (develop on host run on target) eg

bull Yocto ProjectOpenEmbedded

bull Buildroot

This tutorial uses cross compilation

Debugging Embedded Devices using GDB 10 Copyright copy 2011-2020 2net Ltd

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 11: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Getting a toolchain

Your options are

bull Build from upstream source eg using CrosstoolNGhttpcrosstool-nggithubio

bull Download from a trusted third party eg Linaro or Bootlin

bull Use the one provided by your SoCboard vendor (check quality first)

bull Use an embedded build system (Yocto Project OpenEmbeddedBuildroot) to generate one

Debugging Embedded Devices using GDB 11 Copyright copy 2011-2020 2net Ltd

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 12: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Toolchain prefix

bull GNU toolchains are usually identified by a prefix of the formarch-vendor-kernel-operating system

bull Example mipsel-unknown-linux-gnu-

bull arch mipsel (MIPS little endian)

bull vendor unknown

bull kernel linux

bull operating system gnu

bull So the C compiler would bemipsel-unknown-linux-gnu-gcc

Debugging Embedded Devices using GDB 12 Copyright copy 2011-2020 2net Ltd

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 13: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Toolchain prefix for 32-bit ARM toolchains

bull 32-bit ARM has several incompatible ABIs (Application BinaryInterface - the rules for function calls parameter passing etc)

bull Reflected in the Operating system part of the prefix

bull Examples

bull arm-unknown-linux-gnu- Old ABI (obsolete)

bull arm-unknown-linux-gnueabi- Extended ABI with soft floating point()

bull arm-unknown-linux-gnueabihf- Extended ABI with hard floating point()

() Indicates how floating point arguments are passed either in integer registers orhardware floating point registers

Debugging Embedded Devices using GDB 13 Copyright copy 2011-2020 2net Ltd

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 14: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Toolchain sysrootbull The sysroot of the toolchain is the directory containing the supporting

files

bull Header files shared and static libraries etc

bull Native toolchain sysroot = rsquorsquo

bull Cross toolchain sysroot is usually inside the toolchain directory

bull Find it using -print-sysroot

bull Example$ aarch64-buildroot-linux-gnu-gcc -print-sysroot

hometrainingaarch64--glibc--stablebin

aarch64-buildroot-linux-gnusysroot

You need to know the sysroot when cross-compiling and debugging

Debugging Embedded Devices using GDB 14 Copyright copy 2011-2020 2net Ltd

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 15: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

sysroot

[sysroot] lib lib64 -gt lib usr include lib audit gconv lib64 -gt lib libexec getconf share aclocal buildroot i18n locale

sysroot = aarch64-buildroot-linux-gnusysroot

Debugging Embedded Devices using GDB 15 Copyright copy 2011-2020 2net Ltd

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 16: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Getting to know your toolchain

Find out about GCC with these options

bull -print-sysroot print sysroot

bull --version version

bull -v configuration look out for

bull --enable-languages= (example cc++)

bull --with-cpu= (the default CPU)

bull --enable-threads (has POSIX threads library)

Debugging Embedded Devices using GDB 16 Copyright copy 2011-2020 2net Ltd

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 17: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

The Yocto Project SDK

bull If using Yocto ProjectOpenEmbedded you can create an SDK thatincludes a toolchain with

$ bitbake -c populate_sdk ltimage namegt

bull Generates self-installing shell script with a name likepoky-glibc-x86_64-core-image-base-cortexa7t2hf-neon-vfpv4-raspberrypi3-

toolchain-313sh

bull Default install path for this SDK is optpoky313

bull To use the SDK you must first source a script eg$ source optpoky313environment-setup-cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

Debugging Embedded Devices using GDB 17 Copyright copy 2011-2020 2net Ltd

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 18: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Finding the sysroot of a Yocto Project toolchainbull The sysroot is reported as notexist

$ arm-poky-linux-gnueabi-gcc -print-sysroot

notexist

bull Instead the sysroot is set by shell variables CC CXX and LD

bull For example CC contains$ echo $CC

arm-poky-linux-gnueabi-gcc -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7

-fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=

format-security --sysroot=optpoky313sysrootscortexa7t2hf-neon-vfpv4-poky-linux-gnueabi

bull So compile code using$ $CC helloworldc -o helloworld

Debugging Embedded Devices using GDB 18 Copyright copy 2011-2020 2net Ltd

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 19: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

The toolsaddr2line Converts program addresses into file and line noar archive utility is used to create static librariesas GNU assemblercpp C preprocessor expands define include etcg++ C++ front end (assumes source is C++ code)gcc C front end (assumes source is C code)gcov code coverage toolgdb GNU debuggergprof program profiling toolld GNU linkernm lists symbols from object filesobjcopy copy and translate object filesobjdump display information from object filesreadelf displays information about files in ELF object formatsize lists section sizes and the total sizestrings displays strings of printable characters in filesstrip strip object file of debug symbol tables

Debugging Embedded Devices using GDB 19 Copyright copy 2011-2020 2net Ltd

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 20: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time (1)

Boot the Raspberry Pi

Log on

Cross compile helloworld and run on the Raspberry Pi

Debugging Embedded Devices using GDB 20 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 21: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 21 Copyright copy 2011-2020 2net Ltd

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 22: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Preparing to debug 12

Compile with the right level of debug informationgcc -gN myprogc -o myprog

where N is from 0 to 3Level Description0 no debug information (equivalent to omitting -g)1 minimal information just enough to generate a backtrace2 (default) source-level debugging and single-stepping3 information about macros

You can replace -gN with -ggdbN to generate GDB specific debug infoinstead of generic DWARF format

Debugging Embedded Devices using GDB 22 Copyright copy 2011-2020 2net Ltd

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 23: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Preparing to debug 22

bull Code optimization can be a problem

bull especially if you plan to do a lot of single-stepping

bull Consider turning off optimization with compiler flag -O0

bull Or enable just GDB compatible optimizations with compiler flag -Og

Debugging Embedded Devices using GDB 23 Copyright copy 2011-2020 2net Ltd

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 24: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Remote debugging

Programsymbols

gdb fromtoolchain

Programwithout symbols

gdbserver

Host Target

Networkor

serial

Debugging Embedded Devices using GDB 24 Copyright copy 2011-2020 2net Ltd

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 25: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Debug info

bull Need debug info on the host for the applications and libraries youwant to debug

bull Itrsquos OK for the files on the target to be stripped gdbserver does not usedebug info

bull Debug info may be included in the binary (the Buildroot way)

bull Or placed in a sub-directory named debug (the YoctoProjectOpenEmbedded way)

Debugging Embedded Devices using GDB 25 Copyright copy 2011-2020 2net Ltd

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 26: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Setting sysroot

bull sysroot tells GDB where to find library debug info

bull For Buildrootset sysroot lttoolchain sysrootgt

bull Using a Yocto Project SDKset sysroot optpokyltversiongtsysrootsltarchitecturegt

Debugging Embedded Devices using GDB 26 Copyright copy 2011-2020 2net Ltd

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 27: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Command-line debuggingDevelopment host Embedded target

gdbserver 2001 helloworld

$ arm-poky-linux-gnueabi-gdb helloworld(gdb) set sysroot optpoky313(gdb) target remote 1921684222001

ldquoRemote debugging from host 192168421rdquo

program runs to main()

(gdb) break main(gdb) continue

Debugging Embedded Devices using GDB 27 Copyright copy 2011-2020 2net Ltd

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 28: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Notes

bull GDB command target remote links gdb to gdbserver

bull Usually a TCP connection but can be UDP or serial

bull gbdserver loads the program into memory and halts at the firstinstruction

bull You canrsquot use commands such as step or next until after the start of Ccode at main()

bull break main followed by continue stops at main() from which point youcan single step

Debugging Embedded Devices using GDB 28 Copyright copy 2011-2020 2net Ltd

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 29: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

BreakpointsAdd a breakpoint

break [line|function] example

(gdb) break main

Breakpoint 1 at 0x400535 file helloworldc line 7

List breakpointsinfo break

(gdb) info break

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400535 in main at helloworldc7

Delete a breakpointdelete break

(gdb) delete break 1

Debugging Embedded Devices using GDB 29 Copyright copy 2011-2020 2net Ltd

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 30: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Controlling execution

Continue executing the program from a breakpointcontinue

Step one line of code stepping into functionsstep

Step one line of code stepping over functionsnext

Run to the end of the current functionfinish

Run the program from the start (does not work with remote debugging)run

Debugging Embedded Devices using GDB 30 Copyright copy 2011-2020 2net Ltd

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 31: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Displaying and changing variables

Display a variableprint some_var

(gdb) print i

$1 = 1

Change a variableset some_var=new_value

(gdb) set var i=99

Debugging Embedded Devices using GDB 31 Copyright copy 2011-2020 2net Ltd

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 32: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Debug helloworld

Debugging Embedded Devices using GDB 32 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 33: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 33 Copyright copy 2011-2020 2net Ltd

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 34: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

GDB command files

bull At start-up GDB reads commands from

bull $HOMEgdbinit

bull gdbinit in current directory

bull Files named by gdb command line option -x [file name]

bull Note auto-load safe-path

bull Recent versions of GDB ignore gdbinit unless you enable it in$HOMEgdbinit

add-auto-load-safe-path homemynamemyprojectgdbinit

Debugging Embedded Devices using GDB 34 Copyright copy 2011-2020 2net Ltd

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 35: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Defining a new commandbull You can define a new command like this

define bmain

break main

info break

end

bull Then run it just like any other gdb command(gdb) bmain

Breakpoint 1 at 0x400516 file helloworldc line 7

Num Type Disp Enb Address What

1 breakpoint keep y 0x00400516 in main at helloworldc7

bull Useful for sequences that you use many times

bull You can put the code into a command file

Debugging Embedded Devices using GDB 35 Copyright copy 2011-2020 2net Ltd

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 36: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Create a command file to speed things up

Debugging Embedded Devices using GDB 36 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 37: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 37 Copyright copy 2011-2020 2net Ltd

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 38: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

TUI

The Terminal User Interface TUI is an optional component of GDB

Just add -tui to the gdb command (assuming tui is enabled) forexample

arm-poky-linux-gnueabi-gdb -tui helloworld

Or toggle on and off with Ctrl-x a

Debugging Embedded Devices using GDB 38 Copyright copy 2011-2020 2net Ltd

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 39: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

TUI

Debugging Embedded Devices using GDB 39 Copyright copy 2011-2020 2net Ltd

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 40: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

cgdbhttpscgdbgithubio

Similar to TUI but better

Not usually installable as a package but you can instead can get it andinstall like this$ wget httpscgdbmefilescgdb-071targz

$ tar xz cgdb-071targz

$ cd cgdb-071

$ configure --prefix=usrlocal

$ make

$ sudo make install

Then launch it like this

cgdb -d arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 40 Copyright copy 2011-2020 2net Ltd

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 41: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

cgdb

Debugging Embedded Devices using GDB 41 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 42: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

DDD Data Display Debugger

A graphical front-end to GDB

Launch like this

ddd --debugger arm-poky-linux-gnueabi-gdb helloworld

Debugging Embedded Devices using GDB 42 Copyright copy 2011-2020 2net Ltd

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 43: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

DDD Data Display Debugger

Debugging Embedded Devices using GDB 43 Copyright copy 2011-2020 2net Ltd

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 44: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Other front ends for GDB

bull Eclipse CDT (CC++ Development Toolkit)

bull Microsoft Visual Studio Code

bull KDevelop

bull and others

Debugging Embedded Devices using GDB 44 Copyright copy 2011-2020 2net Ltd

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 45: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Debug using cgdb

Debugging Embedded Devices using GDB 45 Copyright copy 2011-2020 2net Ltd

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 46: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

watchpointsBreak when a variable changes

watch some_var

(gdb) watch i

Hardware watchpoint 2 i

(gdb) c

Continuing

0 Hello world

Hardware watchpoint 2 i

Old value = 0

New value = 1

0x0000000000400556 in main (argc=1 argv=0x7fffffffde28) at helloworldc7

7 for (i = 0 i lt 4 i++)

Conditional watchwatch some_var if condition

(gdb) watch i if i == 3

Debugging Embedded Devices using GDB 46 Copyright copy 2011-2020 2net Ltd

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 47: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Set a watchpoint

Debugging Embedded Devices using GDB 47 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 48: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 48 Copyright copy 2011-2020 2net Ltd

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 49: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

stack frames and back traceEach function has a stack frame which contains the local (auto) variables

Show stack framesbt

(gdb) bt

0 addtree (p=0x0 w=0xffffdcd0 quick) at word-countc39

1 0x004008b4 in addtree (p=0x603250 w=0xfffffdcd0 quick) at word-countc53

2 0x004009fd in main (argc=1 argv=0xffffde28) at word-countc92

Display local variablesinfo local

Change current stack frameframe N

(gdb) frame 2

Debugging Embedded Devices using GDB 49 Copyright copy 2011-2020 2net Ltd

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 50: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Stack frames

Debugging Embedded Devices using GDB 50 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 51: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 51 Copyright copy 2011-2020 2net Ltd

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 52: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Debugging library code

bull By default GDB searches for source code in

bull $cdir the compile directory (which is encoded in the ELF header)

bull $cwd the current working directory(gdb) show dir

Source directories searched $cdir$cwd

bull You can extend the search path with the directory command(gdb) dir homechrissrcmylib

Source directories searched homechrissrcmylib$cdir$cwd

Debugging Embedded Devices using GDB 52 Copyright copy 2011-2020 2net Ltd

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 53: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Coping with a relocated sysrootbull $cdir may be wrong if the library is copied to a different directory

bull For example when installing an SDK

bull You can find $cdir like this$ arm-poky-linux-gnueabi-objdump --dwarf helloworld | grep DW_AT_comp_dir

[]

lt1cgt DW_AT_comp_dir (indirect string offset 0x8)

usrsrcdebugglibc231+gitAUTOINC+6fdf971c9d-r0gitcsu

[]

bull Then you can ask GDB to substitute the embedded path with the newone

(gdb) set substitute-path usrsrcdebug optpoky313sysrootscortexa7t2hf-

neon-vfpv4-poky-linux-gnueabiusrsrcdebug

Debugging Embedded Devices using GDB 53 Copyright copy 2011-2020 2net Ltd

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 54: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Debug library code

Debugging Embedded Devices using GDB 54 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 55: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 55 Copyright copy 2011-2020 2net Ltd

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 56: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Just-in-time debugging

bull Both gdb and gdbserver can attach to a running process and debug ityou just need to know the PID

bull With gdbserver you attach like this (PID 999 is an example) gdbserver --attach 2001 999

bull To detach and allow the process to run freely again(gdb) detach

Debugging Embedded Devices using GDB 56 Copyright copy 2011-2020 2net Ltd

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 57: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Attaching to a running program

Debugging Embedded Devices using GDB 57 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 58: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 58 Copyright copy 2011-2020 2net Ltd

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 59: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Core dump

corefile

stack

mmap

heap

data

text (code)

A core file is created if

bull size is lt RLIMIT_CORE

bull the program has write permissions tocreate a file

bull not running with set-user-ID

bull Set RLIMIT_CORE to un-limited usingcommand ulimit -c unlimited

Debugging Embedded Devices using GDB 59 Copyright copy 2011-2020 2net Ltd

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 60: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Core pattern

bull By default core files are called core and placed in the workingdirectory of the program

bull Or core file names are constructed according toprocsyskernelcore_pattern

bull See man core(5) for details

Example corefilese-p

e executable namep PID

Debugging Embedded Devices using GDB 60 Copyright copy 2011-2020 2net Ltd

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 61: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Using GDB to analyse a core dump

bull Copy the core file from the target

bull Then run gdb ltprogram executablegt ltcore filegt

arm-poky-linux-gnueabi-gdb may-crash core

Core was generated by `may-crash

Program terminated with signal SIGSEGV Segmentation fault

0 0x0046851a in gratuitous_error () at may-crashc7

7 p = 42

Debugging Embedded Devices using GDB 61 Copyright copy 2011-2020 2net Ltd

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 62: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Demo time

Core dumps

Debugging Embedded Devices using GDB 62 Copyright copy 2011-2020 2net Ltd

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 63: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

bull Toolchains

bull Remote debugging with gdbserver

bull GDB command files

bull GDB front ends

bull Stack frames

bull Debugging libraries

bull Attaching to a running program

bull Core dumps

bull Final thoughts

Debugging Embedded Devices using GDB 63 Copyright copy 2011-2020 2net Ltd

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 64: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Further reading

bull The Art of Debugging with GDB DDD and Eclipse by Norman Matloffand Peter Jay Salzman No Starch Press 1st edition (28 Sept 2008)

bull GDB Pocket Reference by Arnold Robbins OrsquoReilly Media 1st edition(12 May 2005)

bull Mastering Embedded Linux Programming by Chris Simmonds PacktPublishing 2nd edition

Debugging Embedded Devices using GDB 64 Copyright copy 2011-2020 2net Ltd

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts
Page 65: Debugging Embedded Devices using GDB - 2net...Toolchain sysroot The sysroot of the toolchain is the directory containing the supporting files Header files; shared and static libraries,

Any questions

Looking after the Inner Penguin blog at https2netcouk

2net_software

httpsuklinkedincominchrisdsimmonds

Debugging Embedded Devices using GDB 65 Copyright copy 2011-2020 2net Ltd

  • Debugging embedded devices using GDB
  • Toolchains
  • Remote debugging with gdbserver
  • GDB command files
  • GDB front ends
  • Stack frames
  • Debugging libraries
  • Attaching to a running program
  • Core dumps
  • Final thoughts