xref: /linux/tools/perf/tests/shell/test_uprobe_from_different_cu.sh (revision c94cd9508b1335b949fd13ebd269313c65492df0)
1#!/bin/bash
2# test perf probe of function from different CU
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7# Skip if there's no probe command.
8if ! perf | grep probe
9then
10        echo "Skip: probe command isn't present"
11        exit 2
12fi
13
14# skip if there's no gcc
15if ! [ -x "$(command -v gcc)" ]; then
16        echo "failed: no gcc compiler"
17        exit 2
18fi
19
20temp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX)
21
22cleanup()
23{
24	trap - EXIT TERM INT
25	if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then
26		echo "--- Cleaning up ---"
27		perf probe -x ${temp_dir}/testfile -d foo || true
28		rm -f "${temp_dir}/"*
29		rmdir "${temp_dir}"
30	fi
31}
32
33trap_cleanup()
34{
35        cleanup
36        exit 1
37}
38
39trap trap_cleanup EXIT TERM INT
40
41cat > ${temp_dir}/testfile-foo.h << EOF
42struct t
43{
44  int *p;
45  int c;
46};
47
48extern int foo (int i, struct t *t);
49EOF
50
51cat > ${temp_dir}/testfile-foo.c << EOF
52#include "testfile-foo.h"
53
54int
55foo (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}
63EOF
64
65cat > ${temp_dir}/testfile-main.c << EOF
66#include "testfile-foo.h"
67
68static struct t g;
69
70int
71main (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}
81EOF
82
83gcc -g -Og -flto -c ${temp_dir}/testfile-foo.c -o ${temp_dir}/testfile-foo.o
84gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o
85gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o
86
87perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo"
88perf probe -x ${temp_dir}/testfile foo
89
90cleanup
91