SlideShare a Scribd company logo
1 of 38
Download to read offline
Viller Hsiao <villerhsiao@gmail.com> 
Dec. 14, 2014 
Small Talk about Dynamically 
Trace Code
Rights to Copy 
copyright © 2014 Viller Hsiao 
Attribution – ShareAlike 3.0 
You are free 
● to copy, distribute, display, and perform the work 
● to make derivative works 
● to make commercial use of the work 
Under the following conditions 
Corrections, suggestions, contributions 
and translations are welcome! 
– Attribution. You must give the original author credit. 
– Share Alike. If you alter, transform, or build upon this work, you may distribute the 
resulting work only under a license identical to this one. 
For any reuse or distribution, you must make clear to others the license terms of this work. 
● Any of these conditions can be waived if you get permission from the copyright holder. 
Your fair use and other rights are in no way affected by the above. 
License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode 
12/14/14 2/38
Who am I ? 
Viller Hsiao 
source url: http://goo.gl/n7NAfS 
Embeded Linux / RTOS engineer 
2007 ~ Present Realtek Corp. 
2003 ~ 2007 BenQ/Qisda 
12/14/14 3/38
An Embedded Programmer 
gcc 
Hardware 
Spec 
mmaakkeeffiillee menuconfig 
uclibc 
musl 
armcc 
newlib 
ld objdump 
kbuild 
memory 
management Linker 
script 
glibc 
uboot 
source url: http://ext.pimg.tw/aabb52043/1367377582-3526768649_n.png 
12/14/14 4/38
Topic Today 
How to trace C sources environment 
We have cross reference tools. 
ctags, cscope, LXR, source insight ... and so on 
BUT 
12/14/14 5/38
Difficulties of Tracing C/C++ Sources 
● Preprocessor related 
● Too much #if/#elif/#endif 
● Too much level of macros 
● Configurable included files 
12/14/14 6/38
Difficulties of Tracing C/C++ Sources (Cont.) 
● Runtime related 
● Some debugging and error handling stuff 
● Multi-task environment 
● Hard to realize HW state change 
Different task context 
12/14/14 7/38
Topic Today 
How to trace C sources environment 
Dynamically 
Trace code from runtime execution result 
(Mainly in linux kernel source) 
12/14/14 8/38
Tracing from real execution log 
Provides clues to trace code 
http://www.clker.com/cliparts/c/c/7/p/7/4/earth-hi.png 
http://goo.gl/4PnwFe 
12/14/14 9/38
Agenda Today 
● Build environment 
● Config system 
● Makefile / kbuild 
● Toolchain tips 
● Runtime trace 
● Qemu log 
● Linux ftrace 
12/14/14 10/38
menuconfig 
● Too much words in menuconfig 
● Hard to map definition to code world (CONFIG_XXX) 
12/14/14 11/38
menuconfig Tips – Search 
● Press '/' to search specific configurations 
12/14/14 12/38
menuconfig Tips – Search (Cont.) 
● Search result 
Path in menu 
Kconfig path 
Dependency 
12/14/14 13/38
menuconfig Tips – Result output 
● Linux/.config 
# 
# 
# # Automatically Automatically generated generated file; file; DO DO NOT NOT EDIT. 
EDIT. 
# # Linux/Linux/arm arm 3.14.25 3.14.25 Kernel Kernel Configuration 
Configuration 
# 
# 
CONFIG_CONFIG_ARM=ARM=y 
y 
CONFIG_CONFIG_SYS_SYS_SUPPORTS_SUPPORTS_APM_APM_EMULATION=EMULATION=y 
y 
# 
# General setup 
# 
CONFIG_INIT_ENV_ARG_LIMIT=32 
CONFIG_CROSS_COMPILE="" 
# CONFIG_COMPILE_TEST is not set 
CONFIG_LOCALVERSION="" 
# CONFIG_LOCALVERSION_AUTO is not set 
CONFIG_HAVE_KERNEL_GZIP=y 
CONFIG_HAVE_KERNEL_LZ4=y 
CONFIG_KERNEL_GZIP=y 
# CONFIG_KERNEL_LZMA is not set 
# CONFIG_KERNEL_XZ is not set 
# 
# General setup 
# 
CONFIG_INIT_ENV_ARG_LIMIT=32 
CONFIG_CROSS_COMPILE="" 
# CONFIG_COMPILE_TEST is not set 
CONFIG_LOCALVERSION="" 
# CONFIG_LOCALVERSION_AUTO is not set 
CONFIG_HAVE_KERNEL_GZIP=y 
CONFIG_HAVE_KERNEL_LZ4=y 
CONFIG_KERNEL_GZIP=y 
# CONFIG_KERNEL_LZMA is not set 
# CONFIG_KERNEL_XZ is not set 
12/14/14 14/38
Tips of Tracing makefile 
● make V=1 
● make --dry-run 
12/14/14 15/38
Tips of Tracing makefile (Cont.) 
● make --debug 
12/14/14 16/38
Tips of Tracing Shell Script 
● #!/bin/sh “-x” 
#!/bin/sh -x 
FOO=something_produced_from_command 
if [ x${FOO} != x'' ]; then 
#!/bin/sh -x 
FOO=something_produced_from_command 
if [ x${FOO} != x'' ]; then 
echo "get foo" 
echo "get foo" 
else 
else 
echo "no foo" 
echo "no foo" 
fi 
fi 
● Result 
+ FOO=something_produced_from_command 
+ '[' xsomething_produced_from_command '!=' x ']' 
+ echo 'get foo' 
get foo 
+ FOO=something_produced_from_command 
+ '[' xsomething_produced_from_command '!=' x ']' 
+ echo 'get foo' 
get foo 
12/14/14 17/38
Tips of Tracing Macros/Definitions 
● gcc options: “-save-temps” or “-E” 
$ gcc -save-temps -o main main.c 
$ ls 
main main.c main.i main.o main.s Makefile 
$ gcc -save-temps -o main main.c 
$ ls 
main main.c main.i main.o main.s Makefile 
● Use astyle to beauty result 
$$ aassttyylele --AA88 mmaainin..ii 
12/14/14 18/38
Tips of Tracing Macros/Definitions (Cont.) 
12/14/14 19/38
Tips of Structure Definitions 
● Point to Real definition in the headers 
# 5 "include/linux/linkage.h" 2 
# 1 "include/linux/stringify.h" 1 
# 6 "include/linux/linkage.h" 2 
# 1 "include/linux/export.h" 1 
# 26 "include/linux/export.h" 
struct kernel_symbol 
{ 
unsigned long value; 
const char *name; 
}; 
# 5 "include/linux/linkage.h" 2 
# 1 "include/linux/stringify.h" 1 
# 6 "include/linux/linkage.h" 2 
# 1 "include/linux/export.h" 1 
# 26 "include/linux/export.h" 
struct kernel_symbol 
{ 
unsigned long value; 
const char *name; 
}; 
12/14/14 20/38
Kbuild Tips 
● Add compile flags for some object 
CFLAGS_CFLAGS_ffoooo..oo == --OO00 --gg --ssaavvee--tteemmpp 
● Remove compile flags for some object 
CCFFLLAAGGSS__RREEMMOOVVEE__ffoooo..oo == --OOss 
● All subdirectory 
ssuubbddirir--ccccfflalaggss--yy == --ssaavvee--tteemmppss 
12/14/14 21/38
Qemu Tips 
● Qemu log (“-d op -D /path/to/qemu.log”) 
Setup the search rule 
12/14/14 22/38
Qemu Tips (Cont.) 
● Qemu log + regular expression 
Setup the search rule 
Example: get first user space entry point 
12/14/14 23/38
Qemu Tips (Cont.) 
● Other tips 
● Qemu log + disassembly (objdump) 
● Qemu + gdb ( + qemu log) 
– Breakpoint / stack trace 
– Qemu options 
● -s or -gdb tcp::2345: enable gdb port 
● -S: freeze CPU at startup 
12/14/14 24/38
Ftrace – Overview 
● Kernel function tracer ( > 2.6.28 ?) 
● Record execution log of kernel function 
● Implemented by “-pg” 
– Use dynamic ftrace to reduce overhead 
– Add “-g -O0” to export more information 
● Accessing API – debugfs 
– /path/to/debug/tracing/ 
mount -t debugfs n mount -t debugfs noonnee //ssyyss//kkeerrnneel/l/ddeebbuugg 
12/14/14 25/38
Ftrace – Configs 
12/14/14 26/38
Ftrace – Steps 
[tracing]$ echo “*mtd*” > set_ftrace_filter 
[tracing]$ echo “*blk*” >> set_ftrace_filter 
[tracing]$ echo “*:mod:ext3” >> set_ftrace_filter 
[tracing]$ echo function > current_tracer 
[tracing]$ echo 1 > tracing_on; do_test; echo 0 > tracing_on; 
[tracing]$ cat trace 
[tracing]$ echo “*mtd*” > set_ftrace_filter 
[tracing]$ echo “*blk*” >> set_ftrace_filter 
[tracing]$ echo “*:mod:ext3” >> set_ftrace_filter 
[tracing]$ echo function > current_tracer 
[tracing]$ echo 1 > tracing_on; do_test; echo 0 > tracing_on; 
[tracing]$ cat trace 
12/14/14 27/38
Ftrace – Available Tracer 
[tracing] $ cat available_tracers 
function 
function_graph 
nop 
[tracing] $ cat available_tracers 
function 
function_graph 
nop 
12/14/14 28/38
ftrace 
● function tracer 
12/14/14 29/38
ftrace 
● function tracer 
12/14/14 30/38
ftrace 
● function_graph tracer 
12/14/14 31/38
ftrace 
● function_graph tracer 
Task 
switch 
12/14/14 32/38
ftrace 
● Better front-end 
● trace-cmd 
● kernelshark 
12/14/14 33/38
Ftrace for Code Tracing 
● Ftrace provides clues of 
● Per-module view 
● Top-down view 
● Procedure excution flow 
● Tasks/Threads model relationship 
12/14/14 34/38
Ftrace – Other tracer 
● Trace event 
● ftrace + tracepoint 
● Probe event 
● ftrace + kprobe 
● Other tracer 
● Stack tracer 
● Irqoff latency tracer 
● Syscall tracer 
● And so forth … but not the topic today 
12/14/14 35/38
Conclusion 
● Trace code 
● Cross reference tool helps 
● Drill-down trace 
– Start from interface functions 
● Filesystem hooks: open/read/write/ioctl ... 
● probe/exit 
● ISR entry 
● Architecture interface 
– Global functions: EXPORT_SYMBOLS 
● Execution log provide some hints, too. 
– Log from build flow 
– Qemu instruction trace log 
– Kernel ftrace 
12/14/14 36/38
Reference 
[1] Building embedded linux demo case 
i. Sherif Mousa (Mar 26, 2014), “Building Embedded Linux” 
ii. Linaro wiki, “cross-NG” 
[2] Linux-3.14 documentation, 
i. “kbuild/makefile.txt” 
ii. “trace/ftrace.txt” 
[3] 池田宗廣,大岩尚宏,島本裕志,竹步晶雄,平松雅巳(Jan. 2014), “Linux kernel hacks” 
[4] man of 
i. astyle 
ii. qemu 
iii. Gnu make 
12/14/14 37/38
THE END 
12/14/14 Viller Hsiao

