aboutsummaryrefslogtreecommitdiff
path: root/pw_assert_basic/basic_handler.cc
blob: 046b1008740c6b249780f404cac7e4f995677d24 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// This is a very basic direct output log implementation with no buffering.

// #define PW_LOG_MODULE_NAME "ASRT"
// #include "pw_log/log.h"

#include <cstdio>
#include <cstring>

#include "pw_assert/config.h"
#include "pw_assert_basic/handler.h"
#include "pw_preprocessor/util.h"
#include "pw_string/string_builder.h"
#include "pw_sys_io/sys_io.h"

// ANSI color constants to control the terminal. Not Windows compatible.
// clang-format off
#if PW_ASSERT_BASIC_USE_COLORS
#define MAGENTA   "\033[35m"
#define YELLOW    "\033[33m"
#define RED       "\033[31m"
#define GREEN     "\033[32m"
#define BLUE      "\033[96m"
#define BLACK     "\033[30m"
#define YELLOW_BG "\033[43m"
#define WHITE_BG  "\033[47m"
#define RED_BG    "\033[41m"
#define BOLD      "\033[1m"
#define RESET     "\033[0m"
#else
#define MAGENTA   ""
#define YELLOW    ""
#define RED       ""
#define GREEN     ""
#define BLUE      ""
#define BLACK     ""
#define YELLOW_BG ""
#define WHITE_BG  ""
#define RED_BG    ""
#define BOLD      ""
#define RESET     ""
#endif  // PW_ASSERT_BASIC_USE_COLORS
// clang-format on

static const char* kCrashBanner[] = {
    " ",
    "   ▄████▄      ██▀███      ▄▄▄           ██████     ██░ ██    ",
    "  ▒██▀ ▀█     ▓██ ▒ ██▒   ▒████▄       ▒██    ▒    ▓██░ ██▒   ",
    "  ▒▓█ 💥 ▄    ▓██ ░▄█ ▒   ▒██  ▀█▄     ░ ▓██▄      ▒██▀▀██░   ",
    "  ▒▓▓▄ ▄██▒   ▒██▀▀█▄     ░██▄▄▄▄██      ▒   ██▒   ░▓█ ░██    ",
    "  ▒ ▓███▀ ░   ░██▓ ▒██▒    ▓█   ▓██▒   ▒██████▒▒   ░▓█▒░██▓   ",
    "  ░ ░▒ ▒  ░   ░ ▒▓ ░▒▓░    ▒▒   ▓▒█░   ▒ ▒▓▒ ▒ ░    ▒ ░░▒░▒   ",
    "    ░  ▒        ░▒ ░ ▒░     ▒   ▒▒ ░   ░ ░▒  ░ ░    ▒ ░▒░ ░   ",
    "  ░             ░░   ░      ░   ▒      ░  ░  ░      ░  ░░ ░   ",
    "  ░ ░            ░              ░  ░         ░      ░  ░  ░   ",
    "  ░",
    " ",
};

static void WriteLine(const std::string_view& s) {
  pw::sys_io::WriteLine(s)
      .IgnoreError();  // TODO(b/242598609): Handle Status properly
}

typedef pw::StringBuffer<150> Buffer;

extern "C" void pw_assert_basic_HandleFailure(const char* file_name,
                                              int line_number,
                                              const char* function_name,
                                              const char* format,
                                              ...) {
  // As a matter of usability, crashes should be visible; make it so.
#if PW_ASSERT_BASIC_SHOW_BANNER
  WriteLine(RED);
  for (const char* line : kCrashBanner) {
    WriteLine(line);
  }
  WriteLine(RESET);
#endif  // PW_ASSERT_BASIC_SHOW_BANNER

  WriteLine(
      "  Welp, that didn't go as planned. "
      "It seems we crashed. Terribly sorry!");
  WriteLine("");
  WriteLine(YELLOW "  CRASH MESSAGE" RESET);
  WriteLine("");
  {
    Buffer buffer;
    buffer << "     ";
    va_list args;
    va_start(args, format);
    buffer.FormatVaList(format, args);
    va_end(args);
    WriteLine(buffer.view());
  }

  if (file_name != nullptr && line_number != -1) {
    WriteLine("");
    WriteLine(YELLOW "  CRASH FILE & LINE" RESET);
    WriteLine("");
    {
      Buffer buffer;
      buffer.Format("     %s:%d", file_name, line_number);
      WriteLine(buffer.view());
    }
  }
  if (function_name != nullptr) {
    WriteLine("");
    WriteLine(YELLOW "  CRASH FUNCTION" RESET);
    WriteLine("");
    {
      Buffer buffer;
      buffer.Format("     %s", function_name);
      WriteLine(buffer.view());
    }
  }

  // TODO(pwbug/95): Perhaps surprisingly, this doesn't actually crash the
  // device. At some point we'll have a reboot BSP function or similar, but for
  // now this is acceptable since no one is using this basic backend.
  if (!PW_ASSERT_BASIC_DISABLE_NORETURN) {
#if (PW_ASSERT_BASIC_ACTION == PW_ASSERT_BASIC_ACTION_ABORT)
    // abort() doesn't flush stderr/stdout, so manually flush them before
    // aborting. abort() is preferred to exit(1) because debuggers catch it.
    std::fflush(stderr);
    std::fflush(stdout);
    std::abort();
#elif (PW_ASSERT_BASIC_ACTION == PW_ASSERT_BASIC_ACTION_EXIT)
    // Use _Exit to not run destructors or atexit hooks in case they cause
    // further crashes.
    std::_Exit(-1);
#elif (PW_ASSERT_BASIC_ACTION == PW_ASSERT_BASIC_ACTION_LOOP)
    WriteLine("");
    WriteLine(MAGENTA "  HANG TIME" RESET);
    WriteLine("");
    WriteLine(
        "     ... until a debugger joins. System is waiting in a while(1)");
    while (true) {
    }
#else
#error PW_ASSERT_BASIC_ACTION Must be set to valid option.
#endif
    PW_UNREACHABLE;
  } else {
    WriteLine("");
    WriteLine(MAGENTA "  NOTE: YOU ARE IN ASSERT BASIC TEST MODE" RESET);
    WriteLine("");
    WriteLine("     This build returns from the crash handler for testing.");
    WriteLine("     If you see this message in production, your build is ");
    WriteLine("     incorrectly configured. Search for");
    WriteLine("     PW_ASSERT_BASIC_DISABLE_NORETURN to fix it.");
    WriteLine("");
  }
}