Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The bare files. No documentation yet.

  • Loading branch information...
commit c68867d91529375a7d43b5e52ece20b772d60f7c 1 parent aa358d1
@andreoli authored
View
3  clean.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+rm -rf /tmp/{stack,symbols,wasonstack,uprobes,trace*,p_*}
View
139 finegrain-uprobes.sh
@@ -0,0 +1,139 @@
+#!/bin/bash
+
+find_dsos() {
+ ldd $CMD | grep -Ev 'gate|vdso' | sed -e 's/(\(.*\))//' | \
+ sed -e 's|^\([^/]*\)\(.*\)$|\2|'
+}
+
+find_symbol_abs_address() {
+ echo "$(objdump -t $1 | grep "F .text" | grep -w $2 | awk '{print $1}')"
+}
+
+find_start_addr() {
+ readelf -l $1 | grep -A1 LOAD | grep -B1 "R E" | \
+ grep LOAD | awk '{print $4}'
+}
+
+find_functions_invoked_by_library_function() {
+ echo "disas $2" | gdb -q $1 2>/dev/null | grep -E "callq([^#]+)<" | grep -v "@" | \
+ grep -v -w $2 | awk '{print $5'} | sed -e 's/<\(.*\)>/\1/'
+}
+
+lookup_real_symbol() {
+ objdump -t $1 | grep -E "$2\s+g\s+DF\s+.text" | awk '{print $7}'
+}
+
+symbol_is_weak() {
+ symtype=$(echo $1 | awk '{print $2}')
+ if [[ "$symtype" == "w" ]]; then
+ return 0
+ else
+ return 1
+ fi
+}
+
+print_symbol_info() {
+ objdump -T $1 | grep -w $2
+}
+
+find_dso_owning_symbol() {
+ for d in $1; do
+ if objdump -t $d | grep -qw $2; then
+ echo $d
+ return
+ fi
+ done
+}
+find_dynamic_loader_symbols() {
+ objdump -t $1 | grep -E "g\s+F .text" | awk '{print $6}'
+}
+
+find_used_library_function_symbols() {
+ if [[ $2 == 0 ]]; then
+ objdump -t $1 | grep -E "F \*UND\*" | awk '{print $5}' | cut -f1 -d"@"
+ else
+ objdump -T $1 | grep -E "DF \*UND\*" | awk '{print $6}'
+ fi
+}
+
+find_program_function_symbols() {
+ if [[ $2 == 0 ]]; then
+ objdump -t $1 | grep -E "g\s+F .text" | awk '{print $6}'
+ else
+ objdump -T $1 | grep -E "g\s+DF .text" | awk '{print $7}'
+ fi
+}
+
+hex_sub() {
+ printf "0x%x" $(($1-$2))
+}
+
+CMD=$1
+UPROBES="/tmp/uprobes"
+STACK="/tmp/stack"
+WASONSTACK="/tmp/wasonstack"
+SYMBOLS="/tmp/symbols"
+
+if ! file -L $CMD | grep -q ELF; then
+ echo "$CMD is not an executable."
+ exit 1
+fi
+file -L $CMD | grep -q "not stripped"; has_static_symtable=$?
+mkdir $STACK $WASONSTACK $SYMBOLS
+
+dsos=$(find_dsos)
+symbols=$(find_program_function_symbols $CMD $has_static_symtable)
+for s in $symbols; do
+ touch $SYMBOLS/$s
+done
+
+symbols=$(find_used_library_function_symbols $CMD $has_static_symtable)
+for s in $symbols; do
+ touch $STACK/$s
+done
+
+symbols=$(find_dynamic_loader_symbols $(echo $dsos | grep -o -E "/lib/ld\-(.*)"))
+for s in $symbols; do
+ touch $STACK/$s
+done
+
+while [[ "$(ls -A $STACK 2>/dev/null)" != "" ]]; do
+ sym=$(ls $STACK/* | head -n 1)
+ cp $sym $WASONSTACK
+ mv $sym $SYMBOLS
+ sym=$(basename $sym)
+ dso=$(find_dso_owning_symbol "$dsos" $sym)
+ sym_info=$(print_symbol_info $dso $sym)
+ if symbol_is_weak "$sym_info"; then
+ addr=$(echo $sym_info | awk '{print $1}')
+ strong_sym=$(lookup_real_symbol $dso $addr)
+ if [[ $strong_sym != "" ]]; then
+ sym=$strong_sym
+ fi
+ fi
+ functions_called=$(find_functions_invoked_by_library_function $dso $sym)
+ for fc in $functions_called; do
+ if ! ls $WASONSTACK | grep -q $fc; then
+ touch $STACK/$fc
+ fi
+ done
+done
+
+for s in $(ls $SYMBOLS/*); do
+ sym=$(basename $s)
+ dso=$(find_dso_owning_symbol "$dsos $CMD" $sym)
+ start_addr=$(find_start_addr $dso)
+ abs_addr=$(find_symbol_abs_address $dso $sym)
+ for a in $abs_addr; do
+ rel_addr=$(hex_sub "0x$a" $start_addr)
+ echo "p $dso:$rel_addr" >> $UPROBES
+ if [[ "$dso" == "$CMD" ]]; then
+ printf "0x%x %s\n" "$rel_addr" $sym >> /tmp/p_$(basename $dso)
+ else
+ printf "0x%x %s\n" "$rel_addr" $sym >> /tmp/p_$(basename $dso | sed -ne 's/^\([[:alpha:]]\+\)\(.*\)$/\1/p')
+ fi
+
+ done
+done
+
+exit 0
View
7 full-trace.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+
+./finegrain-uprobes.sh $1
+./trace-process.sh $1
+./rewrite-addresses.sh $1
+#./clean.sh
View
27 rewrite-address-split-trace.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+UPROBES="/tmp/uprobes"
+
+if ! test -r $1; then
+ echo "$1 is not readable."
+ exit 1
+fi
+
+tracefile=$1
+dsos_addresses=$(grep "\/\* p_.* (0x.*) \*\/" $tracefile | awk '{print $4}' | sort | uniq)
+for a in $dsos_addresses; do
+ dso=$(echo $a | cut -f1-2 -d"_")
+ if [[ ! -r /tmp/$dso ]]; then
+ dso_file=$(ls /tmp/$dso*)
+ else
+ dso_file=/tmp/$dso
+ fi
+ dso_decoded_name=$(basename $dso_file)
+ offset=$(echo $a | cut -f3 -d"_" | tr -d ":")
+ symbol=$(grep $offset $dso_file | tail -n 1 | cut -f2 -d" ")
+
+ echo "dso:$dso, offset:$offset, symbol:$symbol"
+ sed -i -e "s/${dso}_${offset}:/${dso_decoded_name:2}:${symbol}/" $tracefile
+done
+
+exit 0
View
24 rewrite-addresses.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+CMD=$1
+UPROBES="/tmp/uprobes"
+TRACEFILE="/tmp/trace"
+TRACEPREFIX="/tmp/trace.split"
+TRACEFILE_DECODED="/tmp/trace.decoded"
+
+nr_cpu=$(lscpu | grep ^CPU\(s\) | awk '{print $2}')
+if [[ "$nr_cpu" -gt 1 ]]; then
+ postfix="s"
+fi
+echo "splitting the trace in $nr_cpu part$postfix"
+split -n $nr_cpu $TRACEFILE $TRACEPREFIX
+
+for f in $(ls $TRACEPREFIX*); do
+ ./rewrite-address-split-trace.sh $f &
+done
+
+wait
+echo "fine"
+
+cat $TRACEPREFIX* > $TRACEFILE_DECODED
+exit 0
View
37 trace-process.sh
@@ -0,0 +1,37 @@
+#!/bin/bash -xv
+
+CMD="$@"
+UPROBES="/tmp/uprobes"
+TRACEFILE="/tmp/trace"
+
+set -- $CMD
+if ! file -L $1 | grep -q ELF; then
+ echo "$1 is not an executable."
+ exit 1
+fi
+set --
+
+echo "tracing $CMD"
+echo function_graph | sudo tee /sys/kernel/debug/tracing/current_tracer
+echo 2000000 | sudo tee /sys/kernel/debug/tracing/buffer_size_kb
+echo 1 | sudo tee /sys/kernel/debug/tracing/tracing_on
+echo | sudo tee /sys/kernel/debug/tracing/uprobe_events
+cat $UPROBES | sudo tee -a /sys/kernel/debug/tracing/uprobe_events
+echo | sudo tee /sys/kernel/debug/tracing/set_ftrace_pid
+echo 1 | sudo tee /sys/kernel/debug/tracing/events/uprobes/enable
+echo 1 | sudo tee /sys/kernel/debug/tracing/tracing_enabled
+
+sudo ./wrapper $CMD
+
+echo 0 | sudo tee /sys/kernel/debug/tracing/tracing_enabled
+echo 0 | sudo tee /sys/kernel/debug/tracing/tracing_on
+
+echo "writing $TRACEFILE"
+cat /sys/kernel/debug/tracing/trace > $TRACEFILE
+
+echo 0 | sudo tee /sys/kernel/debug/tracing/events/uprobes/enable
+echo | sudo tee /sys/kernel/debug/tracing/uprobe_events
+
+echo nop | sudo tee /sys/kernel/debug/tracing/current_tracer
+
+exit 0
Please sign in to comment.
Something went wrong with that request. Please try again.