More Related Content

What's hot

The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelDivye Kapoor
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDPlcplcp1
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF AbyssSasha Goldshtein
 
BPF: Tracing and more
BPF: Tracing and moreBPF: Tracing and more
BPF: Tracing and moreBrendan Gregg
 
Introduction to eBPF
Introduction to eBPFIntroduction to eBPF
Introduction to eBPFRogerColl2
 
Kernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixBrendan Gregg
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF SuperpowersBrendan Gregg
 
eBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KernelThomas Graf
 
DPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabDPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabMichelle Holley
 
Kvm performance optimization for ubuntu
Kvm performance optimization for ubuntuKvm performance optimization for ubuntu
Kvm performance optimization for ubuntuSim Janghoon
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking ExplainedThomas Graf
 
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...Linaro
 
Guaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in RustGuaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in Rustnikomatsakis
 
OpenSCAP Overview(security scanning for docker image and container)
OpenSCAP Overview(security scanning for docker image and container)OpenSCAP Overview(security scanning for docker image and container)
OpenSCAP Overview(security scanning for docker image and container)Jooho Lee
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux SystemJian-Hong Pan
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdfAdrian Huang
 
OpenShift Virtualization - VM and OS Image Lifecycle
OpenShift Virtualization - VM and OS Image LifecycleOpenShift Virtualization - VM and OS Image Lifecycle
OpenShift Virtualization - VM and OS Image LifecycleMihai Criveti
 
