SlideShare a Scribd company logo
Dynamic
Instrumentation
@JeffryMolanus @openebs
Golang Meetup Bangalore XXIV
15 July 2017
Tracing
• To record information about a programs execution
• Useful for understanding code, in particular a very large code base
• Used during debugging, statistics, so on and so forth
• Dynamic tracing is the ability to ad-hoc add or remove certain instrumentation without
making changes to the code that is subject to tracing or restarting the program or system
• In general, tracing should not effect the stability of the program that is being traced in
production, during development its less of importance
• When no tracing is enabled there should be no overhead; when enabled the overhead
depends on what is traced and how
• User land tracing requires abilities in kernel (which is the focus of this talk)
• user space tracing has a little more overhead due to the induced context switch
Tracers on other platforms
• Illumos/Solaris and FreeBSD
• Dtrace, very powerful and production safe used for many years
• Compressed Type Format (CTF) data is available in binaries and libraries, no
need for debug symbols to work with the types
• Solaris uses the same CTF data for type information for debugging
• Event Tracing for Windows (EWT)
• Linux
• Requires debug symbols to be downloaded depending on what you trace and how
specific you want to trace
• With DWARF data more can be done then with plain CTF however
Basic architecture of tracing
• There are generally, two parts of tracing in Linux
• Frontend tools to work/consume with/the in kernel tracing
facilities
• We will look briefly in ftrace, systemtap and BCC
• Backend subsystems
• Kernel code that executes what ever code you want to be
executed on entering the probes function or address
• kprobes, probes, tracepoints, sysdig
ftrace
• Tracepoints; static probes defined in the kernel that can be enabled at
run time
• ABI is kept stable by kernel
• static implies you have to know what you want to trace while
developing the code
• Makes use of sysfs interface to interact with it
• Several wrappers exist to make things a little easier
• tracecmd and kernelshark (UI)
• Also check the excellent stuff from Brendan Gregg
Adding a tracepoint
Trace points in sysfs
kernelshark
kprobes
• kprobes is defined in multiple sub categories
• jprobes: trace function entry (optimised for function entry, copy stack)
• kretprobes: trace function return
• kprobes: trace at any arbitrary instruction in the kernel
• To use it one has to write a kernel module which needs to be loaded at run
time
• this is not guaranteed to be safe
• A kprobe replaces the traced instruction with a break point instruction
• On entry, the pre_handler is called after instrumenting, the post handler
kprobes
Kprobe example
Kprobe example
jprobes
• Note: function
prototype needs to
match the actual
syscall
utrace/uprobes
• Roughly the the same as the kprobe facility in the kernel but focused
on user land tracing
• current ptrace() in linux is implemented using the utrace frame work
• tools like strace and GDB use ptrace()
• Allows for more sophisticated tooling, one of which is uprobes
• Trace points are placed on the an inode:offset tuple
• All binaries that map that address will have a SW breakpoint
injected at that address
ftrace & user space
• The same ftrace interface is available for working with uprobes
• Behind the scene the kernel does the right thing (e.g use kprobe,
tracepoints, or uprobes)
• The same sysfs interface is used, general work flow:
• Find address to place the probe on
• Enable probing
• Disable probing
• View results (flight recorder)
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
eBPF• Pretty sure everyone here has used
BPF likely with out knowing
• tcpdump uses BPF
• eBPF is enhanced BPF
• sandboxed byte code executed by
kernel which is safe and user
defined
• attach eBPF to kprobes and
uprobes
• certain restrictions in abilities
BCC
• BPF Compiler Collection,
compiles code for the in kernel
VM to be executed
• Several high level wrappers for
Python, lua and GO
• Code is still written in C
however
Recap
• Several back-end tracing capabilities in the kernel
• Tracepoints, kprobes, jprobes, kretprobes and uprobes
• eBPF allows attachment to kprobe, uprobes and tracepoints for
safe execution
• Linux tracing world can use better generic frontends for adhoc
tracing
• Best today are perf and systemtap (IMHO)
• Who wants to write C when you want to print a member of a
complex struct? (ply)
Systemtap
• High level scripting language to work with the aforementioned tracing
capabilities of Linux
• Flexible as it allows for writing scripts that can trace specific lines
within a file (debug symbols)
• Next to tracing, it can also make changes to running programs when
run in “guru mode”
• Resulting scripts from systemtap are kernel modules that are loaded
in to the kernel (kprobe and uprobes)
• Adding a eBPF target is in the works as currently, systemtap may
result in unremovable modules or sudden death of traced processes
stp files
• Example script oneliner:
• stap -e ‘probe syscall.open { printf(“exec %s, file%s, execname(),
filename) }’
• stap -L ‘syscall.open'
• syscall.open: __nr:long name:string filename:string flags:long
flags_str:string mode:long argstr:string
• List user space functions in process “trace”
• stap -L ‘process(“./trace").function("*")'
• .call and .return probes for each function
List probes
Tracing line numbers
• What's the value of ret after
line 35?
• Could be done by tracing ret
values, but that is not the
purpose of this exercise
• gcc -g -O0
• full debug info
Tracing line number
• .statement(“main@code/talk/trace.c:36”) { … }
Understanding code flow
Understanding code flow
Downstack
• All functions
being called by
a function
Tracing go
Cant trace return values
Calling convention
• AMD64 calling conventions
• RDI, RSI, RDX, RCX, R8 and R9
• Go is based on PLAN9 which uses a different approach therefore tracing does not work as
well as one would like it to be (yet)
• This also goes for debuggers
• Perhaps Go will start using the X86_64 ABI as it moves forward or all tools and debuggers
will add specific PLAN9 support
• https://go-review.googlesource.com/#/c/28832/ (ABI change?)
• GO bindings to the BCC tool chain
• Allows for creating eBPF tracing tools written in go
• but still requires writing the actual trace logic in C
Summary
• Dynamic tracing is an invaluable tool for
understanding code flow
• To verify hypotheses around software bugs or
understanding
• Ability to make changes to code on the fly with out
recompiling (guru mode)
• Under constant development most noticeable the
eBPF/BCC work

More Related Content

PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
PDF
Return oriented programming
PPTX
Return oriented programming (ROP)
PDF
Overview of FreeBSD PMC Tools
PDF
Get Lower Latency and Higher Throughput for Java Applications
ODP
Linux multiplexing
PPT
Deploying Puppet Code At Light Speed - Puppet Camp Silicon Valley
PPT
Deploying puppet code at light speed
Efficient Bytecode Analysis: Linespeed Shellcode Detection
Return oriented programming
Return oriented programming (ROP)
Overview of FreeBSD PMC Tools
Get Lower Latency and Higher Throughput for Java Applications
Linux multiplexing
Deploying Puppet Code At Light Speed - Puppet Camp Silicon Valley
Deploying puppet code at light speed

What's hot (20)

PDF
Linux Performance Analysis: New Tools and Old Secrets
PDF
Micro control idsecconf2010
PDF
LMG Lightning Talks - SFO17-205
PPTX
Demo
PPTX
epoll() - The I/O Hero
PPTX
0.5mln packets per second with Erlang
PPT
Epoll - from the kernel side
PPTX
Os lectures
PPT
PDF
Exploit techniques and mitigation
PDF
New Process/Thread Runtime
PDF
Practical SystemTAP basics: Perl memory profiling
PDF
DCSF 19 eBPF Superpowers
PDF
Embedded Erlang, Nerves, and SumoBots
PPTX
Operating Systems - A Primer
PDF
Building a Network IP Camera using Erlang
PDF
TFLite NNAPI and GPU Delegates
PPT
Systemtap
PDF
Is That A Penguin In My Windows?
PPTX
Vulnerability desing patterns
Linux Performance Analysis: New Tools and Old Secrets
Micro control idsecconf2010
LMG Lightning Talks - SFO17-205
Demo
epoll() - The I/O Hero
0.5mln packets per second with Erlang
Epoll - from the kernel side
Os lectures
Exploit techniques and mitigation
New Process/Thread Runtime
Practical SystemTAP basics: Perl memory profiling
DCSF 19 eBPF Superpowers
Embedded Erlang, Nerves, and SumoBots
Operating Systems - A Primer
Building a Network IP Camera using Erlang
TFLite NNAPI and GPU Delegates
Systemtap
Is That A Penguin In My Windows?
Vulnerability desing patterns
Ad

Similar to Dynamic Instrumentation- OpenEBS Golang Meetup July 2017 (20)

PPTX
Modern Linux Tracing Landscape
PPTX
Performance analysis and troubleshooting using DTrace
PDF
DEF CON 27 - JEFF DILEO - evil e bpf in depth
PPTX
eBPF Basics
PDF
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
PDF
CNIT 127: Ch 18: Source Code Auditing
PPTX
Week1 Electronic System-level ESL Design and SystemC Begin
PPTX
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
PDF
Serving Deep Learning Models At Scale With RedisAI: Luca Antiga
PDF
Systems Programming Assignment Help - Processes
PDF
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
PDF
A Peek into TFRT
PDF
Threads operating system slides easy understand
PPTX
Practical Windows Kernel Exploitation
PPTX
Ice Age melting down: Intel features considered usefull!
PPTX
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
PPTX
Игорь Фесенко "Direction of C# as a High-Performance Language"
PDF
BPF - in-kernel virtual machine
PPTX
The power of linux advanced tracer [POUG18]
PDF
Linux Perf Tools
Modern Linux Tracing Landscape
Performance analysis and troubleshooting using DTrace
DEF CON 27 - JEFF DILEO - evil e bpf in depth
eBPF Basics
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CNIT 127: Ch 18: Source Code Auditing
Week1 Electronic System-level ESL Design and SystemC Begin
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
Serving Deep Learning Models At Scale With RedisAI: Luca Antiga
Systems Programming Assignment Help - Processes
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
A Peek into TFRT
Threads operating system slides easy understand
Practical Windows Kernel Exploitation
Ice Age melting down: Intel features considered usefull!
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
Игорь Фесенко "Direction of C# as a High-Performance Language"
BPF - in-kernel virtual machine
The power of linux advanced tracer [POUG18]
Linux Perf Tools
Ad

More from OpenEBS (20)

PDF
Redis Conf 2019--Container Attached Storage for Redis
PPTX
Replication and rebuild in cStor
PPTX
Data Agility for Devops - OSI 2018
PPTX
Introduction to cStor replica - Contributors Meet 5th Oct 2018
PPTX
Running OpenEBS on GPDs - Weekly Contributors Meet 28th Sep 2018
PDF
Container Attached Storage (CAS) with OpenEBS - SDC 2018
PPTX
Volume Policies in OpenEBS 0.7
PPTX
Thoughts on heptio's ark - Contributors Meet 21st Sept 2018
PDF
Latest (storage IO) patterns for cloud-native applications
PDF
Deploying OpenEBS with Availability Zones
PDF
Kubernetes Monitoring and Troubleshooting using Weavescope- Kubernetes Meetup...
PDF
OpenEBS Visualization and Monitoring using Weave-scope - Contributors Meet 1s...
PDF
Container Attached Storage (CAS) with OpenEBS - Berlin Kubernetes Meetup - Ma...
PDF
BDD Testing Using Godog - Bangalore Golang Meetup # 32
PDF
Container Attached Storage - Chennai Kubernetes Meetup #2 - April 21st 2018
PDF
Kubernetes Visualization-and-Monitoring-using-Weave-scope
PDF
OpenEBS CAS SDC India - 2018
PPTX
OpenEBS hangout #4
PDF
Containerized Storage for Containers
PDF
South Bay Kubernetes DevOps
Redis Conf 2019--Container Attached Storage for Redis
Replication and rebuild in cStor
Data Agility for Devops - OSI 2018
Introduction to cStor replica - Contributors Meet 5th Oct 2018
Running OpenEBS on GPDs - Weekly Contributors Meet 28th Sep 2018
Container Attached Storage (CAS) with OpenEBS - SDC 2018
Volume Policies in OpenEBS 0.7
Thoughts on heptio's ark - Contributors Meet 21st Sept 2018
Latest (storage IO) patterns for cloud-native applications
Deploying OpenEBS with Availability Zones
Kubernetes Monitoring and Troubleshooting using Weavescope- Kubernetes Meetup...
OpenEBS Visualization and Monitoring using Weave-scope - Contributors Meet 1s...
Container Attached Storage (CAS) with OpenEBS - Berlin Kubernetes Meetup - Ma...
BDD Testing Using Godog - Bangalore Golang Meetup # 32
Container Attached Storage - Chennai Kubernetes Meetup #2 - April 21st 2018
Kubernetes Visualization-and-Monitoring-using-Weave-scope
OpenEBS CAS SDC India - 2018
OpenEBS hangout #4
Containerized Storage for Containers
South Bay Kubernetes DevOps

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
Programs and apps: productivity, graphics, security and other tools
Spectral efficient network and resource selection model in 5G networks
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.

Dynamic Instrumentation- OpenEBS Golang Meetup July 2017

  • 2. Tracing • To record information about a programs execution • Useful for understanding code, in particular a very large code base • Used during debugging, statistics, so on and so forth • Dynamic tracing is the ability to ad-hoc add or remove certain instrumentation without making changes to the code that is subject to tracing or restarting the program or system • In general, tracing should not effect the stability of the program that is being traced in production, during development its less of importance • When no tracing is enabled there should be no overhead; when enabled the overhead depends on what is traced and how • User land tracing requires abilities in kernel (which is the focus of this talk) • user space tracing has a little more overhead due to the induced context switch
  • 3. Tracers on other platforms • Illumos/Solaris and FreeBSD • Dtrace, very powerful and production safe used for many years • Compressed Type Format (CTF) data is available in binaries and libraries, no need for debug symbols to work with the types • Solaris uses the same CTF data for type information for debugging • Event Tracing for Windows (EWT) • Linux • Requires debug symbols to be downloaded depending on what you trace and how specific you want to trace • With DWARF data more can be done then with plain CTF however
  • 4. Basic architecture of tracing • There are generally, two parts of tracing in Linux • Frontend tools to work/consume with/the in kernel tracing facilities • We will look briefly in ftrace, systemtap and BCC • Backend subsystems • Kernel code that executes what ever code you want to be executed on entering the probes function or address • kprobes, probes, tracepoints, sysdig
  • 5. ftrace • Tracepoints; static probes defined in the kernel that can be enabled at run time • ABI is kept stable by kernel • static implies you have to know what you want to trace while developing the code • Makes use of sysfs interface to interact with it • Several wrappers exist to make things a little easier • tracecmd and kernelshark (UI) • Also check the excellent stuff from Brendan Gregg
  • 9. kprobes • kprobes is defined in multiple sub categories • jprobes: trace function entry (optimised for function entry, copy stack) • kretprobes: trace function return • kprobes: trace at any arbitrary instruction in the kernel • To use it one has to write a kernel module which needs to be loaded at run time • this is not guaranteed to be safe • A kprobe replaces the traced instruction with a break point instruction • On entry, the pre_handler is called after instrumenting, the post handler
  • 13. jprobes • Note: function prototype needs to match the actual syscall
  • 14. utrace/uprobes • Roughly the the same as the kprobe facility in the kernel but focused on user land tracing • current ptrace() in linux is implemented using the utrace frame work • tools like strace and GDB use ptrace() • Allows for more sophisticated tooling, one of which is uprobes • Trace points are placed on the an inode:offset tuple • All binaries that map that address will have a SW breakpoint injected at that address
  • 15. ftrace & user space • The same ftrace interface is available for working with uprobes • Behind the scene the kernel does the right thing (e.g use kprobe, tracepoints, or uprobes) • The same sysfs interface is used, general work flow: • Find address to place the probe on • Enable probing • Disable probing • View results (flight recorder)
  • 18. eBPF• Pretty sure everyone here has used BPF likely with out knowing • tcpdump uses BPF • eBPF is enhanced BPF • sandboxed byte code executed by kernel which is safe and user defined • attach eBPF to kprobes and uprobes • certain restrictions in abilities
  • 19. BCC • BPF Compiler Collection, compiles code for the in kernel VM to be executed • Several high level wrappers for Python, lua and GO • Code is still written in C however
  • 20. Recap • Several back-end tracing capabilities in the kernel • Tracepoints, kprobes, jprobes, kretprobes and uprobes • eBPF allows attachment to kprobe, uprobes and tracepoints for safe execution • Linux tracing world can use better generic frontends for adhoc tracing • Best today are perf and systemtap (IMHO) • Who wants to write C when you want to print a member of a complex struct? (ply)
  • 21. Systemtap • High level scripting language to work with the aforementioned tracing capabilities of Linux • Flexible as it allows for writing scripts that can trace specific lines within a file (debug symbols) • Next to tracing, it can also make changes to running programs when run in “guru mode” • Resulting scripts from systemtap are kernel modules that are loaded in to the kernel (kprobe and uprobes) • Adding a eBPF target is in the works as currently, systemtap may result in unremovable modules or sudden death of traced processes
  • 22. stp files • Example script oneliner: • stap -e ‘probe syscall.open { printf(“exec %s, file%s, execname(), filename) }’ • stap -L ‘syscall.open' • syscall.open: __nr:long name:string filename:string flags:long flags_str:string mode:long argstr:string • List user space functions in process “trace” • stap -L ‘process(“./trace").function("*")' • .call and .return probes for each function
  • 24. Tracing line numbers • What's the value of ret after line 35? • Could be done by tracing ret values, but that is not the purpose of this exercise • gcc -g -O0 • full debug info
  • 25. Tracing line number • .statement(“main@code/talk/trace.c:36”) { … }
  • 28. Downstack • All functions being called by a function
  • 31. Calling convention • AMD64 calling conventions • RDI, RSI, RDX, RCX, R8 and R9 • Go is based on PLAN9 which uses a different approach therefore tracing does not work as well as one would like it to be (yet) • This also goes for debuggers • Perhaps Go will start using the X86_64 ABI as it moves forward or all tools and debuggers will add specific PLAN9 support • https://go-review.googlesource.com/#/c/28832/ (ABI change?) • GO bindings to the BCC tool chain • Allows for creating eBPF tracing tools written in go • but still requires writing the actual trace logic in C
  • 32. Summary • Dynamic tracing is an invaluable tool for understanding code flow • To verify hypotheses around software bugs or understanding • Ability to make changes to code on the fly with out recompiling (guru mode) • Under constant development most noticeable the eBPF/BCC work