blob: 6744385baec8bfa19dbccc5c874b2ce4d417b1c5 [file] [log] [blame]
Florian Mayerca4749a2024-02-14 12:55:46 -08001/*
2 * Copyright (C) 2024 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#pragma once
30
31/**
32 * @file android/crash_detail.h
33 * @brief Attach extra information to android crashes.
34 */
35
36#include <stddef.h>
37#include <stdint.h>
38#include <string.h>
39#include <sys/cdefs.h>
40
41__BEGIN_DECLS
42
43typedef struct crash_detail_t crash_detail_t;
44
45/**
46 * Register a new buffer to get logged into tombstones for crashes.
47 *
48 * It will be added to both the tombstone proto in the crash_detail field, and
49 * in the tombstone text format.
50 *
51 * Tombstone proto definition:
52 * https://cs.android.com/android/platform/superproject/main/+/main:system/core/debuggerd/proto/tombstone.proto
53 *
Florian Mayer63df50e2024-02-23 00:23:44 +000054 * An app can get hold of these for any `REASON_CRASH_NATIVE` instance of
55 * `android.app.ApplicationExitInfo`.
56 *
57 * https://developer.android.com/reference/android/app/ApplicationExitInfo#getTraceInputStream()
58
Florian Mayerca4749a2024-02-14 12:55:46 -080059 * The lifetime of name and data has to be valid until the program crashes, or until
60 * android_crash_detail_unregister is called.
61 *
62 * Example usage:
63 * const char* stageName = "garbage_collection";
64 * crash_detail_t* cd = android_crash_detail_register("stage", stageName, strlen(stageName));
65 * do_garbage_collection();
66 * android_crash_detail_unregister(cd);
67 *
68 * If this example crashes in do_garbage_collection, a line will show up in the textual representation of the tombstone:
69 * Extra crash detail: stage: 'garbage_collection'
70 *
71 * Introduced in API 35.
72 *
73 * \param name identifying name for this extra data.
74 * this should generally be a human-readable debug string, but we are treating
75 * it as arbitrary bytes because it could be corrupted by the crash.
76 * \param name_size number of bytes of the buffer pointed to by name
77 * \param data a buffer containing the extra detail bytes, if null the crash detail
78 * is disabled until android_crash_detail_replace_data replaces it with
79 * a non-null pointer.
80 * \param data_size number of bytes of the buffer pointed to by data
81 *
82 * \return a handle to the extra crash detail.
83 */
84crash_detail_t* _Nullable android_crash_detail_register(
85 const void* _Nonnull name, size_t name_size, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
86
87/**
88 * Unregister crash detail from being logged into tombstones.
89 *
90 * After this function returns, the lifetime of the objects crash_detail was
91 * constructed from no longer needs to be valid.
92 *
93 * Introduced in API 35.
94 *
95 * \param crash_detail the crash_detail that should be removed.
96 */
97void android_crash_detail_unregister(crash_detail_t* _Nonnull crash_detail) __INTRODUCED_IN(35);
98
99/**
100 * Replace data of crash detail.
101 *
102 * This is more efficient than using android_crash_detail_unregister followed by
103 * android_crash_detail_register. If you very frequently need to swap out the data,
104 * you can hold onto the crash_detail.
105 *
106 * Introduced in API 35.
107 *
108 * \param data the new buffer containing the extra detail bytes, or null to disable until
109 * android_crash_detail_replace_data is called again with non-null data.
110 * \param data_size the number of bytes of the buffer pointed to by data.
111 */
112void android_crash_detail_replace_data(crash_detail_t* _Nonnull crash_detail, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
113
114/**
115 * Replace name of crash detail.
116 *
117 * This is more efficient than using android_crash_detail_unregister followed by
118 * android_crash_detail_register. If you very frequently need to swap out the name,
119 * you can hold onto the crash_detail.
120 *
121 * Introduced in API 35.
122 *
123 * \param name identifying name for this extra data.
124 * \param name_size number of bytes of the buffer pointed to by name
125 */
126void android_crash_detail_replace_name(crash_detail_t* _Nonnull crash_detail, const void* _Nonnull name, size_t name_size) __INTRODUCED_IN(35);
127
128__END_DECLS