Cilium - Bringing the BPF Revolution to Kubernetes Networking and Security
Cilium - Bringing the BPF Revolution to Kubernetes Networking and SecurityCilium - Bringing the BPF Revolution to Kubernetes Networking and Security
Cilium - Bringing the BPF Revolution to Kubernetes Networking and SecurityThomas Graf
 

What's hot (20)

The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDP
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF Abyss
 
Linux Network Stack
Linux Network StackLinux Network Stack
Linux Network Stack
 
BPF: Tracing and more
BPF: Tracing and moreBPF: Tracing and more
BPF: Tracing and more
 
Introduction to eBPF
Introduction to eBPFIntroduction to eBPF
Introduction to eBPF
 
Kernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at NetflixKernel Recipes 2017: Using Linux perf at Netflix
Kernel Recipes 2017: Using Linux perf at Netflix
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF Superpowers
 
eBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux Kernel
 
eBPF maps 101
eBPF maps 101eBPF maps 101
eBPF maps 101
 
DPDK in Containers Hands-on Lab
DPDK in Containers Hands-on LabDPDK in Containers Hands-on Lab
DPDK in Containers Hands-on Lab
 
Kvm performance optimization for ubuntu
Kvm performance optimization for ubuntuKvm performance optimization for ubuntu
Kvm performance optimization for ubuntu
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
 
Guaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in RustGuaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in Rust
 
OpenSCAP Overview(security scanning for docker image and container)
OpenSCAP Overview(security scanning for docker image and container)OpenSCAP Overview(security scanning for docker image and container)
OpenSCAP Overview(security scanning for docker image and container)
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux System
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdf
 
OpenShift Virtualization - VM and OS Image Lifecycle
OpenShift Virtualization - VM and OS Image LifecycleOpenShift Virtualization - VM and OS Image Lifecycle
OpenShift Virtualization - VM and OS Image Lifecycle
 
Cilium - Bringing the BPF Revolution to Kubernetes Networking and Security
Cilium - Bringing the BPF Revolution to Kubernetes Networking and SecurityCilium - Bringing the BPF Revolution to Kubernetes Networking and Security
Cilium - Bringing the BPF Revolution to Kubernetes Networking and Security
 

Similar to Trace kernel code tips

Continuous Go Profiling & Observability
Continuous Go Profiling & ObservabilityContinuous Go Profiling & Observability
Continuous Go Profiling & ObservabilityScyllaDB
 
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...Valeriy Kravchuk
 
Sacándole jugo a git
Sacándole jugo a gitSacándole jugo a git
Sacándole jugo a gitBerny Cantos
 
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...PROIDEA
 
Deep dive - Concourse CI/CD and Pipelines
Deep dive  - Concourse CI/CD and PipelinesDeep dive  - Concourse CI/CD and Pipelines
Deep dive - Concourse CI/CD and PipelinesSyed Imam
 
A million ways to provision embedded linux devices
A million ways to provision embedded linux devicesA million ways to provision embedded linux devices
A million ways to provision embedded linux devicesMender.io
 
