]> git.sesse.net Git - vlc/blob - projects/macosx/vlc_app/Sources/VLCExceptionHandler.m
03aff6146429de95e97232cf8ed0d8481b95188d
[vlc] / projects / macosx / vlc_app / Sources / VLCExceptionHandler.m
1 /*****************************************************************************
2  * VLCExceptionHandler.m: VLCExceptionHandler implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCExceptionHandler.h"
26 #import <ExceptionHandling/ExceptionHandling.h>
27
28
29 @implementation VLCExceptionHandler
30 + (void)load
31 {
32     [[NSExceptionHandler defaultExceptionHandler] setDelegate:[[VLCExceptionHandler alloc] init]];
33  
34     [[NSExceptionHandler defaultExceptionHandler] setExceptionHandlingMask:
35                 0xffff /* Catch all */ ];
36
37     [[NSExceptionHandler defaultExceptionHandler] setExceptionHangingMask:
38                 NSHangOnUncaughtExceptionMask|
39                 NSHangOnUncaughtSystemExceptionMask|
40                 NSHangOnUncaughtRuntimeErrorMask|
41                 NSHangOnTopLevelExceptionMask|
42                 NSHangOnOtherExceptionMask];
43 }
44
45 /* From Apple's guide on exception */
46 - (BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldLogException:(NSException *)exception mask:(unsigned int)aMask
47 {
48     [self printStackTrace:exception];
49     NSLog(@"*** Exception Handled! %@: %@", [exception name], [exception reason]);
50     int ret = NSRunCriticalAlertPanel(@"Exception not handled!",
51                             [NSString stringWithFormat:@"%@: %@\n\nBack trace has been printed to Console.\n\nWe will now wait for debugger connection...\n",
52                                 [exception name], [exception reason]],
53                             @"Quit", @"Wait Debugger", nil);
54     if( ret == NSOKButton )
55     {
56         [NSApp terminate:self];
57     }
58     return YES;
59 }
60
61 - (void)printStackTrace:(NSException *)e
62 {
63     NSString *stack = [[e userInfo] objectForKey:NSStackTraceKey];
64     if (!stack)
65     {
66         NSLog(@"No stack trace available.");
67         return;
68     }
69
70     NSTask *ls = [[NSTask alloc] init];
71     NSString *pid = [[NSNumber numberWithInt:[[NSProcessInfo processInfo] processIdentifier]] stringValue];
72     NSMutableArray *args = [NSMutableArray arrayWithCapacity:20];
73
74     [args addObject:@"-p"];
75     [args addObject:pid];
76     [args addObjectsFromArray:[stack componentsSeparatedByString:@"  "]];
77     /* Note: function addresses are separated by double spaces, not a single space. */
78
79     [ls setLaunchPath:@"/usr/bin/atos"];
80     [ls setArguments:args];
81     [ls launch];
82     [ls release];
83 }
84
85 @end