about summary refs log tree commit diff
path: root/gmon/tst-mcount-overflow.c
blob: 06cc93ef872eb7c1ef21ab62a0dc72c7b2a6ab87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Test program to trigger mcount overflow in profiling collection.
   Copyright (C) 2017-2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* Program with sufficiently complex, yet pointless, call graph
   that it will trigger an mcount overflow, when you set the
   minarcs/maxarcs tunables to very low values. */

#define PREVENT_TAIL_CALL asm volatile ("")

/* Calls REP(n) macro 16 times, for n=0..15.
 * You need to define REP(n) before using this.
 */
#define REPS \
  REP(0) REP(1) REP(2) REP(3) REP(4) REP(5) REP(6) REP(7) \
  REP(8) REP(9) REP(10) REP(11) REP(12) REP(13) REP(14) REP(15)

/* Defines 16 leaf functions named f1_0 to f1_15 */
#define REP(n) \
  __attribute__ ((noinline, noclone, weak)) void f1_##n (void) {};
REPS
#undef REP

/* Calls all 16 leaf functions f1_* in succession */
__attribute__ ((noinline, noclone, weak)) void
f2 (void)
{
# define REP(n) f1_##n();
  REPS
# undef REP
  PREVENT_TAIL_CALL;
}

/* Defines 16 functions named f2_0 to f2_15, which all just call f2 */
#define REP(n) \
  __attribute__ ((noinline, noclone, weak)) void \
  f2_##n (void) { f2(); PREVENT_TAIL_CALL; };
REPS
#undef REP

__attribute__ ((noinline, noclone, weak)) void
f3 (int count)
{
  for (int i = 0; i < count; ++i)
    {
      /* Calls f1_0(), f2_0(), f1_1(), f2_1(), f3_0(), etc */
#     define REP(n) f1_##n(); f2_##n();
      REPS
#     undef REP
    }
}

int
main (void)
{
  f3 (1000);
  return 0;
}