summaryrefslogtreecommitdiff
path: root/src/util/u_debug_stack_test.cpp
blob: b5f8ea4ba386e8551edf68a8ee55cef4467df048 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright © 2020 Google, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdio.h>
#include <gtest/gtest.h>

#include "util/u_debug_stack.h"

static void __attribute__((noinline))
func_a(void)
{
   struct debug_stack_frame backtrace[16];

   fprintf(stderr, "--- backtrace from func_a:\n");
   debug_backtrace_capture(backtrace, 0, 16);
   debug_backtrace_dump(backtrace, 16);
}

static void __attribute__((noinline))
func_b(void)
{
   struct debug_stack_frame backtrace[16];

   func_a();

   fprintf(stderr, "--- backtrace from func_b:\n");
   debug_backtrace_capture(backtrace, 0, 16);
   debug_backtrace_dump(backtrace, 16);
}

static void __attribute__((noinline))
func_c(struct debug_stack_frame *frames)
{
   debug_backtrace_capture(frames, 0, 16);
}

TEST(u_debug_stack_test, basics)
{
   struct debug_stack_frame backtrace[16];
   struct debug_stack_frame stored_backtrace[16];

   func_c(stored_backtrace);

   fprintf(stderr, "--- backtrace from main to stderr:\n");
   debug_backtrace_capture(backtrace, 0, 16);
   debug_backtrace_print(stderr, backtrace, 16);

   fprintf(stderr, "--- backtrace from main again to debug_printf:\n");
   debug_backtrace_capture(backtrace, 0, 16);
   debug_backtrace_dump(backtrace, 16);

   func_a();

   func_b();

   fprintf(stderr, "--- stored backtrace from start of main:\n");
   debug_backtrace_dump(stored_backtrace, 16);
}

#if _POSIX_C_SOURCE >= 200809L

TEST(u_debug_stack_test, capture_not_overwritten)
{
   struct debug_stack_frame backtrace1[16], backtrace2[16];

   FILE *fp;
   char *bt1, *bt2;
   size_t size;

   /* Old android implementation uses one global capture per thread. Test that
    * we can store multiple captures and that they decode to different
    * backtraces.
    */

   func_c(backtrace1);
   debug_backtrace_capture(backtrace2, 0, 16);

   fp = open_memstream(&bt1, &size);
   debug_backtrace_print(fp, backtrace1, 16);
   fclose(fp);

   fp = open_memstream(&bt2, &size);
   debug_backtrace_print(fp, backtrace2, 16);
   fclose(fp);

   if (size > 0) {
      EXPECT_STRNE(bt1, bt2);
   }
}

#endif