ObjFW  Check-in [a889f21180]

Overview
Comment:Don't depened on execinfo.h for backtraces.

Instead, __builtin_return_address() is used now. This makes backtraces
available on many platforms, it even works if dladdr is missing (it does
not show symbol names then, though).

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a889f21180f276e71ee7413dbbd5b534ce61bc45bd6c842947ccb4cba5097b4b
User & Date: js on 2013-05-22 20:36:31
Other Links: manifest | tags
Context
2013-05-22
21:09
Show a backtrace for uncaught exceptions. check-in: 32230a1615 user: js tags: trunk
20:36
Don't depened on execinfo.h for backtraces. check-in: a889f21180 user: js tags: trunk
18:45
Import OFThread.h unconditionally. check-in: d7365be372 user: js tags: trunk
Changes

Modified configure.ac from [b23f940e4b] to [c6b89e288b].

464
465
466
467
468
469
470

471
472
473
474
475
476
477
], [
	AC_CHECK_FUNC(random, [
		AC_DEFINE(HAVE_RANDOM, 1, [Whether we have random()])
	])
])

AC_CHECK_LIB(dl, dlopen, LIBS="$LIBS -ldl")


AC_ARG_ENABLE(threads,
	AS_HELP_STRING([--disable-threads], [disable thread support]))
AS_IF([test x"$enable_threads" != x"no"], [
	case "$host_os" in
	mingw*)
		AC_MSG_CHECKING(for threads)







>







464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
], [
	AC_CHECK_FUNC(random, [
		AC_DEFINE(HAVE_RANDOM, 1, [Whether we have random()])
	])
])

AC_CHECK_LIB(dl, dlopen, LIBS="$LIBS -ldl")
AC_CHECK_HEADERS(dlfcn.h)

AC_ARG_ENABLE(threads,
	AS_HELP_STRING([--disable-threads], [disable thread support]))
