blob: 490ec8c5f481c466050739a18d057c3ad4913b65 [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
Florian Mayerca4749a2024-02-14 12:55:46 -080036#include <sys/cdefs.h>
37
Elliott Hughes414dd2d2024-10-16 14:48:30 +000038#include <stddef.h>
39
Florian Mayerca4749a2024-02-14 12:55:46 -080040__BEGIN_DECLS
41
42typedef struct crash_detail_t crash_detail_t;
43
44/**
45 * Register a new buffer to get logged into tombstones for crashes.
46 *
47 * It will be added to both the tombstone proto in the crash_detail field, and
48 * in the tombstone text format.
49 *
50 * Tombstone proto definition:
51 * https://cs.android.com/android/platform/superproject/main/+/main:system/core/debuggerd/proto/tombstone.proto
52 *
Florian Mayer63df50e2024-02-23 00:23:44 +000053 * An app can get hold of these for any `REASON_CRASH_NATIVE` instance of
54 * `android.app.ApplicationExitInfo`.
55 *
56 * https://developer.android.com/reference/android/app/ApplicationExitInfo#getTraceInputStream()
57
Florian Mayerca4749a2024-02-14 12:55:46 -080058 * The lifetime of name and data has to be valid until the program crashes, or until
59 * android_crash_detail_unregister is called.
60 *
61 * Example usage:
62 * const char* stageName = "garbage_collection";
63 * crash_detail_t* cd = android_crash_detail_register("stage", stageName, strlen(stageName));
64 * do_garbage_collection();
65 * android_crash_detail_unregister(cd);
66 *
67 * If this example crashes in do_garbage_collection, a line will show up in the textual representation of the tombstone:
68 * Extra crash detail: stage: 'garbage_collection'
69 *
70 * Introduced in API 35.
71 *
72 * \param name identifying name for this extra data.
Florian Mayera5d67782024-03-21 13:48:43 -070073 * this should generally be a human-readable UTF-8 string, but we are treating
Florian Mayerca4749a2024-02-14 12:55:46 -080074 * it as arbitrary bytes because it could be corrupted by the crash.
75 * \param name_size number of bytes of the buffer pointed to by name
76 * \param data a buffer containing the extra detail bytes, if null the crash detail
77 * is disabled until android_crash_detail_replace_data replaces it with
78 * a non-null pointer.
79 * \param data_size number of bytes of the buffer pointed to by data
80 *
81 * \return a handle to the extra crash detail.
82 */
83crash_detail_t* _Nullable android_crash_detail_register(
84 const void* _Nonnull name, size_t name_size, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
85
86/**
87 * Unregister crash detail from being logged into tombstones.
88 *
89 * After this function returns, the lifetime of the objects crash_detail was
90 * constructed from no longer needs to be valid.
91 *
92 * Introduced in API 35.
93 *
94 * \param crash_detail the crash_detail that should be removed.
95 */
96void android_crash_detail_unregister(crash_detail_t* _Nonnull crash_detail) __INTRODUCED_IN(35);
97
98/**
99 * Replace data of crash detail.
100 *
101 * This is more efficient than using android_crash_detail_unregister followed by
102 * android_crash_detail_register. If you very frequently need to swap out the data,
103 * you can hold onto the crash_detail.
104 *
105 * Introduced in API 35.
106 *
107 * \param data the new buffer containing the extra detail bytes, or null to disable until
108 * android_crash_detail_replace_data is called again with non-null data.
109 * \param data_size the number of bytes of the buffer pointed to by data.
110 */
111void android_crash_detail_replace_data(crash_detail_t* _Nonnull crash_detail, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
112
113/**
114 * Replace name of crash detail.
115 *
116 * This is more efficient than using android_crash_detail_unregister followed by
117 * android_crash_detail_register. If you very frequently need to swap out the name,
118 * you can hold onto the crash_detail.
119 *
120 * Introduced in API 35.
121 *
122 * \param name identifying name for this extra data.
123 * \param name_size number of bytes of the buffer pointed to by name
124 */
125void android_crash_detail_replace_name(crash_detail_t* _Nonnull crash_detail, const void* _Nonnull name, size_t name_size) __INTRODUCED_IN(35);
126
127__END_DECLS