[filesystem.c] Add a cast to aviod a warning with VisualStudio 2017.
[metze/wireshark/wip.git] / wsutil / crash_info.c
1 /* crash_info.c
2  * Routines to try to provide more useful information in crash dumps.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2006 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #include "crash_info.h"
28
29 #ifdef __APPLE__
30 /*
31  * Copyright 2005-2012 Apple Inc. All rights reserved.
32  *
33  * IMPORTANT:  This Apple software is supplied to you by Apple Computer,
34  * Inc. ("Apple") in consideration of your agreement to the following
35  * terms, and your use, installation, modification or redistribution of
36  * this Apple software constitutes acceptance of these terms.  If you do
37  * not agree with these terms, please do not use, install, modify or
38  * redistribute this Apple software.
39  *
40  * In consideration of your agreement to abide by the following terms, and
41  * subject to these terms, Apple grants you a personal, non-exclusive
42  * license, under Apple's copyrights in this original Apple software (the
43  * "Apple Software"), to use, reproduce, modify and redistribute the Apple
44  * Software, with or without modifications, in source and/or binary forms;
45  * provided that if you redistribute the Apple Software in its entirety and
46  * without modifications, you must retain this notice and the following
47  * text and disclaimers in all such redistributions of the Apple Software.
48  * Neither the name, trademarks, service marks or logos of Apple Computer,
49  * Inc. may be used to endorse or promote products derived from the Apple
50  * Software without specific prior written permission from Apple.  Except
51  * as expressly stated in this notice, no other rights or licenses, express
52  * or implied, are granted by Apple herein, including but not limited to
53  * any patent rights that may be infringed by your derivative works or by
54  * other works in which the Apple Software may be incorporated.
55  *
56  * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
57  * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
58  * THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
59  * FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
60  * OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
61  *
62  * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
63  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65  * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
66  * MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
67  * AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
68  * STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
69  * POSSIBILITY OF SUCH DAMAGE.
70  */
71
72 #include <stdint.h>
73 #include <stdlib.h>
74 #include <stdio.h>
75 #include <stdarg.h>
76
77 /*
78  * This used to be the way to add an application-specific string to
79  * crash dumps; see
80  *
81  *      http://www.allocinit.net/blog/2008/01/04/application-specific-information-in-leopard-crash-reports/
82  *
83  * It still appears to work as of OS X 10.8 (Mountain Lion).
84  */
85 __private_extern__ char *__crashreporter_info__ = NULL;
86
87 #if 0
88 /*
89  * And this appears to be the new way to do it, as of Lion.
90  * However, if we do both, we get the message twice, so we're
91  * not doing this one, for now.
92  *
93  * This code was lifted from SVN trunk CUPS.
94  */
95 #define _crc_make_getter(attr, type) (type)(gCRAnnotations.attr)
96 #define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg))
97 #define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden")))
98 #define CRASHREPORTER_ANNOTATIONS_VERSION 4
99 #define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
100
101 /*
102  * Yes, these are all 64-bit, even on 32-bit platforms.
103  *
104  * version is presumably the version of this structure.
105  *
106  * message and message2 are reported, one after the other,
107  * under "Application Specific Information".
108  *
109  * signature_string is reported under "Application Specific
110  * Signatures".
111  *
112  * backtrace is reported under "Application Specific Backtrace".
113  *
114  * Dunno which versions are supported by which versions of macOS.
115  */
116 struct crashreporter_annotations_t {
117         uint64_t version;               /* unsigned long */
118         uint64_t message;               /* char * */
119         uint64_t signature_string;      /* char * */
120         uint64_t backtrace;             /* char * */
121         uint64_t message2;              /* char * */
122         uint64_t thread;                /* uint64_t */
123         uint64_t dialog_mode;           /* unsigned int */
124 };
125
126 CRASH_REPORTER_CLIENT_HIDDEN
127 struct crashreporter_annotations_t gCRAnnotations
128         __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
129         CRASHREPORTER_ANNOTATIONS_VERSION,      /* version */
130         0,                                      /* message */
131         0,                                      /* signature_string */
132         0,                                      /* backtrace */
133         0,                                      /* message2 */
134         0,                                      /* thread */
135         0                                       /* dialog_mode */
136 };
137
138 #define CRSetCrashLogMessage(m) _crc_make_setter(message, m)
139 #endif /* 0 */
140
141 void
142 ws_add_crash_info(const char *fmt, ...)
143 {
144         va_list ap;
145         char *m, *old_info, *new_info;
146
147         va_start(ap, fmt);
148         m = g_strdup_vprintf(fmt, ap);
149         va_end(ap);
150         if (__crashreporter_info__ == NULL)
151                 __crashreporter_info__ = m;
152         else {
153                 old_info = __crashreporter_info__;
154                 new_info = g_strdup_printf("%s\n%s", old_info, m);
155                 g_free(m);
156                 __crashreporter_info__ = new_info;
157                 g_free(old_info);
158         }
159 }
160
161 #else /* __APPLE__ */
162 /*
163  * Perhaps Google Breakpad (http://code.google.com/p/google-breakpad/) or
164  * other options listed at
165  * http://stackoverflow.com/questions/7631908/library-for-logging-call-stack-at-runtime-windows-linux
166  * ?
167  */
168 void
169 ws_add_crash_info(const char *fmt _U_, ...)
170 {
171 }
172 #endif /* __APPLE__ */
173
174 /*
175  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
176  *
177  * Local variables:
178  * c-basic-offset: 8
179  * tab-width: 8
180  * indent-tabs-mode: t
181  * End:
182  *
183  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
184  * :indentSize=8:tabSize=8:noTabs=false:
185  */