AS_IF([test x"$enable_threads" != x"no"], [
	case "$host_os" in
	mingw*)
		AC_MSG_CHECKING(for threads)
746
747
748
749
750
751
752

753








754
755
756
757
758
759
760
		;;
esac
AS_IF([test x"$have_processes" = x"yes"], [
	AC_SUBST(OFPROCESS_M, "OFProcess.m")
	AC_DEFINE(OF_HAVE_PROCESSES, 1, [Whether we have processes])
])


AC_CHECK_HEADERS(execinfo.h)









AS_IF([test x"$objc_runtime" = x"Apple runtime"], [
	AC_CHECK_HEADER(Foundation/NSObject.h, [
		AC_SUBST(FOUNDATION_COMPAT_M, "foundation-compat.m")
		AC_SUBST(BRIDGE, "bridge")

		AS_IF([test x"$enable_shared" != x"no"], [







>
|
>
>
>
>
>
>
>
>







747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
		;;
esac
AS_IF([test x"$have_processes" = x"yes"], [
	AC_SUBST(OFPROCESS_M, "OFProcess.m")
	AC_DEFINE(OF_HAVE_PROCESSES, 1, [Whether we have processes])
])

AC_MSG_CHECKING(for __builtin_return_address)
AC_TRY_LINK([], [
	__builtin_return_address(0);
], [
	AC_MSG_RESULT(yes)
	AC_DEFINE(HAVE_BUILTIN_RETURN_ADDRESS, 1,
		[Whether we have __builtin_return_address])
], [
	AC_MSG_RESULT(no)
])

AS_IF([test x"$objc_runtime" = x"Apple runtime"], [
	AC_CHECK_HEADER(Foundation/NSObject.h, [
		AC_SUBST(FOUNDATION_COMPAT_M, "foundation-compat.m")
		AC_SUBST(BRIDGE, "bridge")

		AS_IF([test x"$enable_shared" != x"no"], [

Modified src/OFPlugin.m from [ae3dcbbf47] to [b063f0c48a].

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#ifndef _WIN32
#include <dlfcn.h>
#endif

#import "OFPlugin.h"
#import "OFString.h"

#import "OFInitializationFailedException.h"








|
|







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#ifdef HAVE_DLFCN_H
# include <dlfcn.h>
#endif

#import "OFPlugin.h"
#import "OFString.h"

#import "OFInitializationFailedException.h"

Modified src/exceptions/OFException.h from [8c5ce69b79] to [a50325ba8b].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"

#define OF_EXCEPTION_MAX_BACKTRACE_SIZE 32

@class OFString;
@class OFArray;
@class OFMutableArray;

/*!
 * @brief The base class for all exceptions in ObjFW
 *
 * The OFException class is the base class for all exceptions in ObjFW, except
 * the OFAllocFailedException.
 */
@interface OFException: OFObject
{
	Class _inClass;
	OFMutableArray *_backtrace;
	void *_backtraceBuffer[OF_EXCEPTION_MAX_BACKTRACE_SIZE];
	int _backtraceSize;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly) Class inClass;
#endif

/*!







<
<













<
|
<







12
13
14
15
16
17
18


19
20
21
22
23
24
25
26
27
28
29
30
31

32

33
34
35
36
37
38
39
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFObject.h"



@class OFString;
@class OFArray;
@class OFMutableArray;

/*!
 * @brief The base class for all exceptions in ObjFW
 *
 * The OFException class is the base class for all exceptions in ObjFW, except
 * the OFAllocFailedException.
 */
@interface OFException: OFObject
{
	Class _inClass;

	void *_returnAddresses[32];

}

#ifdef OF_HAVE_PROPERTIES
@property (readonly) Class inClass;
#endif

/*!

Modified src/exceptions/OFException.m from [122354a46f] to [18d7beaf46].

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * file.
 */

#include "config.h"

#include <stdlib.h>

#ifdef HAVE_EXECINFO_H
# include <execinfo.h>
#endif

#import "OFException.h"
#import "OFString.h"
#import "OFArray.h"

#import "autorelease.h"







|
|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * file.
 */

#include "config.h"

#include <stdlib.h>

#ifdef HAVE_DLFCN_H
# include <dlfcn.h>
#endif

#import "OFException.h"
#import "OFString.h"
#import "OFArray.h"

#import "autorelease.h"
47
48
49
50
51
52
53
54









55
56


































57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84


85
86
87
88
89
90


91
92
93
94

95

96
97
98
99
100
101

102
103
104

105
106
107
108
109
110
111

112
113
114
115
116
117
118
119
120
}

- initWithClass: (Class)class
{
	self = [super init];

	_inClass = class;
#ifdef HAVE_EXECINFO_H









	_backtraceSize = backtrace(_backtraceBuffer,
	    OF_EXCEPTION_MAX_BACKTRACE_SIZE);


































#endif

	return self;
}

- (void)dealloc
{
	[_backtrace release];

	[super dealloc];
}

- (Class)inClass
{
	return _inClass;
}

- (OFString*)description
{
	return [OFString stringWithFormat:
	    @"An exception of class %@ occurred in class %@!",
	    object_getClass(self), _inClass];
}

- (OFArray*)backtrace
{
#ifdef HAVE_EXECINFO_H
	char **symbols;



	if (_backtrace != nil)
		return _backtrace;

	if (_backtraceSize < 1)
		return nil;



	symbols = backtrace_symbols(_backtraceBuffer, _backtraceSize);
	@try {
		int i;



		_backtrace = [[OFMutableArray alloc] init];

		for (i = 0; i < _backtraceSize; i++) {
			void *pool = objc_autoreleasePoolPush();
			OFString *symbol;


			symbol = [OFString
			    stringWithCString: symbols[i]
				     encoding: OF_STRING_ENCODING_NATIVE];

			[_backtrace addObject: symbol];

			objc_autoreleasePoolPop(pool);
		}
	} @finally {
		free(symbols);
	}


	[_backtrace makeImmutable];

	return _backtrace;
#else
	return nil;
#endif
}
@end







|
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<
<
<
<
<
<
<














|
|
>
>

<
|
|
<
|
>
>

<
<
|
>

>
|

|
<
|
|
>
|
|
<
>
|
|
<
|
<
<
|
>

|

|





47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

123
124

125
126
127
128


129
130
131
132
133
134
135

136
137
138
139
140

141
142
143

144


145
146
147
148
149
150
151
152
153
154
155
}

- initWithClass: (Class)class
{
	self = [super init];

	_inClass = class;
#ifdef HAVE_BUILTIN_RETURN_ADDRESS
	/*
	 * We can't use a loop here, as __builtin_return_address() and
	 * __builtin_frame_address() only allow a constant as parameter.
	 */
# define GET_FRAME(i)					\
	if (__builtin_frame_address(i + 1) == NULL)	\
		goto backtrace_done;			\
	if ((_returnAddresses[i] = (			\
	    __builtin_return_address(i))) == NULL)	\
		goto backtrace_done;

	GET_FRAME(0)
	GET_FRAME(1)
	GET_FRAME(2)
	GET_FRAME(3)
	GET_FRAME(4)
	GET_FRAME(5)
	GET_FRAME(6)
	GET_FRAME(7)
	GET_FRAME(8)
	GET_FRAME(9)
	GET_FRAME(10)
	GET_FRAME(11)
	GET_FRAME(12)
	GET_FRAME(13)
	GET_FRAME(14)
	GET_FRAME(15)
	GET_FRAME(16)
	GET_FRAME(17)
	GET_FRAME(18)
	GET_FRAME(19)
	GET_FRAME(20)
	GET_FRAME(21)
	GET_FRAME(22)
	GET_FRAME(23)
	GET_FRAME(24)
	GET_FRAME(25)
	GET_FRAME(26)
	GET_FRAME(27)
	GET_FRAME(28)
	GET_FRAME(29)
	GET_FRAME(30)
	GET_FRAME(31)
# undef GET_FRAME
backtrace_done:
#endif

	return self;
}








- (Class)inClass
{
	return _inClass;
}

- (OFString*)description
{
	return [OFString stringWithFormat:
	    @"An exception of class %@ occurred in class %@!",
	    object_getClass(self), _inClass];
}

- (OFArray*)backtrace
{
#ifdef HAVE_BUILTIN_RETURN_ADDRESS
	OFMutableArray *backtrace = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();
	uint_fast8_t i;


	for (i = 0; i < 32 && _returnAddresses[i] != NULL; i++) {
		void *addr =

		    __builtin_extract_return_addr(_returnAddresses[i]);
# ifdef HAVE_DLFCN_H
		Dl_info info;



		if (dladdr(addr, &info)) {
			ptrdiff_t offset = (char*)addr - (char*)info.dli_saddr;

			if (info.dli_sname == NULL)
				info.dli_sname = "??";

			[backtrace addObject:

			    [OFString stringWithFormat: @"%p <%s+%td> at %s",
							addr, info.dli_sname,
							offset,
							info.dli_fname]];
		} else

# endif
			[backtrace addObject:
			    [OFString stringWithFormat: @"%p", addr]];

	}



	objc_autoreleasePoolPop(pool);

	[backtrace makeImmutable];

	return backtrace;
#else
	return nil;
#endif
}
@end