1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15
16 #include "aemu/base/c_header.h"
17
18 #include <stdarg.h>
19
20 #ifdef __cplusplus
21 #include "aemu/base/StringFormat.h"
22
23 #include <utility>
24 #include <stdio.h>
25 #else
26 #include <stdbool.h>
27 #endif
28
29
30 #if defined(__cplusplus) && ! defined(__clang__)
31 #define ANDROID_NORETURN [[noreturn]]
32 #else // !__cplusplus || __clang__
33 #ifdef _MSC_VER
34 #define ANDROID_NORETURN __declspec(noreturn)
35 #else // !_MSC_VER
36 #define ANDROID_NORETURN __attribute__((noreturn))
37 #endif // !_MSC_VER
38 #endif // !__cplusplus || __clang__
39
40 ANDROID_BEGIN_HEADER
41
42 // Call this to enable appending of messages.
43 void crashhandler_enable_message_store();
44
45 // Append message to crash dump file without aborting.
46 // Useful for saving debugging information just before a crash.
47 //
48 // Note, this message will be added to a crash attrbute, it is not
49 // guaranteed to make it to the final report as we only have
50 // limited amount of storage space available.
51 void crashhandler_append_message(const char* message);
52
53 // A variadic overload with C interface
54 void crashhandler_append_message_format(const char* format, ...);
55 void crashhandler_append_message_format_v(const char* format, va_list args);
56
57 // Append a string to the crash report. One is free to free both of the
58 // parameters after the call - function doesn't take ownership of them.
59 //
60 // The strings are stored in the minidump as 'name' : 'string'
61 void crashhandler_add_string(const char* name, const char* string);
62
63 // Reads the contents of file |source| and attaching it as a string annotation
64 // under |source|. This is the same as reading the file source and calling
65 // crashhandler_add_string(destination, contents_of_file_source)
66 bool crashhandler_copy_attachment(const char* destination, const char* source);
67
68
69
70 // Track crashes on exit.
71 void crashhandler_exitmode(const char* message);
72
73 // Abort the program execution immediately, recording the message
74 // with the crashreport if possible.
75 ANDROID_NORETURN void crashhandler_die(const char* message)
76 __attribute__((noinline));
77
78 // A variadic overload with C interface
79 ANDROID_NORETURN void crashhandler_die_format(const char* format, ...);
80 ANDROID_NORETURN void crashhandler_die_format_v(const char* format, va_list args);
81
82 void pause_hangdetector();
83 void resume_hangdetector();
84 void detect_hanging_looper(void*);
85 ANDROID_END_HEADER
86
87
88 #ifdef __cplusplus
89 // A variadic overload for a convenient message formatting
90 template <class... Args>
crashhandler_die(const char * format,Args &&...args)91 ANDROID_NORETURN void crashhandler_die(const char* format, Args&&... args) {
92 char buffer[2048] = {}; // 2048 is enough for everyone ;)
93 snprintf(buffer, sizeof(buffer) - 1, format,
94 android::base::unpackFormatArg(std::forward<Args>(args))...);
95 crashhandler_die(buffer);
96 }
97 #endif
98