Troubleshooting .net core on linux
Troubleshooting .net core on linuxTroubleshooting .net core on linux
Troubleshooting .net core on linuxPavel Klimiankou
 
Fuzzing softwares for bugs - OWASP Seasides
Fuzzing softwares for bugs - OWASP SeasidesFuzzing softwares for bugs - OWASP Seasides
Fuzzing softwares for bugs - OWASP SeasidesOWASPSeasides
 
Intro - End to end ML with Kubeflow @ SignalConf 2018
Intro - End to end ML with Kubeflow @ SignalConf 2018Intro - End to end ML with Kubeflow @ SignalConf 2018
Intro - End to end ML with Kubeflow @ SignalConf 2018Holden Karau
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactAlessandro Selli
 
Exploring Raspberry Pi
Exploring Raspberry PiExploring Raspberry Pi
Exploring Raspberry PiLentin Joseph
 
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideBKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideLinaro
 
Introduction to docker
Introduction to dockerIntroduction to docker
Introduction to dockerJustyna Ilczuk
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Herokuronnywang_tw
 
Debugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDBDebugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDBbmbouter
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside OutFerenc Kovács
 
Perspectives on Docker
Perspectives on DockerPerspectives on Docker
Perspectives on DockerRightScale
 

Similar to Trace kernel code tips (20)

Continuous Go Profiling & Observability
Continuous Go Profiling & ObservabilityContinuous Go Profiling & Observability
Continuous Go Profiling & Observability
 
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
 
Sacándole jugo a git
Sacándole jugo a gitSacándole jugo a git
Sacándole jugo a git
 
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
CONFidence 2017: Escaping the (sand)box: The promises and pitfalls of modern ...
 
Kick my mouse away
Kick my mouse awayKick my mouse away
Kick my mouse away
 
Deep dive - Concourse CI/CD and Pipelines
Deep dive  - Concourse CI/CD and PipelinesDeep dive  - Concourse CI/CD and Pipelines
Deep dive - Concourse CI/CD and Pipelines
 
A million ways to provision embedded linux devices
A million ways to provision embedded linux devicesA million ways to provision embedded linux devices
A million ways to provision embedded linux devices
 
Troubleshooting .net core on linux
Troubleshooting .net core on linuxTroubleshooting .net core on linux
Troubleshooting .net core on linux
 
Fuzzing softwares for bugs - OWASP Seasides
Fuzzing softwares for bugs - OWASP SeasidesFuzzing softwares for bugs - OWASP Seasides
Fuzzing softwares for bugs - OWASP Seasides
 
Intro - End to end ML with Kubeflow @ SignalConf 2018
Intro - End to end ML with Kubeflow @ SignalConf 2018Intro - End to end ML with Kubeflow @ SignalConf 2018
Intro - End to end ML with Kubeflow @ SignalConf 2018
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compact
 
Exploring Raspberry Pi
Exploring Raspberry PiExploring Raspberry Pi
Exploring Raspberry Pi
 
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation GuideBKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
BKK16-302: Android Optimizing Compiler: New Member Assimilation Guide
 
Introduction to docker
Introduction to dockerIntroduction to docker
Introduction to docker
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
 
Debugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDBDebugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDB
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
 
Linux Kernel Debugging
Linux Kernel DebuggingLinux Kernel Debugging
Linux Kernel Debugging
 
Perspectives on Docker
Perspectives on DockerPerspectives on Docker
Perspectives on Docker
 

More from Viller Hsiao

Bpf performance tools chapter 4 bcc
Bpf performance tools chapter 4   bccBpf performance tools chapter 4   bcc
Bpf performance tools chapter 4 bccViller Hsiao
 
Prerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyPrerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyViller Hsiao
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsoViller Hsiao
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracingViller Hsiao
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingViller Hsiao
 
mbed-os 3.0 modules dependency graph
mbed-os 3.0 modules dependency graphmbed-os 3.0 modules dependency graph
mbed-os 3.0 modules dependency graphViller Hsiao
 
