2 * Worldvisions Weaver Software:
3 * Copyright (C) 2005 Net Integration Technologies, Inc.
5 * Routines to save messages that can be logged when a program crashes.
14 IWvStream
*WvCrashInfo::in_stream
= NULL
;
15 const char *WvCrashInfo::in_stream_id
= NULL
;
16 enum WvCrashInfo::InStreamState
WvCrashInfo::in_stream_state
= UNUSED
;
18 // FIXME: this file mostly only works in Linux
22 static const char *argv0
= program_invocation_short_name
;
24 static const char *argv0
= "UNKNOWN";
27 // Reserve enough buffer for a screenful of programme.
28 static const int buffer_size
= 2048;
29 static char will_msg
[buffer_size
];
30 static char assert_msg
[buffer_size
];
32 static const int ring_buffer_order
= wvcrash_ring_buffer_order
;
33 static const int ring_buffer_size
= wvcrash_ring_buffer_size
;
34 static const int ring_buffer_mask
= ring_buffer_size
- 1;
35 static char ring_buffer
[ring_buffer_size
+1];
36 static int ring_buffer_start
= 0, ring_buffer_used
= 0;
41 void __assert_fail(const char *__assertion
, const char *__file
,
42 unsigned int __line
, const char *__function
)
44 // Set the assert message that WvCrash will dump.
45 snprintf(assert_msg
, buffer_size
,
46 "%s: %s:%u: %s: Assertion `%s' failed.\n",
47 argv0
, __file
, __line
, __function
, __assertion
);
48 assert_msg
[buffer_size
- 1] = '\0';
50 // Emulate the GNU C library's __assert_fail().
51 fprintf(stderr
, "%s: %s:%u: %s: Assertion `%s' failed.\n",
52 argv0
, __file
, __line
, __function
, __assertion
);
57 // Wrapper for standards compliance.
58 void __assert(const char *__assertion
, const char *__file
,
59 unsigned int __line
, const char *__function
)
61 __assert_fail(__assertion
, __file
, __line
, __function
);
65 // Support the GNU assert_perror() extension.
66 void __assert_perror_fail(int __errnum
, const char *__file
,
67 unsigned int __line
, const char *__function
)
69 // Set the assert message that WvCrash will dump.
70 snprintf(assert_msg
, buffer_size
,
71 "%s: %s:%u: %s: Unexpected error: %s.\n",
72 argv0
, __file
, __line
, __function
, strerror(__errnum
));
73 assert_msg
[buffer_size
- 1] = '\0';
75 // Emulate the GNU C library's __assert_perror_fail().
76 fprintf(stderr
, "%s: %s:%u: %s: Unexpected error: %s.\n",
77 argv0
, __file
, __line
, __function
, strerror(__errnum
));
83 // This function is meant to support people who wish to leave a last will
84 // and testament in the WvCrash.
85 void wvcrash_leave_will(const char *will
)
89 strncpy(will_msg
, will
, buffer_size
);
90 will_msg
[buffer_size
- 1] = '\0';
97 const char *wvcrash_read_will()
103 const char *wvcrash_read_assert()
109 void wvcrash_ring_buffer_put(const char *str
)
111 wvcrash_ring_buffer_put(str
, strlen(str
));
115 void wvcrash_ring_buffer_put(const char *str
, size_t len
)
119 int pos
= (ring_buffer_start
+ ring_buffer_used
) & ring_buffer_mask
;
120 ring_buffer
[pos
] = *str
++;
122 if (ring_buffer_used
== ring_buffer_size
)
123 ring_buffer_start
= (ring_buffer_start
+ 1) & ring_buffer_mask
;
130 const char *wvcrash_ring_buffer_get()
132 if (ring_buffer_used
== 0)
135 if (ring_buffer_start
+ ring_buffer_used
>= ring_buffer_size
)
137 ring_buffer
[ring_buffer_size
] = '\0';
138 result
= &ring_buffer
[ring_buffer_start
];
139 ring_buffer_used
-= ring_buffer_size
- ring_buffer_start
;
140 ring_buffer_start
= 0;
144 ring_buffer
[ring_buffer_start
+ ring_buffer_used
] = '\0';
145 result
= &ring_buffer
[ring_buffer_start
];
146 ring_buffer_start
+= ring_buffer_used
;
147 ring_buffer_used
= 0;
153 void __wvcrash_init_buffers(const char *program_name
)
156 argv0
= program_name
;
158 assert_msg
[0] = '\0';
164 void wvcrash_leave_will(const char *will
) {}
165 const char *wvcrash_read_will() { return NULL
; }