#!/bin/bash usage() { cat < [] This will try to find a matching symbol in the executable and add a probe at its entry point with the given arguments. OPTIONS -C Clear uprobe_events, then exit -r Probe on return rather than function entry -n dry-run, only show what would be done, rather than doing it. Implies -v -v verbose, show commands -D demangle symbol names before checking the regexp SEE ALSO https://www.kernel.org/doc/Documentation/trace/uprobetracer.txt EOF exit 1 } fail() { echo "$@" exit 1 } log() { VERBOSITY=$1 shift (( $VERBOSITY <= $VERBOSE )) && echo "$@" } CLEAR=0 DEMANGLER=cat PROBETYPE=p: DRYRUN=0 VERBOSE=0 [[ $# == 0 ]] && usage while getopts "CDrnvh" flag do case $flag in C ) CLEAR=1;; D ) DEMANGLER=c++filt;; r ) PROBETYPE=r:;; n ) DRYRUN=1;; v ) VERBOSE=$((VERBOSE + 1));; * ) usage;; esac done shift $(($OPTIND - 1 )) if (( $CLEAR )) then echo "" | sudo tee /sys/kernel/debug/tracing/uprobe_events exit 0 fi (( $# < 2 )) && usage DSO=$1 REGEXP=$2 shift 2 ADDITIONAL_ARGUMENTS="$@" #echo $DEMANGLER #echo $PROBETYPE #echo $DRYRUN #echo $VERBOSE #echo $DSO #echo $REGEXP #echo $ADDITIONAL_ARGUMENTS [[ -f "$DSO" ]] || fail "Not found: '$DSO'" # Build string to pipe into uprobe_events TODO=$( ( eu-readelf -S "$DSO" | sed 's/^\[ */[/'; eu-readelf -s "$DSO" | $DEMANGLER ) | grep -- "^\[\|$REGEXP" | grep -v UNDEF | awk -vADDITIONAL_ARGUMENTS="$ADDITIONAL_ARGUMENTS" \ -vPROBETYPE=$PROBETYPE \ -vDSO="$DSO" \ -vPROBENAME=$(echo "$REGEXP" | tr -cd "0-9a-zA-Z") \ '/^\[/ { # First eu-readelf checks segments. We need the offset that # maps adresses to file offsets seg=$1 fileoffset[seg]=strtonum("0x"$4)-strtonum("0x"$5) #print seg, fileoffset[seg] } $4 == "FUNC" { addr = $2 seg = $7 $1 = ""; $2 = ""; $3 = ""; $4 = ""; $5 = ""; $6 = ""; $7 = "" symbol[addr]=$0 segment[addr]=seg } END { for ( addr in symbol ) { #print segment[addr], fileoffset["["segment[addr]"]"], addr, symbol[addr] printf "%s%s %s:0x%x %s\n", PROBETYPE, PROBENAME"_"(++num), DSO, strtonum("0x"addr)-fileoffset["["segment[addr]"]"], ADDITIONAL_ARGUMENTS } }' ) log 1 "Would do this: echo $TODO | sudo tee -a /sys/kernel/debug/tracing/uprobe_events" (( $DRYRUN )) || echo "$TODO" | sudo tee -a /sys/kernel/debug/tracing/uprobe_events log 1 " You now have this in uprobe_events: $( sudo cat /sys/kernel/debug/tracing/uprobe_events )"