BRL-CAD
exit.h
Go to the documentation of this file.
1 /* E X I T . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #ifndef BU_EXIT_H
22 #define BU_EXIT_H
23 
24 #include "common.h"
25 
26 #include <stdio.h> /* for FILE */
27 
28 #include "bu/defines.h"
29 #include "bu/hook.h"
30 
31 
32 __BEGIN_DECLS
33 
34 /** @{ */
35 /** @file bu/exit.h */
36 
37 /**
38  * this routine provides a trace of the call stack to the caller,
39  * generally called either directly, via a signal handler, or through
40  * bu_bomb() with the appropriate bu_debug flags set.
41  *
42  * the routine waits indefinitely (in a spin loop) until a signal
43  * (SIGINT) is received, at which point execution continues, or until
44  * some other signal is received that terminates the application.
45  *
46  * the stack backtrace will be written to the provided 'fp' file
47  * pointer. it's the caller's responsibility to open and close
48  * that pointer if necessary. If 'fp' is NULL, stdout will be used.
49  *
50  * returns truthfully if a backtrace was attempted.
51  */
52 BU_EXPORT extern int bu_backtrace(FILE *fp);
53 
54 /**
55  * A version of bu_backtrace where the caller provides their own
56  * full path to their executable for passing to GDB, rather than
57  * having libbu attempt to determine that path.
58  *
59  * Passing NULL to argv0 makes the behavior identical to
60  * that of bu_backtrace.
61  */
62 BU_EXPORT extern int bu_backtrace_app(FILE *fp, const char *argv0);
63 
64 /**
65  * Adds a hook to the list of bu_bomb hooks. The top (newest) one of these
66  * will be called with its associated client data and a string to be
67  * processed. Typically, these hook functions will display the output
68  * (possibly in an X window) or record it.
69  *
70  * NOTE: The hook functions are all non-PARALLEL.
71  */
72 BU_EXPORT extern void bu_bomb_add_hook(bu_hook_t func, void *clientdata);
73 
74 /* TODO - missing functions, if we're not going to expose the bu_bomb hook
75  * list.. is this the API we want? */
76 BU_EXPORT extern void bu_bomb_save_all_hooks(struct bu_hook_list *save_hlp);
77 BU_EXPORT extern void bu_bomb_delete_all_hooks(void);
78 BU_EXPORT extern void bu_bomb_restore_hooks(struct bu_hook_list *save_hlp);
79 
80 /**
81  * Abort the running process.
82  *
83  * The bu_bomb routine is called on a fatal error, generally where no
84  * recovery is possible. Error handlers may, however, be registered
85  * with BU_SETJUMP(). This routine intentionally limits calls to
86  * other functions and intentionally uses no stack variables. Just in
87  * case the application is out of memory, bu_bomb deallocates a small
88  * buffer of memory.
89  *
90  * Before termination, it optionally performs the following operations
91  * in the order listed:
92  *
93  * 1. Outputs str to standard error
94  *
95  * 2. Calls any callback functions set in the global bu_bomb_hook_list
96  * variable with str passed as an argument.
97  *
98  * 3. Jumps to any user specified error handler registered with the
99  * BU_SETJUMP() facility.
100  *
101  * 4. Outputs str to the terminal device in case standard error is
102  * redirected.
103  *
104  * 5. Aborts abnormally (via abort) if BU_DEBUG_COREDUMP is defined.
105  *
106  * 6. Exits with exit(12).
107  *
108  * Only produce a core-dump when that debugging bit is set. Note that
109  * this function is meant to be a last resort semi-graceful abort.
110  *
111  * This routine should never return unless there is a BU_SETJUMP()
112  * handler registered.
113  */
114 BU_EXPORT NORETURN extern void bu_bomb(const char *str);
115 
116 
117 /**
118  * Semi-graceful termination of the application that doesn't cause a
119  * stack trace, exiting with the specified status after printing the
120  * given message. It's okay for this routine to use the stack,
121  * contrary to bu_bomb's behavior since it should be called for
122  * expected termination situations.
123  *
124  * This routine should generally not be called within a library. Use
125  * bu_bomb or (better) cascade the error back up to the application.
126  *
127  * This routine should never return.
128  */
129 BU_EXPORT NORETURN _BU_ATTR_PRINTF23 extern void bu_exit(int status, const char *fmt, ...);
130 
131 
132 /**
133  * @brief
134  * Generate a crash report file, including a call stack backtrace and
135  * other system details.
136  */
137 
138 /**
139  * this routine writes out details of the currently running process to
140  * the specified file, including an informational header about the
141  * execution environment, stack trace details, kernel and hardware
142  * information, and current version information.
143  *
144  * returns truthfully if the crash report was written.
145  *
146  * due to various reasons, this routine is NOT thread-safe.
147  */
148 BU_EXPORT extern int bu_crashreport(const char *filename);
149 
150 /**
151  * A version of bu_crashreport where the caller provides their own
152  * full path to their executable for passing to GDB, rather than
153  * having libbu attempt to determine that path.
154  *
155  * Passing NULL to argv0 makes the behavior identical to
156  * that of bu_crashreport.
157  */
158 BU_EXPORT extern int bu_crashreport_app(const char *filename, const char *argv0);
159 
160 
161 /** @} */
162 
163 __END_DECLS
164 
165 #endif /* BU_EXIT_H */
166 
167 /*
168  * Local Variables:
169  * mode: C
170  * tab-width: 8
171  * indent-tabs-mode: t
172  * c-file-style: "stroustrup"
173  * End:
174  * ex: shiftwidth=4 tabstop=8
175  */
Header file for the BRL-CAD common definitions.
void bu_bomb_restore_hooks(struct bu_hook_list *save_hlp)
int bu_backtrace_app(FILE *fp, const char *argv0)
void bu_bomb_delete_all_hooks(void)
int bu_backtrace(FILE *fp)
void bu_bomb_add_hook(bu_hook_t func, void *clientdata)
int bu_crashreport_app(const char *filename, const char *argv0)
int bu_crashreport(const char *filename)
Generate a crash report file, including a call stack backtrace and other system details.
void bu_bomb_save_all_hooks(struct bu_hook_list *save_hlp)
NORETURN void bu_bomb(const char *str)
NORETURN _BU_ATTR_PRINTF23 void bu_exit(int status, const char *fmt,...)
#define _BU_ATTR_PRINTF23
Definition: defines.h:95
int(* bu_hook_t)(void *, void *)
Definition: hook.h:41
#define NORETURN
Definition: common.h:428