156cbeacfSGeorg Müller#!/bin/bash
256cbeacfSGeorg Müller# test perf probe of function from different CU
356cbeacfSGeorg Müller# SPDX-License-Identifier: GPL-2.0
456cbeacfSGeorg Müller
556cbeacfSGeorg Müllerset -e
656cbeacfSGeorg Müller
7*98ce8e4aSGeorg Müller# skip if there's no gcc
8*98ce8e4aSGeorg Müllerif ! [ -x "$(command -v gcc)" ]; then
9*98ce8e4aSGeorg Müller        echo "failed: no gcc compiler"
10*98ce8e4aSGeorg Müller        exit 2
11*98ce8e4aSGeorg Müllerfi
12*98ce8e4aSGeorg Müller
1356cbeacfSGeorg Müllertemp_dir=$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX)
1456cbeacfSGeorg Müller
1556cbeacfSGeorg Müllercleanup()
1656cbeacfSGeorg Müller{
1756cbeacfSGeorg Müller	trap - EXIT TERM INT
1856cbeacfSGeorg Müller	if [[ "${temp_dir}" =~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then
1956cbeacfSGeorg Müller		echo "--- Cleaning up ---"
20*98ce8e4aSGeorg Müller		perf probe -x ${temp_dir}/testfile -d foo || true
2156cbeacfSGeorg Müller		rm -f "${temp_dir}/"*
2256cbeacfSGeorg Müller		rmdir "${temp_dir}"
2356cbeacfSGeorg Müller	fi
2456cbeacfSGeorg Müller}
2556cbeacfSGeorg Müller
2656cbeacfSGeorg Müllertrap_cleanup()
2756cbeacfSGeorg Müller{
2856cbeacfSGeorg Müller        cleanup
2956cbeacfSGeorg Müller        exit 1
3056cbeacfSGeorg Müller}
3156cbeacfSGeorg Müller
3256cbeacfSGeorg Müllertrap trap_cleanup EXIT TERM INT
3356cbeacfSGeorg Müller
3456cbeacfSGeorg Müllercat > ${temp_dir}/testfile-foo.h << EOF
3556cbeacfSGeorg Müllerstruct t
3656cbeacfSGeorg Müller{
3756cbeacfSGeorg Müller  int *p;
3856cbeacfSGeorg Müller  int c;
3956cbeacfSGeorg Müller};
4056cbeacfSGeorg Müller
4156cbeacfSGeorg Müllerextern int foo (int i, struct t *t);
4256cbeacfSGeorg MüllerEOF
4356cbeacfSGeorg Müller
4456cbeacfSGeorg Müllercat > ${temp_dir}/testfile-foo.c << EOF
4556cbeacfSGeorg Müller#include "testfile-foo.h"
4656cbeacfSGeorg Müller
4756cbeacfSGeorg Müllerint
4856cbeacfSGeorg Müllerfoo (int i, struct t *t)
4956cbeacfSGeorg Müller{
5056cbeacfSGeorg Müller  int j, res = 0;
5156cbeacfSGeorg Müller  for (j = 0; j < i && j < t->c; j++)
5256cbeacfSGeorg Müller    res += t->p[j];
5356cbeacfSGeorg Müller
5456cbeacfSGeorg Müller  return res;
5556cbeacfSGeorg Müller}
5656cbeacfSGeorg MüllerEOF
5756cbeacfSGeorg Müller
5856cbeacfSGeorg Müllercat > ${temp_dir}/testfile-main.c << EOF
5956cbeacfSGeorg Müller#include "testfile-foo.h"
6056cbeacfSGeorg Müller
6156cbeacfSGeorg Müllerstatic struct t g;
6256cbeacfSGeorg Müller
6356cbeacfSGeorg Müllerint
6456cbeacfSGeorg Müllermain (int argc, char **argv)
6556cbeacfSGeorg Müller{
6656cbeacfSGeorg Müller  int i;
6756cbeacfSGeorg Müller  int j[argc];
6856cbeacfSGeorg Müller  g.c = argc;
6956cbeacfSGeorg Müller  g.p = j;
7056cbeacfSGeorg Müller  for (i = 0; i < argc; i++)
7156cbeacfSGeorg Müller    j[i] = (int) argv[i][0];
7256cbeacfSGeorg Müller  return foo (3, &g);
7356cbeacfSGeorg Müller}
7456cbeacfSGeorg MüllerEOF
7556cbeacfSGeorg Müller
7656cbeacfSGeorg Müllergcc -g -Og -flto -c ${temp_dir}/testfile-foo.c -o ${temp_dir}/testfile-foo.o
7756cbeacfSGeorg Müllergcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/testfile-main.o
7856cbeacfSGeorg Müllergcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/testfile-main.o
7956cbeacfSGeorg Müller
8056cbeacfSGeorg Müllerperf probe -x ${temp_dir}/testfile --funcs foo
8156cbeacfSGeorg Müllerperf probe -x ${temp_dir}/testfile foo
8256cbeacfSGeorg Müller
8356cbeacfSGeorg Müllercleanup
84