Introduction to ARM mbed-OS 3.0 uvisor
Introduction to ARM mbed-OS 3.0 uvisorIntroduction to ARM mbed-OS 3.0 uvisor
Introduction to ARM mbed-OS 3.0 uvisorViller Hsiao
 
My first-crawler-in-python
My first-crawler-in-pythonMy first-crawler-in-python
My first-crawler-in-pythonViller Hsiao
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCUViller Hsiao
 
f9-microkernel-ktimer
f9-microkernel-ktimerf9-microkernel-ktimer
f9-microkernel-ktimerViller Hsiao
 

More from Viller Hsiao (10)

Bpf performance tools chapter 4 bcc
Bpf performance tools chapter 4   bccBpf performance tools chapter 4   bcc
Bpf performance tools chapter 4 bcc
 
Prerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyPrerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrency
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdso
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracing
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracing
 
mbed-os 3.0 modules dependency graph
mbed-os 3.0 modules dependency graphmbed-os 3.0 modules dependency graph
mbed-os 3.0 modules dependency graph
 
Introduction to ARM mbed-OS 3.0 uvisor
Introduction to ARM mbed-OS 3.0 uvisorIntroduction to ARM mbed-OS 3.0 uvisor
Introduction to ARM mbed-OS 3.0 uvisor
 
My first-crawler-in-python
My first-crawler-in-pythonMy first-crawler-in-python
My first-crawler-in-python
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCU
 
f9-microkernel-ktimer
f9-microkernel-ktimerf9-microkernel-ktimer
f9-microkernel-ktimer
 

Recently uploaded

System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESNarmatha D
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptxNikhil Raut
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIES
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptx
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

