This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrace.c
67 lines (55 loc) · 2.16 KB
/
backtrace.c
1
2
3
4
5
6
7
8
9
10
11
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
+------------------------------------------------------------------------+
| Zephir Language |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Zephir Team (http://www.zephir-lang.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Vladimir Kolesnikov <[email protected]> |
+------------------------------------------------------------------------+
*/
#ifndef ZEPHIR_RELEASE
#if defined(linux)
#include <execinfo.h>
#include <Zend/zend.h>
#include <ext/standard/php_smart_str.h>
/**
* A buffer for backtrace. It is better to have it allocated statically
* in order not to face out of memory conditions later
*/
void *backtrace_buf[4096];
void zephir_print_backtrace(void)
{
int i;
int stack_size = backtrace(backtrace_buf, sizeof(backtrace_buf) / sizeof(void*));
char **stack_symbols = backtrace_symbols(backtrace_buf, stack_size);
char buf[50];
smart_str s;
s.c = NULL;
for (i = 0; i < stack_size; ++i) {
snprintf(buf, sizeof(buf), "#%d %p [", i, backtrace_buf[i]);
smart_str_appends(&s, buf);
smart_str_appends(&s, stack_symbols[i]);
smart_str_appends(&s, "]\n");
}
smart_str_0(&s);
fprintf(stderr, "%s\n", s.c);
smart_str_free(&s);
}
#else
void zephir_print_backtrace(void)
{
/**
* Not implemented yet for anything other than Linux
*/
}
#endif
#endif /* ZEPHIR_RELEASE */