Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: src/platform-posix.h

Issue 18431004: Common implementation of OS::DumpBacktrace() and OS::StackWalk(). (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Templatized POSIX backtrace Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform-macos.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 10 matching lines...) Expand all
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #ifndef V8_PLATFORM_POSIX_H_ 28 #ifndef V8_PLATFORM_POSIX_H_
29 #define V8_PLATFORM_POSIX_H_ 29 #define V8_PLATFORM_POSIX_H_
30 30
31 #include <cxxabi.h>
32 #include <stdio.h>
33
34 #include "platform.h"
35
31 namespace v8 { 36 namespace v8 {
32 namespace internal { 37 namespace internal {
33 38
34 // Used by platform implementation files during OS::PostSetUp(). 39 // Used by platform implementation files during OS::PostSetUp().
35 void POSIXPostSetUp(); 40 void POSIXPostSetUp();
36 41
42 // Used by platform implementation files during OS::DumpBacktrace()
43 // and OS::StackWalk().
44 template<int (*backtrace)(void**, int),
45 char** (*backtrace_symbols)(void* const*, int)>
46 struct POSIXBacktraceHelper {
47 static void DumpBacktrace() {
48 void* trace[100];
49 int size = backtrace(trace, ARRAY_SIZE(trace));
50 char** symbols = backtrace_symbols(trace, size);
51 fprintf(stderr, "\n==== C stack trace ===============================\n\n");
52 if (size == 0) {
53 fprintf(stderr, "(empty)\n");
54 } else if (symbols == NULL) {
55 fprintf(stderr, "(no symbols)\n");
56 } else {
57 for (int i = 1; i < size; ++i) {
58 fprintf(stderr, "%2d: ", i);
59 char mangled[201];
60 if (sscanf(symbols[i], "%*[^(]%*[(]%200[^)+]", mangled) == 1) {// NOLINT
61 int status;
62 size_t length;
63 char* demangled = abi::__cxa_demangle(
64 mangled, NULL, &length, &status);
65 fprintf(stderr, "%s\n", demangled != NULL ? demangled : mangled);
66 free(demangled);
67 } else {
68 fprintf(stderr, "??\n");
69 }
70 }
71 }
72 fflush(stderr);
73 free(symbols);
74 }
75
76 static int StackWalk(Vector<OS::StackFrame> frames) {
77 int frames_size = frames.length();
78 ScopedVector<void*> addresses(frames_size);
79
80 int frames_count = backtrace(addresses.start(), frames_size);
81
82 char** symbols = backtrace_symbols(addresses.start(), frames_count);
83 if (symbols == NULL) {
84 return OS::kStackWalkError;
85 }
86
87 for (int i = 0; i < frames_count; i++) {
88 frames[i].address = addresses[i];
89 // Format a text representation of the frame based on the information
90 // available.
91 OS::SNPrintF(MutableCStrVector(frames[i].text, OS::kStackWalkMaxTextLen),
92 "%s", symbols[i]);
93 // Make sure line termination is in place.
94 frames[i].text[OS::kStackWalkMaxTextLen - 1] = '\0';
95 }
96
97 free(symbols);
98
99 return frames_count;
100 }
101 };
102
37 } } // namespace v8::internal 103 } } // namespace v8::internal
38 104
39 #endif // V8_PLATFORM_POSIX_H_ 105 #endif // V8_PLATFORM_POSIX_H_
OLDNEW
« no previous file with comments | « src/platform-macos.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698