malloc: Use __get_nprocs on arena_get2 (BZ 30945)
[glibc.git] / sysdeps / sparc / backtrace.c
blobb5b29e5a75d481724283a7d9268b1d1a4f691f64
1 /* Return backtrace of current program state.
2 Copyright (C) 2013-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
19 #include <execinfo.h>
20 #include <stddef.h>
21 #include <sysdep.h>
22 #include <sys/trap.h>
23 #include <backtrace.h>
24 #include <unwind-link.h>
26 struct layout
28 unsigned long locals[8];
29 unsigned long ins[6];
30 unsigned long next;
31 void *return_address;
34 struct trace_arg
36 void **array;
37 struct unwind_link *unwind_link;
38 _Unwind_Word cfa;
39 int cnt;
40 int size;
43 static _Unwind_Reason_Code
44 backtrace_helper (struct _Unwind_Context *ctx, void *a)
46 struct trace_arg *arg = a;
47 _Unwind_Ptr ip;
49 /* We are first called with address in the __backtrace function.
50 Skip it. */
51 if (arg->cnt != -1)
53 ip = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetIP) (ctx);
54 arg->array[arg->cnt] = (void *) ip;
56 /* Check whether we make any progress. */
57 _Unwind_Word cfa
58 = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetCFA) (ctx);
60 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
61 && cfa == arg->cfa)
62 return _URC_END_OF_STACK;
63 arg->cfa = cfa;
65 if (++arg->cnt == arg->size)
66 return _URC_END_OF_STACK;
67 return _URC_NO_REASON;
70 int
71 __backtrace (void **array, int size)
73 int count;
74 struct trace_arg arg =
76 .array = array,
77 .unwind_link = __libc_unwind_link_get (),
78 .size = size,
79 .cnt = -1,
82 if (size <= 0)
83 return 0;
85 if (arg.unwind_link == NULL)
87 struct layout *current;
88 unsigned long fp, i7;
90 asm volatile ("mov %%fp, %0" : "=r"(fp));
91 asm volatile ("mov %%i7, %0" : "=r"(i7));
92 current = (struct layout *) (fp + BACKTRACE_STACK_BIAS);
94 array[0] = (void *) i7;
96 if (size == 1)
97 return 1;
99 backtrace_flush_register_windows();
100 for (count = 1; count < size; count++)
102 array[count] = current->return_address;
103 if (!current->next)
104 break;
105 current = (struct layout *) (current->next + BACKTRACE_STACK_BIAS);
108 else
110 UNWIND_LINK_PTR (arg.unwind_link, _Unwind_Backtrace)
111 (backtrace_helper, &arg);
113 /* _Unwind_Backtrace seems to put NULL address above
114 _start. Fix it up here. */
115 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
116 --arg.cnt;
117 count = arg.cnt != -1 ? arg.cnt : 0;
119 return count;
121 weak_alias (__backtrace, backtrace)
122 libc_hidden_def (__backtrace)