| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
// configuration options |
| 4 |
global follow_fork = 0 // -Gfollow_fork=1 means trace descendant processes too |
| 5 |
global count = 0 // -Gcount=1 means summarize time and calls for each library call */ |
| 6 |
|
| 7 |
global report_line% |
| 8 |
global report_line_returnval% |
| 9 |
global func_time% |
| 10 |
global func_time_total% |
| 11 |
global func_calls% |
| 12 |
|
| 13 |
global calls_nonreturn |
| 14 |
probe begin |
| 15 |
{ |
| 16 |
// list of calls that never .return |
| 17 |
calls_nonreturn["__libc_start_main"]=1 |
| 18 |
} |
| 19 |
|
| 20 |
function filter_p() |
| 21 |
{ |
| 22 |
// from strace.stp |
| 23 |
if (target() == 0) return 0; |
| 24 |
if (!follow_fork && pid() != target()) return 1; |
| 25 |
if (follow_fork && !target_set_pid(pid())) return 1; |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
function arg:string (arg1:long) { |
| 30 |
if (arg1 < 255) |
| 31 |
return sprintf("%d", arg1) |
| 32 |
else |
| 33 |
return sprintf("%#lx", arg1) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
function report (rl) |
| 38 |
{ |
| 39 |
printf("%s", report_line[rl]) |
| 40 |
rll = strlen(report_line[rl]) |
| 41 |
n_tabs = ((64 - rll) / 8) |
| 42 |
if (rll % 8 == 0) |
| 43 |
n_tabs -= 1 |
| 44 |
if (rll <= 64) |
| 45 |
for (i=0; i < n_tabs; i++) |
| 46 |
{ |
| 47 |
printf("\t") |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
probe process.plt, process.library("*").plt |
| 53 |
{ |
| 54 |
if (filter_p()) next; |
| 55 |
if (strlen($$name) == 0) next; |
| 56 |
|
| 57 |
if ($$name in calls_nonreturn) |
| 58 |
next |
| 59 |
|
| 60 |
if (count) // -Gcount=1 |
| 61 |
{ |
| 62 |
func_calls[$$name] <<< 1 |
| 63 |
func_time[$$name]=gettimeofday_us() |
| 64 |
next |
| 65 |
} |
| 66 |
|
| 67 |
%( arch == "x86_64" %? |
| 68 |
outline = sprintf("%s (%s, %s, %s, %s)", $$name, arg(register("rdi")), arg(register("rsi")), arg(register("rdx")), arg(register("rcx"))) |
| 69 |
%) |
| 70 |
%( arch == "i386" %? |
| 71 |
outline = sprintf("%s[%d] %s (%s, %s, %s, %s)", execname(), tid(), $$name, arg(register("edi")), arg(register("esi")), arg(register("edx")), arg(register("ecx"))) |
| 72 |
%) |
| 73 |
|
| 74 |
report_line[$$name] = outline |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
probe process.plt.return, process.library("*").plt.return |
| 79 |
{ |
| 80 |
if (filter_p()) next; |
| 81 |
if (strlen($$name) == 0) next; |
| 82 |
|
| 83 |
// cache cumulative time spent in each call |
| 84 |
if (count) |
| 85 |
{ |
| 86 |
now = gettimeofday_us() |
| 87 |
then = func_time[$$name] |
| 88 |
func_time_total[$$name] += now-then |
| 89 |
delete func_time[$$name] |
| 90 |
next |
| 91 |
} |
| 92 |
|
| 93 |
report_line_returnval[$$name] = arg(retval) |
| 94 |
foreach (rl in report_line) { |
| 95 |
first_key = rl |
| 96 |
break |
| 97 |
} |
| 98 |
|
| 99 |
// if we have nested calls then wait for the return of the first call |
| 100 |
if ($$name == first_key) |
| 101 |
{ |
| 102 |
foreach (rl in report_line) |
| 103 |
{ |
| 104 |
report (rl) |
| 105 |
printf ("=%s\n", report_line_returnval[rl]) |
| 106 |
} |
| 107 |
delete report_line |
| 108 |
delete report_line_returnval |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
probe end |
| 113 |
{ |
| 114 |
if (count) |
| 115 |
{ |
| 116 |
cumulative_time = 0 |
| 117 |
printf ("%% time seconds calls\n") |
| 118 |
printf ( "------ --------- -------\n") |
| 119 |
|
| 120 |
foreach (ttt in func_time_total) |
| 121 |
{ |
| 122 |
cumulative_time += func_time_total[ttt] |
| 123 |
} |
| 124 |
|
| 125 |
foreach (ttt in func_time_total) |
| 126 |
{ |
| 127 |
percent = (func_time_total[ttt] * 100) / cumulative_time |
| 128 |
if (percent > 0) |
| 129 |
{ |
| 130 |
printf("%5d%% ", percent) |
| 131 |
printf("%14d ", func_time_total[ttt]%1000000) |
| 132 |
printf("%10d ", @count(func_calls[ttt])) |
| 133 |
printf(" %s\n",ttt) |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|