1*387f9dfdSAndroid Build Coastguard WorkerDemonstrations of trace. 2*387f9dfdSAndroid Build Coastguard Worker 3*387f9dfdSAndroid Build Coastguard Worker 4*387f9dfdSAndroid Build Coastguard Workertrace probes functions you specify and displays trace messages if a particular 5*387f9dfdSAndroid Build Coastguard Workercondition is met. You can control the message format to display function 6*387f9dfdSAndroid Build Coastguard Workerarguments and return values. 7*387f9dfdSAndroid Build Coastguard Worker 8*387f9dfdSAndroid Build Coastguard WorkerFor example, suppose you want to trace all commands being exec'd across the 9*387f9dfdSAndroid Build Coastguard Workersystem: 10*387f9dfdSAndroid Build Coastguard Worker 11*387f9dfdSAndroid Build Coastguard Worker# trace 'sys_execve "%s", arg1' 12*387f9dfdSAndroid Build Coastguard WorkerPID COMM FUNC - 13*387f9dfdSAndroid Build Coastguard Worker4402 bash sys_execve /usr/bin/man 14*387f9dfdSAndroid Build Coastguard Worker4411 man sys_execve /usr/local/bin/less 15*387f9dfdSAndroid Build Coastguard Worker4411 man sys_execve /usr/bin/less 16*387f9dfdSAndroid Build Coastguard Worker4410 man sys_execve /usr/local/bin/nroff 17*387f9dfdSAndroid Build Coastguard Worker4410 man sys_execve /usr/bin/nroff 18*387f9dfdSAndroid Build Coastguard Worker4409 man sys_execve /usr/local/bin/tbl 19*387f9dfdSAndroid Build Coastguard Worker4409 man sys_execve /usr/bin/tbl 20*387f9dfdSAndroid Build Coastguard Worker4408 man sys_execve /usr/local/bin/preconv 21*387f9dfdSAndroid Build Coastguard Worker4408 man sys_execve /usr/bin/preconv 22*387f9dfdSAndroid Build Coastguard Worker4415 nroff sys_execve /usr/bin/locale 23*387f9dfdSAndroid Build Coastguard Worker4416 nroff sys_execve /usr/bin/groff 24*387f9dfdSAndroid Build Coastguard Worker4418 groff sys_execve /usr/bin/grotty 25*387f9dfdSAndroid Build Coastguard Worker4417 groff sys_execve /usr/bin/troff 26*387f9dfdSAndroid Build Coastguard Worker^C 27*387f9dfdSAndroid Build Coastguard Worker 28*387f9dfdSAndroid Build Coastguard WorkerThe ::sys_execve syntax specifies that you want an entry probe (which is the 29*387f9dfdSAndroid Build Coastguard Workerdefault), in a kernel function (which is the default) called sys_execve. Next, 30*387f9dfdSAndroid Build Coastguard Workerthe format string to print is simply "%s", which prints a string. Finally, the 31*387f9dfdSAndroid Build Coastguard Workervalue to print is the first argument to the sys_execve function, which happens 32*387f9dfdSAndroid Build Coastguard Workerto be the command that is exec'd. The above trace was generated by executing 33*387f9dfdSAndroid Build Coastguard Worker"man ls" in a separate shell. As you see, man executes a number of additional 34*387f9dfdSAndroid Build Coastguard Workerprograms to finally display the man page. 35*387f9dfdSAndroid Build Coastguard Worker 36*387f9dfdSAndroid Build Coastguard WorkerNext, suppose you are looking for large reads across the system. Let's trace 37*387f9dfdSAndroid Build Coastguard Workerthe read system call and inspect the third argument, which is the number of 38*387f9dfdSAndroid Build Coastguard Workerbytes to be read: 39*387f9dfdSAndroid Build Coastguard Worker 40*387f9dfdSAndroid Build Coastguard Worker# trace 'sys_read (arg3 > 20000) "read %d bytes", arg3' 41*387f9dfdSAndroid Build Coastguard WorkerPID COMM FUNC - 42*387f9dfdSAndroid Build Coastguard Worker4490 dd sys_read read 1048576 bytes 43*387f9dfdSAndroid Build Coastguard Worker4490 dd sys_read read 1048576 bytes 44*387f9dfdSAndroid Build Coastguard Worker4490 dd sys_read read 1048576 bytes 45*387f9dfdSAndroid Build Coastguard Worker4490 dd sys_read read 1048576 bytes 46*387f9dfdSAndroid Build Coastguard Worker^C 47*387f9dfdSAndroid Build Coastguard Worker 48*387f9dfdSAndroid Build Coastguard WorkerDuring the trace, I executed "dd if=/dev/zero of=/dev/null bs=1M count=4". 49*387f9dfdSAndroid Build Coastguard WorkerThe individual reads are visible, with the custom format message printed for 50*387f9dfdSAndroid Build Coastguard Workereach read. The parenthesized expression "(arg3 > 20000)" is a filter that is 51*387f9dfdSAndroid Build Coastguard Workerevaluated for each invocation of the probe before printing anything. 52*387f9dfdSAndroid Build Coastguard Worker 53*387f9dfdSAndroid Build Coastguard WorkerEvent message filter is useful while you only interesting the specific event. 54*387f9dfdSAndroid Build Coastguard WorkerLike the program open thousands file and you only want to see the "temp" file 55*387f9dfdSAndroid Build Coastguard Workerand print stack. 56*387f9dfdSAndroid Build Coastguard Worker 57*387f9dfdSAndroid Build Coastguard Worker# trace 'do_sys_open "%s", arg2@user' -UK -f temp 58*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 59*387f9dfdSAndroid Build Coastguard Worker9557 9557 a.out do_sys_open temp.1 60*387f9dfdSAndroid Build Coastguard Worker do_sys_open+0x1 [kernel] 61*387f9dfdSAndroid Build Coastguard Worker do_syscall_64+0x5b [kernel] 62*387f9dfdSAndroid Build Coastguard Worker entry_SYSCALL_64_after_hwframe+0x44 [kernel] 63*387f9dfdSAndroid Build Coastguard Worker __open_nocancel+0x7 [libc-2.17.so] 64*387f9dfdSAndroid Build Coastguard Worker __libc_start_main+0xf5 [libc-2.17.so] 65*387f9dfdSAndroid Build Coastguard Worker9558 9558 a.out do_sys_open temp.2 66*387f9dfdSAndroid Build Coastguard Worker do_sys_open+0x1 [kernel] 67*387f9dfdSAndroid Build Coastguard Worker do_syscall_64+0x5b [kernel] 68*387f9dfdSAndroid Build Coastguard Worker entry_SYSCALL_64_after_hwframe+0x44 [kernel] 69*387f9dfdSAndroid Build Coastguard Worker __open_nocancel+0x7 [libc-2.17.so] 70*387f9dfdSAndroid Build Coastguard Worker __libc_start_main+0xf5 [libc-2.17.so] 71*387f9dfdSAndroid Build Coastguard Worker 72*387f9dfdSAndroid Build Coastguard WorkerProcess name filter is porting from tools/opensnoop 73*387f9dfdSAndroid Build Coastguard Worker 74*387f9dfdSAndroid Build Coastguard Worker# trace 'do_sys_open "%s", arg2@user' -UK -n out 75*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 76*387f9dfdSAndroid Build Coastguard Worker9557 9557 a.out do_sys_open temp.1 77*387f9dfdSAndroid Build Coastguard Worker do_sys_open+0x1 [kernel] 78*387f9dfdSAndroid Build Coastguard Worker do_syscall_64+0x5b [kernel] 79*387f9dfdSAndroid Build Coastguard Worker entry_SYSCALL_64_after_hwframe+0x44 [kernel] 80*387f9dfdSAndroid Build Coastguard Worker __open_nocancel+0x7 [libc-2.17.so] 81*387f9dfdSAndroid Build Coastguard Worker __libc_start_main+0xf5 [libc-2.17.so] 82*387f9dfdSAndroid Build Coastguard Worker 83*387f9dfdSAndroid Build Coastguard WorkerYou can also trace user functions. For example, let's simulate the bashreadline 84*387f9dfdSAndroid Build Coastguard Workerscript, which attaches to the readline function in bash and prints its return 85*387f9dfdSAndroid Build Coastguard Workervalue, effectively snooping all bash shell input across the system: 86*387f9dfdSAndroid Build Coastguard Worker 87*387f9dfdSAndroid Build Coastguard Worker# trace 'r:bash:readline "%s", retval' 88*387f9dfdSAndroid Build Coastguard WorkerPID COMM FUNC - 89*387f9dfdSAndroid Build Coastguard Worker2740 bash readline echo hi! 90*387f9dfdSAndroid Build Coastguard Worker2740 bash readline man ls 91*387f9dfdSAndroid Build Coastguard Worker^C 92*387f9dfdSAndroid Build Coastguard Worker 93*387f9dfdSAndroid Build Coastguard WorkerThe special retval keyword stands for the function's return value, and can 94*387f9dfdSAndroid Build Coastguard Workerbe used only in a retprobe, specified by the 'r' prefix. The next component 95*387f9dfdSAndroid Build Coastguard Workerof the probe is the library that contains the desired function. It's OK to 96*387f9dfdSAndroid Build Coastguard Workerspecify executables too, as long as they can be found in the PATH. Or, you 97*387f9dfdSAndroid Build Coastguard Workercan specify the full path to the executable (e.g. "/usr/bin/bash"). 98*387f9dfdSAndroid Build Coastguard Worker 99*387f9dfdSAndroid Build Coastguard WorkerSometimes it can be useful to see where in code the events happen. There are 100*387f9dfdSAndroid Build Coastguard Workerflags to print the kernel stack (-K), the user stack (-U) and optionally 101*387f9dfdSAndroid Build Coastguard Workerinclude the virtual address in the stacks as well (-a): 102*387f9dfdSAndroid Build Coastguard Worker 103*387f9dfdSAndroid Build Coastguard Worker# trace.py -U -a 'r::sys_futex "%d", retval' 104*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 105*387f9dfdSAndroid Build Coastguard Worker793922 793951 poller sys_futex 0 106*387f9dfdSAndroid Build Coastguard Worker 7f6c72b6497a __lll_unlock_wake+0x1a [libpthread-2.23.so] 107*387f9dfdSAndroid Build Coastguard Worker 627fef folly::FunctionScheduler::run()+0x46f [router] 108*387f9dfdSAndroid Build Coastguard Worker 7f6c7345f171 execute_native_thread_routine+0x21 [libstdc++.so.6.0.21] 109*387f9dfdSAndroid Build Coastguard Worker 7f6c72b5b7a9 start_thread+0xd9 [libpthread-2.23.so] 110*387f9dfdSAndroid Build Coastguard Worker 7f6c7223fa7d clone+0x6d [libc-2.23.so] 111*387f9dfdSAndroid Build Coastguard Worker 112*387f9dfdSAndroid Build Coastguard WorkerMultiple probes can be combined on the same command line. For example, let's 113*387f9dfdSAndroid Build Coastguard Workertrace failed read and write calls on the libc level, and include a time column: 114*387f9dfdSAndroid Build Coastguard Worker 115*387f9dfdSAndroid Build Coastguard Worker# trace 'r:c:read ((int)retval < 0) "read failed: %d", retval' \ 116*387f9dfdSAndroid Build Coastguard Worker 'r:c:write ((int)retval < 0) "write failed: %d", retval' -T 117*387f9dfdSAndroid Build Coastguard WorkerTIME PID COMM FUNC - 118*387f9dfdSAndroid Build Coastguard Worker05:31:57 3388 bash write write failed: -1 119*387f9dfdSAndroid Build Coastguard Worker05:32:00 3388 bash write write failed: -1 120*387f9dfdSAndroid Build Coastguard Worker^C 121*387f9dfdSAndroid Build Coastguard Worker 122*387f9dfdSAndroid Build Coastguard WorkerNote that the retval variable must be cast to int before comparing to zero. 123*387f9dfdSAndroid Build Coastguard WorkerThe reason is that the default type for argN and retval is an unsigned 64-bit 124*387f9dfdSAndroid Build Coastguard Workerinteger, which can never be smaller than 0. 125*387f9dfdSAndroid Build Coastguard Worker 126*387f9dfdSAndroid Build Coastguard Workertrace has also some basic support for kernel tracepoints. For example, let's 127*387f9dfdSAndroid Build Coastguard Workertrace the block:block_rq_complete tracepoint and print out the number of sectors 128*387f9dfdSAndroid Build Coastguard Workertransferred: 129*387f9dfdSAndroid Build Coastguard Worker 130*387f9dfdSAndroid Build Coastguard Worker# trace 't:block:block_rq_complete "sectors=%d", args->nr_sector' -T 131*387f9dfdSAndroid Build Coastguard WorkerTIME PID COMM FUNC - 132*387f9dfdSAndroid Build Coastguard Worker01:23:51 0 swapper/0 block_rq_complete sectors=8 133*387f9dfdSAndroid Build Coastguard Worker01:23:55 10017 kworker/u64: block_rq_complete sectors=1 134*387f9dfdSAndroid Build Coastguard Worker01:23:55 0 swapper/0 block_rq_complete sectors=8 135*387f9dfdSAndroid Build Coastguard Worker^C 136*387f9dfdSAndroid Build Coastguard Worker 137*387f9dfdSAndroid Build Coastguard WorkerSuppose that you want to trace a system-call in a short-lived process, you can use 138*387f9dfdSAndroid Build Coastguard Workerthe -s option to trace. The option is followed by list of libraries/executables to 139*387f9dfdSAndroid Build Coastguard Workeruse for symbol resolution. 140*387f9dfdSAndroid Build Coastguard Worker# trace -s /lib/x86_64-linux-gnu/libc.so.6,/bin/ping 'p:c:inet_pton' -U 141*387f9dfdSAndroid Build Coastguard WorkerNote: Kernel bpf will report stack map with ip/build_id 142*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC 143*387f9dfdSAndroid Build Coastguard Worker4175 4175 ping inet_pton 144*387f9dfdSAndroid Build Coastguard Worker inet_pton+0x136340 [libc.so.6] 145*387f9dfdSAndroid Build Coastguard Worker getaddrinfo+0xfb510 [libc.so.6] 146*387f9dfdSAndroid Build Coastguard Worker _init+0x2a08 [ping] 147*387f9dfdSAndroid Build Coastguard Worker 148*387f9dfdSAndroid Build Coastguard WorkerDuring the trace, 'ping -c1 google.com' was executed to obtain the above results 149*387f9dfdSAndroid Build Coastguard Worker 150*387f9dfdSAndroid Build Coastguard WorkerTo discover the tracepoint structure format (which you can refer to as the "args" 151*387f9dfdSAndroid Build Coastguard Workerpointer variable), use the tplist tool. For example: 152*387f9dfdSAndroid Build Coastguard Worker 153*387f9dfdSAndroid Build Coastguard Worker# tplist -v block:block_rq_complete 154*387f9dfdSAndroid Build Coastguard Workerblock:block_rq_complete 155*387f9dfdSAndroid Build Coastguard Worker dev_t dev; 156*387f9dfdSAndroid Build Coastguard Worker sector_t sector; 157*387f9dfdSAndroid Build Coastguard Worker unsigned int nr_sector; 158*387f9dfdSAndroid Build Coastguard Worker int errors; 159*387f9dfdSAndroid Build Coastguard Worker char rwbs[8]; 160*387f9dfdSAndroid Build Coastguard Worker 161*387f9dfdSAndroid Build Coastguard WorkerThis output tells you that you can use "args->dev", "args->sector", etc. in your 162*387f9dfdSAndroid Build Coastguard Workerpredicate and trace arguments. 163*387f9dfdSAndroid Build Coastguard Worker 164*387f9dfdSAndroid Build Coastguard Worker 165*387f9dfdSAndroid Build Coastguard WorkerMore and more high-level libraries are instrumented with USDT probe support. 166*387f9dfdSAndroid Build Coastguard WorkerThese probes can be traced by trace just like kernel tracepoints. For example, 167*387f9dfdSAndroid Build Coastguard Workertrace new threads being created and their function name, include time column 168*387f9dfdSAndroid Build Coastguard Workerand on which CPU it happened: 169*387f9dfdSAndroid Build Coastguard Worker 170*387f9dfdSAndroid Build Coastguard Worker# trace 'u:pthread:pthread_create "%U", arg3' -T -C 171*387f9dfdSAndroid Build Coastguard WorkerTIME CPU PID TID COMM FUNC - 172*387f9dfdSAndroid Build Coastguard Worker13:22:01 25 2627 2629 automount pthread_create expire_proc_indirect+0x0 [automount] 173*387f9dfdSAndroid Build Coastguard Worker13:22:01 5 21360 21414 osqueryd pthread_create [unknown] [osqueryd] 174*387f9dfdSAndroid Build Coastguard Worker13:22:03 25 2627 2629 automount pthread_create expire_proc_indirect+0x0 [automount] 175*387f9dfdSAndroid Build Coastguard Worker13:22:04 15 21360 21414 osqueryd pthread_create [unknown] [osqueryd] 176*387f9dfdSAndroid Build Coastguard Worker13:22:07 25 2627 2629 automount pthread_create expire_proc_indirect+0x0 [automount] 177*387f9dfdSAndroid Build Coastguard Worker13:22:07 4 21360 21414 osqueryd pthread_create [unknown] [osqueryd] 178*387f9dfdSAndroid Build Coastguard Worker^C 179*387f9dfdSAndroid Build Coastguard Worker 180*387f9dfdSAndroid Build Coastguard WorkerThe "%U" format specifier tells trace to resolve arg3 as a user-space symbol, 181*387f9dfdSAndroid Build Coastguard Workerif possible. Similarly, use "%K" for kernel symbols. 182*387f9dfdSAndroid Build Coastguard Worker 183*387f9dfdSAndroid Build Coastguard WorkerRuby, Node, and OpenJDK are also instrumented with USDT. For example, let's 184*387f9dfdSAndroid Build Coastguard Workertrace Ruby methods being called (this requires a version of Ruby built with 185*387f9dfdSAndroid Build Coastguard Workerthe --enable-dtrace configure flag): 186*387f9dfdSAndroid Build Coastguard Worker 187*387f9dfdSAndroid Build Coastguard Worker# trace 'u:ruby:method__entry "%s.%s", arg1, arg2' -p $(pidof irb) -T 188*387f9dfdSAndroid Build Coastguard WorkerTIME PID COMM FUNC - 189*387f9dfdSAndroid Build Coastguard Worker12:08:43 18420 irb method__entry IRB::Context.verbose? 190*387f9dfdSAndroid Build Coastguard Worker12:08:43 18420 irb method__entry RubyLex.ungetc 191*387f9dfdSAndroid Build Coastguard Worker12:08:43 18420 irb method__entry RuxyLex.debug? 192*387f9dfdSAndroid Build Coastguard Worker^C 193*387f9dfdSAndroid Build Coastguard Worker 194*387f9dfdSAndroid Build Coastguard WorkerIn the previous invocation, arg1 and arg2 are the class name and method name 195*387f9dfdSAndroid Build Coastguard Workerfor the Ruby method being invoked. 196*387f9dfdSAndroid Build Coastguard Worker 197*387f9dfdSAndroid Build Coastguard WorkerYou can also trace exported functions from shared libraries, or an imported 198*387f9dfdSAndroid Build Coastguard Workerfunction on the actual executable: 199*387f9dfdSAndroid Build Coastguard Worker 200*387f9dfdSAndroid Build Coastguard Worker# sudo ./trace.py 'r:/usr/lib64/libtinfo.so:curses_version "Version=%s", retval' 201*387f9dfdSAndroid Build Coastguard Worker# tput -V 202*387f9dfdSAndroid Build Coastguard Worker 203*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 204*387f9dfdSAndroid Build Coastguard Worker21720 21720 tput curses_version Version=ncurses 6.0.20160709 205*387f9dfdSAndroid Build Coastguard Worker^C 206*387f9dfdSAndroid Build Coastguard Worker 207*387f9dfdSAndroid Build Coastguard Worker 208*387f9dfdSAndroid Build Coastguard WorkerOccasionally, it can be useful to filter specific strings. For example, you 209*387f9dfdSAndroid Build Coastguard Workermight be interested in open() calls that open a specific file: 210*387f9dfdSAndroid Build Coastguard Worker 211*387f9dfdSAndroid Build Coastguard Worker# trace 'p:c:open (STRCMP("test.txt", arg1)) "opening %s", arg1' -T 212*387f9dfdSAndroid Build Coastguard WorkerTIME PID COMM FUNC - 213*387f9dfdSAndroid Build Coastguard Worker01:43:15 10938 cat open opening test.txt 214*387f9dfdSAndroid Build Coastguard Worker01:43:20 10939 cat open opening test.txt 215*387f9dfdSAndroid Build Coastguard Worker^C 216*387f9dfdSAndroid Build Coastguard Worker 217*387f9dfdSAndroid Build Coastguard Worker 218*387f9dfdSAndroid Build Coastguard WorkerIn the preceding example, as well as in many others, readability may be 219*387f9dfdSAndroid Build Coastguard Workerimproved by providing the function's signature, which names the arguments and 220*387f9dfdSAndroid Build Coastguard Workerlets you access structure sub-fields, which is hard with the "arg1", "arg2" 221*387f9dfdSAndroid Build Coastguard Workerconvention. For example: 222*387f9dfdSAndroid Build Coastguard Worker 223*387f9dfdSAndroid Build Coastguard Worker# trace 'p:c:open(char *filename) "opening %s", filename' 224*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 225*387f9dfdSAndroid Build Coastguard Worker17507 17507 cat open opening FAQ.txt 226*387f9dfdSAndroid Build Coastguard Worker^C 227*387f9dfdSAndroid Build Coastguard Worker 228*387f9dfdSAndroid Build Coastguard Worker# trace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec' 229*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 230*387f9dfdSAndroid Build Coastguard Worker777 785 automount SyS_nanosleep sleep for 500000000 ns 231*387f9dfdSAndroid Build Coastguard Worker777 785 automount SyS_nanosleep sleep for 500000000 ns 232*387f9dfdSAndroid Build Coastguard Worker777 785 automount SyS_nanosleep sleep for 500000000 ns 233*387f9dfdSAndroid Build Coastguard Worker777 785 automount SyS_nanosleep sleep for 500000000 ns 234*387f9dfdSAndroid Build Coastguard Worker^C 235*387f9dfdSAndroid Build Coastguard Worker 236*387f9dfdSAndroid Build Coastguard WorkerRemember to use the -I argument include the appropriate header file. We didn't 237*387f9dfdSAndroid Build Coastguard Workerneed to do that here because `struct timespec` is used internally by the tool, 238*387f9dfdSAndroid Build Coastguard Workerso it always includes this header file. 239*387f9dfdSAndroid Build Coastguard Worker 240*387f9dfdSAndroid Build Coastguard WorkerTo aggregate amount of trace, you need specify -A with -M EVENTS. A typical 241*387f9dfdSAndroid Build Coastguard Workerexample: 242*387f9dfdSAndroid Build Coastguard Worker1, if we find that the sys CPU utilization is higher by 'top' command 243*387f9dfdSAndroid Build Coastguard Worker2, then find that the timer interrupt is more normal by 'irqtop' command 244*387f9dfdSAndroid Build Coastguard Worker3, to confirm kernel timer setting frequence by 'funccount -i 1 clockevents_program_event' 245*387f9dfdSAndroid Build Coastguard Worker4, to trace timer setting by 'trace clockevents_program_event -K -A -M 1000' 246*387f9dfdSAndroid Build Coastguard Worker 247*387f9dfdSAndroid Build Coastguard Worker1294576 1294584 CPU 0/KVM clockevents_program_event 248*387f9dfdSAndroid Build Coastguard Worker clockevents_program_event+0x1 [kernel] 249*387f9dfdSAndroid Build Coastguard Worker hrtimer_start_range_ns+0x209 [kernel] 250*387f9dfdSAndroid Build Coastguard Worker start_sw_timer+0x173 [kvm] 251*387f9dfdSAndroid Build Coastguard Worker restart_apic_timer+0x6c [kvm] 252*387f9dfdSAndroid Build Coastguard Worker kvm_set_msr_common+0x442 [kvm] 253*387f9dfdSAndroid Build Coastguard Worker __kvm_set_msr+0xa2 [kvm] 254*387f9dfdSAndroid Build Coastguard Worker kvm_emulate_wrmsr+0x36 [kvm] 255*387f9dfdSAndroid Build Coastguard Worker vcpu_enter_guest+0x326 [kvm] 256*387f9dfdSAndroid Build Coastguard Worker kvm_arch_vcpu_ioctl_run+0xcc [kvm] 257*387f9dfdSAndroid Build Coastguard Worker kvm_vcpu_ioctl+0x22f [kvm] 258*387f9dfdSAndroid Build Coastguard Worker do_vfs_ioctl+0xa1 [kernel] 259*387f9dfdSAndroid Build Coastguard Worker ksys_ioctl+0x60 [kernel] 260*387f9dfdSAndroid Build Coastguard Worker __x64_sys_ioctl+0x16 [kernel] 261*387f9dfdSAndroid Build Coastguard Worker do_syscall_64+0x59 [kernel] 262*387f9dfdSAndroid Build Coastguard Worker entry_SYSCALL_64_after_hwframe+0x44 [kernel] 263*387f9dfdSAndroid Build Coastguard Worker-->COUNT 271 264*387f9dfdSAndroid Build Coastguard Worker... 265*387f9dfdSAndroid Build Coastguard WorkerSo we can know that 271 timer setting in recent 1000(~27%). 266*387f9dfdSAndroid Build Coastguard Worker 267*387f9dfdSAndroid Build Coastguard WorkerAs a final example, let's trace open syscalls for a specific process. By 268*387f9dfdSAndroid Build Coastguard Workerdefault, tracing is system-wide, but the -p switch overrides this: 269*387f9dfdSAndroid Build Coastguard Worker 270*387f9dfdSAndroid Build Coastguard Worker# trace -p 2740 'do_sys_open "%s", arg2@user' -T 271*387f9dfdSAndroid Build Coastguard WorkerTIME PID COMM FUNC - 272*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /etc/ld.so.cache 273*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libselinux.so.1 274*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libcap.so.2 275*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libacl.so.1 276*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libc.so.6 277*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libpcre.so.1 278*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libdl.so.2 279*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libattr.so.1 280*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /lib64/libpthread.so.0 281*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /usr/lib/locale/locale-archive 282*387f9dfdSAndroid Build Coastguard Worker05:36:16 15872 ls do_sys_open /home/vagrant 283*387f9dfdSAndroid Build Coastguard Worker^C 284*387f9dfdSAndroid Build Coastguard Worker 285*387f9dfdSAndroid Build Coastguard WorkerIn this example, we traced the "ls ~" command as it was opening its shared 286*387f9dfdSAndroid Build Coastguard Workerlibraries and then accessing the /home/vagrant directory listing. 287*387f9dfdSAndroid Build Coastguard Worker 288*387f9dfdSAndroid Build Coastguard Worker 289*387f9dfdSAndroid Build Coastguard WorkerLastly, if a high-frequency event is traced you may overflow the perf ring 290*387f9dfdSAndroid Build Coastguard Workerbuffer. This shows as "Lost N samples": 291*387f9dfdSAndroid Build Coastguard Worker 292*387f9dfdSAndroid Build Coastguard Worker# trace sys_open 293*387f9dfdSAndroid Build Coastguard Worker5087 5087 pgrep sys_open 294*387f9dfdSAndroid Build Coastguard Worker5087 5087 pgrep sys_open 295*387f9dfdSAndroid Build Coastguard Worker5087 5087 pgrep sys_open 296*387f9dfdSAndroid Build Coastguard Worker5087 5087 pgrep sys_open 297*387f9dfdSAndroid Build Coastguard Worker5087 5087 pgrep sys_open 298*387f9dfdSAndroid Build Coastguard WorkerLost 764896 samples 299*387f9dfdSAndroid Build Coastguard WorkerLost 764896 samples 300*387f9dfdSAndroid Build Coastguard WorkerLost 764896 samples 301*387f9dfdSAndroid Build Coastguard Worker 302*387f9dfdSAndroid Build Coastguard WorkerThe perf ring buffer size can be changed with -b. The unit is size per-CPU buffer 303*387f9dfdSAndroid Build Coastguard Workersize and is measured in pages. The value must be a power of two and defaults to 304*387f9dfdSAndroid Build Coastguard Worker64 pages. 305*387f9dfdSAndroid Build Coastguard Worker 306*387f9dfdSAndroid Build Coastguard Worker# trace.py 'sys_setsockopt(int fd, int level, int optname, char* optval, int optlen)(level==0 && optname == 1 && STRCMP("{0x6C, 0x00, 0x00, 0x00}", optval))' -U -M 1 --bin_cmp 307*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 308*387f9dfdSAndroid Build Coastguard Worker1855611 1863183 worker sys_setsockopt found 309*387f9dfdSAndroid Build Coastguard Worker 310*387f9dfdSAndroid Build Coastguard WorkerIn this example we are catching setsockopt syscall to change IPv4 IP_TOS 311*387f9dfdSAndroid Build Coastguard Workervalue only for the cases where new TOS value is equal to 108. we are using 312*387f9dfdSAndroid Build Coastguard WorkerSTRCMP helper in binary mode (--bin_cmp flag) to compare optval array 313*387f9dfdSAndroid Build Coastguard Workeragainst int value of 108 (parametr of setsockopt call) in hex representation 314*387f9dfdSAndroid Build Coastguard Worker(little endian format) 315*387f9dfdSAndroid Build Coastguard Worker 316*387f9dfdSAndroid Build Coastguard WorkerFor advanced users there is a possibility to insert the kprobes or uprobes 317*387f9dfdSAndroid Build Coastguard Workerafter a certain offset, rather than the start of the function call 318*387f9dfdSAndroid Build Coastguard WorkerThis is useful for tracing register values at different places of the 319*387f9dfdSAndroid Build Coastguard Workerexecution of a function. Lets consider the following example: 320*387f9dfdSAndroid Build Coastguard Worker 321*387f9dfdSAndroid Build Coastguard Workerint main() 322*387f9dfdSAndroid Build Coastguard Worker{ 323*387f9dfdSAndroid Build Coastguard Worker int val = 0xdead; 324*387f9dfdSAndroid Build Coastguard Worker printf("%d\n", val); 325*387f9dfdSAndroid Build Coastguard Worker val = 0xbeef; 326*387f9dfdSAndroid Build Coastguard Worker printf("%d\n", val); 327*387f9dfdSAndroid Build Coastguard Worker} 328*387f9dfdSAndroid Build Coastguard Worker 329*387f9dfdSAndroid Build Coastguard WorkerAfter compiling the code with -O3 optimization the object code looks 330*387f9dfdSAndroid Build Coastguard Workerlike the following (with GCC 10 and x86_64 architecture): 331*387f9dfdSAndroid Build Coastguard Worker 332*387f9dfdSAndroid Build Coastguard Workerobjdump --disassemble=main --prefix-addresses a.out 333*387f9dfdSAndroid Build Coastguard Worker 334*387f9dfdSAndroid Build Coastguard Worker0000000000001060 <main> endbr64 335*387f9dfdSAndroid Build Coastguard Worker0000000000001064 <main+0x4> sub $0x8,%rsp 336*387f9dfdSAndroid Build Coastguard Worker0000000000001068 <main+0x8> mov $0xdead,%edx 337*387f9dfdSAndroid Build Coastguard Worker000000000000106d <main+0xd> mov $0x1,%edi 338*387f9dfdSAndroid Build Coastguard Worker0000000000001072 <main+0x12> xor %eax,%eax 339*387f9dfdSAndroid Build Coastguard Worker0000000000001074 <main+0x14> lea 0xf89(%rip),%rsi 340*387f9dfdSAndroid Build Coastguard Worker000000000000107b <main+0x1b> callq 0000000000001050 <__printf_chk@plt> 341*387f9dfdSAndroid Build Coastguard Worker0000000000001080 <main+0x20> mov $0xbeef,%edx 342*387f9dfdSAndroid Build Coastguard Worker0000000000001085 <main+0x25> lea 0xf78(%rip),%rsi 343*387f9dfdSAndroid Build Coastguard Worker000000000000108c <main+0x2c> xor %eax,%eax 344*387f9dfdSAndroid Build Coastguard Worker000000000000108e <main+0x2e> mov $0x1,%edi 345*387f9dfdSAndroid Build Coastguard Worker0000000000001093 <main+0x33> callq 0000000000001050 <__printf_chk@plt> 346*387f9dfdSAndroid Build Coastguard Worker0000000000001098 <main+0x38> xor %eax,%eax 347*387f9dfdSAndroid Build Coastguard Worker000000000000109a <main+0x3a> add $0x8,%rsp 348*387f9dfdSAndroid Build Coastguard Worker000000000000109e <main+0x3e> retq 349*387f9dfdSAndroid Build Coastguard Worker 350*387f9dfdSAndroid Build Coastguard WorkerThe 0xdead and later the 0xbeef values are moved into the edx register. 351*387f9dfdSAndroid Build Coastguard WorkerAs the disassembly shows the edx register contains the 0xdead value 352*387f9dfdSAndroid Build Coastguard Workerafter the 0xd offset and 0xbeef after the 0x25 offset. To verify this 353*387f9dfdSAndroid Build Coastguard Workerwith trace lets insert probes to those offsets. The following 354*387f9dfdSAndroid Build Coastguard Workercommand inserts two uprobe one after the 0xd offset and another one 355*387f9dfdSAndroid Build Coastguard Workerafter the 0x25 offset of the main function. The probe print the 356*387f9dfdSAndroid Build Coastguard Workervalue of the edx register which will show us the correct values. 357*387f9dfdSAndroid Build Coastguard Worker 358*387f9dfdSAndroid Build Coastguard Workertrace 'p:/tmp/a.out:main+0xd "%x", ctx->dx' 'p:/tmp/a.out:main+0x25 "%x", ctx->dx' 359*387f9dfdSAndroid Build Coastguard WorkerPID TID COMM FUNC - 360*387f9dfdSAndroid Build Coastguard Worker25754 25754 a.out main dead 361*387f9dfdSAndroid Build Coastguard Worker25754 25754 a.out main beef 362*387f9dfdSAndroid Build Coastguard Worker 363*387f9dfdSAndroid Build Coastguard Worker 364*387f9dfdSAndroid Build Coastguard WorkerUSAGE message: 365*387f9dfdSAndroid Build Coastguard Worker 366*387f9dfdSAndroid Build Coastguard Workerusage: trace [-h] [-b BUFFER_PAGES] [-p PID] [-L TID] [--uid UID] [-v] 367*387f9dfdSAndroid Build Coastguard Worker [-Z STRING_SIZE] [-S] [-M MAX_EVENTS] [-t] [-u] [-T] [-C] 368*387f9dfdSAndroid Build Coastguard Worker [-c CGROUP_PATH] [-n NAME] [-f MSG_FILTER] [-B] 369*387f9dfdSAndroid Build Coastguard Worker [-s SYM_FILE_LIST] [-K] [-U] [-a] [-I header] 370*387f9dfdSAndroid Build Coastguard Worker probe [probe ...] 371*387f9dfdSAndroid Build Coastguard Worker 372*387f9dfdSAndroid Build Coastguard WorkerAttach to functions and print trace messages. 373*387f9dfdSAndroid Build Coastguard Worker 374*387f9dfdSAndroid Build Coastguard Workerpositional arguments: 375*387f9dfdSAndroid Build Coastguard Worker probe probe specifier (see examples) 376*387f9dfdSAndroid Build Coastguard Worker 377*387f9dfdSAndroid Build Coastguard Workeroptional arguments: 378*387f9dfdSAndroid Build Coastguard Worker -h, --help show this help message and exit 379*387f9dfdSAndroid Build Coastguard Worker -b BUFFER_PAGES, --buffer-pages BUFFER_PAGES 380*387f9dfdSAndroid Build Coastguard Worker number of pages to use for perf_events ring buffer 381*387f9dfdSAndroid Build Coastguard Worker (default: 64) 382*387f9dfdSAndroid Build Coastguard Worker -p PID, --pid PID id of the process to trace (optional) 383*387f9dfdSAndroid Build Coastguard Worker -L TID, --tid TID id of the thread to trace (optional) 384*387f9dfdSAndroid Build Coastguard Worker --uid UID id of the user to trace (optional) 385*387f9dfdSAndroid Build Coastguard Worker -v, --verbose print resulting BPF program code before executing 386*387f9dfdSAndroid Build Coastguard Worker -Z STRING_SIZE, --string-size STRING_SIZE 387*387f9dfdSAndroid Build Coastguard Worker maximum size to read from strings 388*387f9dfdSAndroid Build Coastguard Worker -S, --include-self do not filter trace's own pid from the trace 389*387f9dfdSAndroid Build Coastguard Worker -M MAX_EVENTS, --max-events MAX_EVENTS 390*387f9dfdSAndroid Build Coastguard Worker number of events to print before quitting 391*387f9dfdSAndroid Build Coastguard Worker -t, --timestamp print timestamp column (offset from trace start) 392*387f9dfdSAndroid Build Coastguard Worker -u, --unix-timestamp print UNIX timestamp instead of offset from trace 393*387f9dfdSAndroid Build Coastguard Worker start, requires -t 394*387f9dfdSAndroid Build Coastguard Worker -T, --time print time column 395*387f9dfdSAndroid Build Coastguard Worker -C, --print_cpu print CPU id 396*387f9dfdSAndroid Build Coastguard Worker -c CGROUP_PATH, --cgroup-path CGROUP_PATH 397*387f9dfdSAndroid Build Coastguard Worker cgroup path 398*387f9dfdSAndroid Build Coastguard Worker -n NAME, --name NAME only print process names containing this name 399*387f9dfdSAndroid Build Coastguard Worker -f MSG_FILTER, --msg-filter MSG_FILTER 400*387f9dfdSAndroid Build Coastguard Worker only print the msg of event containing this string 401*387f9dfdSAndroid Build Coastguard Worker -B, --bin_cmp allow to use STRCMP with binary values 402*387f9dfdSAndroid Build Coastguard Worker -s SYM_FILE_LIST, --sym_file_list SYM_FILE_LIST 403*387f9dfdSAndroid Build Coastguard Worker comma separated list of symbol files to use for symbol 404*387f9dfdSAndroid Build Coastguard Worker resolution 405*387f9dfdSAndroid Build Coastguard Worker -K, --kernel-stack output kernel stack trace 406*387f9dfdSAndroid Build Coastguard Worker -U, --user-stack output user stack trace 407*387f9dfdSAndroid Build Coastguard Worker -a, --address print virtual address in stacks 408*387f9dfdSAndroid Build Coastguard Worker -I header, --include header 409*387f9dfdSAndroid Build Coastguard Worker additional header files to include in the BPF program 410*387f9dfdSAndroid Build Coastguard Worker as either full path, or relative to current working 411*387f9dfdSAndroid Build Coastguard Worker directory, or relative to default kernel header search 412*387f9dfdSAndroid Build Coastguard Worker path 413*387f9dfdSAndroid Build Coastguard Worker -A, --aggregate aggregate amount of each trace 414*387f9dfdSAndroid Build Coastguard Worker 415*387f9dfdSAndroid Build Coastguard WorkerEXAMPLES: 416*387f9dfdSAndroid Build Coastguard Worker 417*387f9dfdSAndroid Build Coastguard Workertrace do_sys_open 418*387f9dfdSAndroid Build Coastguard Worker Trace the open syscall and print a default trace message when entered 419*387f9dfdSAndroid Build Coastguard Workertrace kfree_skb+0x12 420*387f9dfdSAndroid Build Coastguard Worker Trace the kfree_skb kernel function after the instruction on the 0x12 offset 421*387f9dfdSAndroid Build Coastguard Workertrace 'do_sys_open "%s", arg2@user' 422*387f9dfdSAndroid Build Coastguard Worker Trace the open syscall and print the filename being opened @user is 423*387f9dfdSAndroid Build Coastguard Worker added to arg2 in kprobes to ensure that char * should be copied from 424*387f9dfdSAndroid Build Coastguard Worker the userspace stack to the bpf stack. If not specified, previous 425*387f9dfdSAndroid Build Coastguard Worker behaviour is expected. 426*387f9dfdSAndroid Build Coastguard Worker 427*387f9dfdSAndroid Build Coastguard Workertrace 'do_sys_open "%s", arg2@user' -n main 428*387f9dfdSAndroid Build Coastguard Worker Trace the open syscall and only print event that process names containing "main" 429*387f9dfdSAndroid Build Coastguard Workertrace 'do_sys_open "%s", arg2@user' --uid 1001 430*387f9dfdSAndroid Build Coastguard Worker Trace the open syscall and only print event that processes with user ID 1001 431*387f9dfdSAndroid Build Coastguard Workertrace 'do_sys_open "%s", arg2@user' -f config 432*387f9dfdSAndroid Build Coastguard Worker Trace the open syscall and print the filename being opened filtered by "config" 433*387f9dfdSAndroid Build Coastguard Workertrace 'sys_read (arg3 > 20000) "read %d bytes", arg3' 434*387f9dfdSAndroid Build Coastguard Worker Trace the read syscall and print a message for reads >20000 bytes 435*387f9dfdSAndroid Build Coastguard Workertrace 'r::do_sys_open "%llx", retval' 436*387f9dfdSAndroid Build Coastguard Worker Trace the return from the open syscall and print the return value 437*387f9dfdSAndroid Build Coastguard Workertrace 'c:open (arg2 == 42) "%s %d", arg1, arg2' 438*387f9dfdSAndroid Build Coastguard Worker Trace the open() call from libc only if the flags (arg2) argument is 42 439*387f9dfdSAndroid Build Coastguard Workertrace 'c:malloc "size = %d", arg1' 440*387f9dfdSAndroid Build Coastguard Worker Trace malloc calls and print the size being allocated 441*387f9dfdSAndroid Build Coastguard Workertrace 'p:c:write (arg1 == 1) "writing %d bytes to STDOUT", arg3' 442*387f9dfdSAndroid Build Coastguard Worker Trace the write() call from libc to monitor writes to STDOUT 443*387f9dfdSAndroid Build Coastguard Workertrace 'r::__kmalloc (retval == 0) "kmalloc failed!"' 444*387f9dfdSAndroid Build Coastguard Worker Trace returns from __kmalloc which returned a null pointer 445*387f9dfdSAndroid Build Coastguard Workertrace 'r:c:malloc (retval) "allocated = %x", retval' 446*387f9dfdSAndroid Build Coastguard Worker Trace returns from malloc and print non-NULL allocated buffers 447*387f9dfdSAndroid Build Coastguard Workertrace 't:block:block_rq_complete "sectors=%d", args->nr_sector' 448*387f9dfdSAndroid Build Coastguard Worker Trace the block_rq_complete kernel tracepoint and print # of tx sectors 449*387f9dfdSAndroid Build Coastguard Workertrace 'u:pthread:pthread_create (arg4 != 0)' 450*387f9dfdSAndroid Build Coastguard Worker Trace the USDT probe pthread_create when its 4th argument is non-zero 451*387f9dfdSAndroid Build Coastguard Workertrace 'u:pthread:libpthread:pthread_create (arg4 != 0)' 452*387f9dfdSAndroid Build Coastguard Worker Ditto, but the provider name "libpthread" is specified. 453*387f9dfdSAndroid Build Coastguard Workertrace 'p::SyS_nanosleep(struct timespec *ts) "sleep for %lld ns", ts->tv_nsec' 454*387f9dfdSAndroid Build Coastguard Worker Trace the nanosleep syscall and print the sleep duration in ns 455*387f9dfdSAndroid Build Coastguard Workertrace -c /sys/fs/cgroup/system.slice/workload.service '__x64_sys_nanosleep' '__x64_sys_clone' 456*387f9dfdSAndroid Build Coastguard Worker Trace nanosleep/clone syscall calls only under workload.service 457*387f9dfdSAndroid Build Coastguard Worker cgroup hierarchy. 458*387f9dfdSAndroid Build Coastguard Workertrace -I 'linux/fs.h' \ 459*387f9dfdSAndroid Build Coastguard Worker 'p::uprobe_register(struct inode *inode) "a_ops = %llx", inode->i_mapping->a_ops' 460*387f9dfdSAndroid Build Coastguard Worker Trace the uprobe_register inode mapping ops, and the symbol can be found 461*387f9dfdSAndroid Build Coastguard Worker in /proc/kallsyms 462*387f9dfdSAndroid Build Coastguard Workertrace -I 'kernel/sched/sched.h' \ 463*387f9dfdSAndroid Build Coastguard Worker 'p::__account_cfs_rq_runtime(struct cfs_rq *cfs_rq) "%d", cfs_rq->runtime_remaining' 464*387f9dfdSAndroid Build Coastguard Worker Trace the cfs scheduling runqueue remaining runtime. The struct cfs_rq is defined 465*387f9dfdSAndroid Build Coastguard Worker in kernel/sched/sched.h which is in kernel source tree and not in kernel-devel 466*387f9dfdSAndroid Build Coastguard Worker package. So this command needs to run at the kernel source tree root directory 467*387f9dfdSAndroid Build Coastguard Worker so that the added header file can be found by the compiler. 468*387f9dfdSAndroid Build Coastguard Workertrace -I 'net/sock.h' \ 469*387f9dfdSAndroid Build Coastguard Worker 'udpv6_sendmsg(struct sock *sk) (sk->sk_dport == 13568)' 470*387f9dfdSAndroid Build Coastguard Worker Trace udpv6 sendmsg calls only if socket's destination port is equal 471*387f9dfdSAndroid Build Coastguard Worker to 53 (DNS; 13568 in big endian order) 472*387f9dfdSAndroid Build Coastguard Workertrace -I 'linux/fs_struct.h' 'mntns_install "users = %d", $task->fs->users' 473*387f9dfdSAndroid Build Coastguard Worker Trace the number of users accessing the file system of the current task 474*387f9dfdSAndroid Build Coastguard Workertrace -s /lib/x86_64-linux-gnu/libc.so.6,/bin/ping 'p:c:inet_pton' -U 475*387f9dfdSAndroid Build Coastguard Worker Trace inet_pton system call and use the specified libraries/executables for 476*387f9dfdSAndroid Build Coastguard Worker symbol resolution. 477