Trace kernel code tips

  • 1. Viller Hsiao <villerhsiao@gmail.com> Dec. 14, 2014 Small Talk about Dynamically Trace Code
  • 2. Rights to Copy copyright © 2014 Viller Hsiao Attribution – ShareAlike 3.0 You are free ● to copy, distribute, display, and perform the work ● to make derivative works ● to make commercial use of the work Under the following conditions Corrections, suggestions, contributions and translations are welcome! – Attribution. You must give the original author credit. – Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. ● Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode 12/14/14 2/38
  • 3. Who am I ? Viller Hsiao source url: http://goo.gl/n7NAfS Embeded Linux / RTOS engineer 2007 ~ Present Realtek Corp. 2003 ~ 2007 BenQ/Qisda 12/14/14 3/38
  • 4. An Embedded Programmer gcc Hardware Spec mmaakkeeffiillee menuconfig uclibc musl armcc newlib ld objdump kbuild memory management Linker script glibc uboot source url: http://ext.pimg.tw/aabb52043/1367377582-3526768649_n.png 12/14/14 4/38
  • 5. Topic Today How to trace C sources environment We have cross reference tools. ctags, cscope, LXR, source insight ... and so on BUT 12/14/14 5/38
  • 6. Difficulties of Tracing C/C++ Sources ● Preprocessor related ● Too much #if/#elif/#endif ● Too much level of macros ● Configurable included files 12/14/14 6/38
  • 7. Difficulties of Tracing C/C++ Sources (Cont.) ● Runtime related ● Some debugging and error handling stuff ● Multi-task environment ● Hard to realize HW state change Different task context 12/14/14 7/38
  • 8. Topic Today How to trace C sources environment Dynamically Trace code from runtime execution result (Mainly in linux kernel source) 12/14/14 8/38
  • 9. Tracing from real execution log Provides clues to trace code http://www.clker.com/cliparts/c/c/7/p/7/4/earth-hi.png http://goo.gl/4PnwFe 12/14/14 9/38
  • 10. Agenda Today ● Build environment ● Config system ● Makefile / kbuild ● Toolchain tips ● Runtime trace ● Qemu log ● Linux ftrace 12/14/14 10/38
  • 11. menuconfig ● Too much words in menuconfig ● Hard to map definition to code world (CONFIG_XXX) 12/14/14 11/38
  • 12. menuconfig Tips – Search ● Press '/' to search specific configurations 12/14/14 12/38
  • 13. menuconfig Tips – Search (Cont.) ● Search result Path in menu Kconfig path Dependency 12/14/14 13/38
  • 14. menuconfig Tips – Result output ● Linux/.config # # # # Automatically Automatically generated generated file; file; DO DO NOT NOT EDIT. EDIT. # # Linux/Linux/arm arm 3.14.25 3.14.25 Kernel Kernel Configuration Configuration # # CONFIG_CONFIG_ARM=ARM=y y CONFIG_CONFIG_SYS_SYS_SUPPORTS_SUPPORTS_APM_APM_EMULATION=EMULATION=y y # # General setup # CONFIG_INIT_ENV_ARG_LIMIT=32 CONFIG_CROSS_COMPILE="" # CONFIG_COMPILE_TEST is not set CONFIG_LOCALVERSION="" # CONFIG_LOCALVERSION_AUTO is not set CONFIG_HAVE_KERNEL_GZIP=y CONFIG_HAVE_KERNEL_LZ4=y CONFIG_KERNEL_GZIP=y # CONFIG_KERNEL_LZMA is not set # CONFIG_KERNEL_XZ is not set # # General setup # CONFIG_INIT_ENV_ARG_LIMIT=32 CONFIG_CROSS_COMPILE="" # CONFIG_COMPILE_TEST is not set CONFIG_LOCALVERSION="" # CONFIG_LOCALVERSION_AUTO is not set CONFIG_HAVE_KERNEL_GZIP=y CONFIG_HAVE_KERNEL_LZ4=y CONFIG_KERNEL_GZIP=y # CONFIG_KERNEL_LZMA is not set # CONFIG_KERNEL_XZ is not set 12/14/14 14/38
  • 15. Tips of Tracing makefile ● make V=1 ● make --dry-run 12/14/14 15/38
  • 16. Tips of Tracing makefile (Cont.) ● make --debug 12/14/14 16/38
  • 17. Tips of Tracing Shell Script ● #!/bin/sh “-x” #!/bin/sh -x FOO=something_produced_from_command if [ x${FOO} != x'' ]; then #!/bin/sh -x FOO=something_produced_from_command if [ x${FOO} != x'' ]; then echo "get foo" echo "get foo" else else echo "no foo" echo "no foo" fi fi ● Result + FOO=something_produced_from_command + '[' xsomething_produced_from_command '!=' x ']' + echo 'get foo' get foo + FOO=something_produced_from_command + '[' xsomething_produced_from_command '!=' x ']' + echo 'get foo' get foo 12/14/14 17/38
  • 18. Tips of Tracing Macros/Definitions ● gcc options: “-save-temps” or “-E” $ gcc -save-temps -o main main.c $ ls main main.c main.i main.o main.s Makefile $ gcc -save-temps -o main main.c $ ls main main.c main.i main.o main.s Makefile ● Use astyle to beauty result $$ aassttyylele --AA88 mmaainin..ii 12/14/14 18/38
  • 19. Tips of Tracing Macros/Definitions (Cont.) 12/14/14 19/38
  • 20. Tips of Structure Definitions ● Point to Real definition in the headers # 5 "include/linux/linkage.h" 2 # 1 "include/linux/stringify.h" 1 # 6 "include/linux/linkage.h" 2 # 1 "include/linux/export.h" 1 # 26 "include/linux/export.h" struct kernel_symbol { unsigned long value; const char *name; }; # 5 "include/linux/linkage.h" 2 # 1 "include/linux/stringify.h" 1 # 6 "include/linux/linkage.h" 2 # 1 "include/linux/export.h" 1 # 26 "include/linux/export.h" struct kernel_symbol { unsigned long value; const char *name; }; 12/14/14 20/38
  • 21. Kbuild Tips ● Add compile flags for some object CFLAGS_CFLAGS_ffoooo..oo == --OO00 --gg --ssaavvee--tteemmpp ● Remove compile flags for some object CCFFLLAAGGSS__RREEMMOOVVEE__ffoooo..oo == --OOss ● All subdirectory ssuubbddirir--ccccfflalaggss--yy == --ssaavvee--tteemmppss 12/14/14 21/38
  • 22. Qemu Tips ● Qemu log (“-d op -D /path/to/qemu.log”) Setup the search rule 12/14/14 22/38
  • 23. Qemu Tips (Cont.) ● Qemu log + regular expression Setup the search rule Example: get first user space entry point 12/14/14 23/38
  • 24. Qemu Tips (Cont.) ● Other tips ● Qemu log + disassembly (objdump) ● Qemu + gdb ( + qemu log) – Breakpoint / stack trace – Qemu options ● -s or -gdb tcp::2345: enable gdb port ● -S: freeze CPU at startup 12/14/14 24/38
  • 25. Ftrace – Overview ● Kernel function tracer ( > 2.6.28 ?) ● Record execution log of kernel function ● Implemented by “-pg” – Use dynamic ftrace to reduce overhead – Add “-g -O0” to export more information ● Accessing API – debugfs – /path/to/debug/tracing/ mount -t debugfs n mount -t debugfs noonnee //ssyyss//kkeerrnneel/l/ddeebbuugg 12/14/14 25/38
  • 26. Ftrace – Configs 12/14/14 26/38
  • 27. Ftrace – Steps [tracing]$ echo “*mtd*” > set_ftrace_filter [tracing]$ echo “*blk*” >> set_ftrace_filter [tracing]$ echo “*:mod:ext3” >> set_ftrace_filter [tracing]$ echo function > current_tracer [tracing]$ echo 1 > tracing_on; do_test; echo 0 > tracing_on; [tracing]$ cat trace [tracing]$ echo “*mtd*” > set_ftrace_filter [tracing]$ echo “*blk*” >> set_ftrace_filter [tracing]$ echo “*:mod:ext3” >> set_ftrace_filter [tracing]$ echo function > current_tracer [tracing]$ echo 1 > tracing_on; do_test; echo 0 > tracing_on; [tracing]$ cat trace 12/14/14 27/38
  • 28. Ftrace – Available Tracer [tracing] $ cat available_tracers function function_graph nop [tracing] $ cat available_tracers function function_graph nop 12/14/14 28/38
  • 29. ftrace ● function tracer 12/14/14 29/38
  • 30. ftrace ● function tracer 12/14/14 30/38
  • 31. ftrace ● function_graph tracer 12/14/14 31/38
  • 32. ftrace ● function_graph tracer Task switch 12/14/14 32/38
  • 33. ftrace ● Better front-end ● trace-cmd ● kernelshark 12/14/14 33/38
  • 34. Ftrace for Code Tracing ● Ftrace provides clues of ● Per-module view ● Top-down view ● Procedure excution flow ● Tasks/Threads model relationship 12/14/14 34/38
  • 35. Ftrace – Other tracer ● Trace event ● ftrace + tracepoint ● Probe event ● ftrace + kprobe ● Other tracer ● Stack tracer ● Irqoff latency tracer ● Syscall tracer ● And so forth … but not the topic today 12/14/14 35/38
  • 36. Conclusion ● Trace code ● Cross reference tool helps ● Drill-down trace – Start from interface functions ● Filesystem hooks: open/read/write/ioctl ... ● probe/exit ● ISR entry ● Architecture interface – Global functions: EXPORT_SYMBOLS ● Execution log provide some hints, too. – Log from build flow – Qemu instruction trace log – Kernel ftrace 12/14/14 36/38
  • 37. Reference [1] Building embedded linux demo case i. Sherif Mousa (Mar 26, 2014), “Building Embedded Linux” ii. Linaro wiki, “cross-NG” [2] Linux-3.14 documentation, i. “kbuild/makefile.txt” ii. “trace/ftrace.txt” [3] 池田宗廣,大岩尚宏,島本裕志,竹步晶雄,平松雅巳(Jan. 2014), “Linux kernel hacks” [4] man of i. astyle ii. qemu iii. Gnu make 12/14/14 37/38
  • 38. THE END 12/14/14 Viller Hsiao