91 lines · bash
1#!/bin/bash2# test perf probe of function from different CU3# SPDX-License-Identifier: GPL-2.04 5set -e6 7# Skip if there's no probe command.8if ! perf | grep probe9then10 echo "Skip: probe command isn't present"11 exit 212fi13 14# skip if there's no gcc15if ! [ -x "$(command -v gcc)" ]; then16 echo "failed: no gcc compiler"17 exit 218fi19 20temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX)21 22cleanup()23{24 trap - EXIT TERM INT25 if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then26 echo "--- Cleaning up ---"27 perf probe -x ${temp_dir}/testfile -d foo || true28 rm -f "${temp_dir}/"*29 rmdir "${temp_dir}"30 fi31}32 33trap_cleanup()34{35 cleanup36 exit 137}38 39trap trap_cleanup EXIT TERM INT40 41cat > ${temp_dir}/testfile-foo.h << EOF42struct t43{44 int *p;45 int c;46};47 48extern int foo (int i, struct t *t);49EOF50 51cat > ${temp_dir}/testfile-foo.c << EOF52#include "testfile-foo.h"53 54int55foo (int i, struct t *t)56{57 int j, res = 0;58 for (j = 0; j < i && j < t->c; j++)59 res += t->p[j];60 61 return res;62}63EOF64 65cat > ${temp_dir}/testfile-main.c << EOF66#include "testfile-foo.h"67 68static struct t g;69 70int71main (int argc, char **argv)72{73 int i;74 int j[argc];75 g.c = argc;76 g.p = j;77 for (i = 0; i < argc; i++)78 j[i] = (int) argv[i][0];79 return foo (3, &g);80}81EOF82 83gcc -g -Og -flto -c ${temp_dir}/testfile-foo.c -o ${temp_dir}/testfile-foo.o84gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o85gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o86 87perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo"88perf probe -x ${temp_dir}/testfile foo89 90cleanup91