Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 1 | # Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 5 | # Print tagged object. |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 6 | define job |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 7 | call (void) _v8_internal_Print_Object((void*)($arg0)) |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 8 | end |
| 9 | document job |
| 10 | Print a v8 JavaScript object |
| 11 | Usage: job tagged_ptr |
| 12 | end |
| 13 | |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 14 | # Print content of v8::internal::Handle. |
| 15 | define jh |
| 16 | call (void) _v8_internal_Print_Object(*((v8::internal::Object**)($arg0).location_)) |
| 17 | end |
| 18 | document jh |
| 19 | Print content of a v8::internal::Handle |
| 20 | Usage: jh internal_handle |
| 21 | end |
| 22 | |
| 23 | # Print content of v8::Local handle. |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 24 | define jlh |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 25 | call (void) _v8_internal_Print_Object(*((v8::internal::Object**)($arg0).val_)) |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 26 | end |
| 27 | document jlh |
| 28 | Print content of a v8::Local handle |
| 29 | Usage: jlh local_handle |
| 30 | end |
| 31 | |
| 32 | # Print Code objects containing given PC. |
| 33 | define jco |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 34 | call (void) _v8_internal_Print_Code((void*)($arg0)) |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 35 | end |
| 36 | document jco |
| 37 | Print a v8 Code object from an internal code address |
| 38 | Usage: jco pc |
| 39 | end |
| 40 | |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 41 | # Print LayoutDescriptor. |
| 42 | define jld |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 43 | call (void) _v8_internal_Print_LayoutDescriptor((void*)($arg0)) |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 44 | end |
| 45 | document jld |
| 46 | Print a v8 LayoutDescriptor object |
| 47 | Usage: jld tagged_ptr |
| 48 | end |
| 49 | |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 50 | # Print TransitionTree. |
| 51 | define jtt |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 52 | call (void) _v8_internal_Print_TransitionTree((void*)($arg0)) |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 53 | end |
| 54 | document jtt |
| 55 | Print the complete transition tree of the given v8 Map. |
| 56 | Usage: jtt tagged_ptr |
| 57 | end |
| 58 | |
| 59 | # Print JavaScript stack trace. |
| 60 | define jst |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 61 | call (void) _v8_internal_Print_StackTrace() |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 62 | end |
| 63 | document jst |
| 64 | Print the current JavaScript stack trace |
| 65 | Usage: jst |
| 66 | end |
| 67 | |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 68 | # Print TurboFan graph node. |
| 69 | define pn |
| 70 | call _v8_internal_Node_Print((void*)($arg0)) |
| 71 | end |
| 72 | document pn |
| 73 | Print a v8 TurboFan graph node |
| 74 | Usage: pn node_address |
| 75 | end |
| 76 | |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 77 | # Skip the JavaScript stack. |
| 78 | define jss |
| 79 | set $js_entry_sp=v8::internal::Isolate::Current()->thread_local_top()->js_entry_sp_ |
| 80 | set $rbp=*(void**)$js_entry_sp |
| 81 | set $rsp=$js_entry_sp + 2*sizeof(void*) |
| 82 | set $pc=*(void**)($js_entry_sp+sizeof(void*)) |
| 83 | end |
| 84 | document jss |
| 85 | Skip the jitted stack on x64 to where we entered JS last. |
| 86 | Usage: jss |
| 87 | end |
| 88 | |
| 89 | # Print stack trace with assertion scopes. |
| 90 | define bta |
| 91 | python |
| 92 | import re |
| 93 | frame_re = re.compile("^#(\d+)\s*(?:0x[a-f\d]+ in )?(.+) \(.+ at (.+)") |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 94 | assert_re = re.compile("^\s*(\S+) = .+<v8::internal::Per\w+AssertScope<v8::internal::(\S*), (false|true)>") |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 95 | btl = gdb.execute("backtrace full", to_string = True).splitlines() |
| 96 | for l in btl: |
| 97 | match = frame_re.match(l) |
| 98 | if match: |
| 99 | print("[%-2s] %-60s %-40s" % (match.group(1), match.group(2), match.group(3))) |
| 100 | match = assert_re.match(l) |
| 101 | if match: |
| 102 | if match.group(3) == "false": |
| 103 | prefix = "Disallow" |
| 104 | color = "\033[91m" |
| 105 | else: |
| 106 | prefix = "Allow" |
| 107 | color = "\033[92m" |
| 108 | print("%s -> %s %s (%s)\033[0m" % (color, prefix, match.group(2), match.group(1))) |
| 109 | end |
| 110 | end |
| 111 | document bta |
| 112 | Print stack trace with assertion scopes |
| 113 | Usage: bta |
| 114 | end |
| 115 | |
| 116 | # Search for a pointer inside all valid pages. |
| 117 | define space_find |
| 118 | set $space = $arg0 |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 119 | set $current_page = $space->first_page() |
| 120 | while ($current_page != 0) |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 121 | printf "# Searching in %p - %p\n", $current_page->area_start(), $current_page->area_end()-1 |
| 122 | find $current_page->area_start(), $current_page->area_end()-1, $arg1 |
| 123 | set $current_page = $current_page->next_page() |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | define heap_find |
| 128 | set $heap = v8::internal::Isolate::Current()->heap() |
| 129 | printf "# Searching for %p in old_space ===============================\n", $arg0 |
| 130 | space_find $heap->old_space() ($arg0) |
| 131 | printf "# Searching for %p in map_space ===============================\n", $arg0 |
| 132 | space_find $heap->map_space() $arg0 |
| 133 | printf "# Searching for %p in code_space ===============================\n", $arg0 |
| 134 | space_find $heap->code_space() $arg0 |
| 135 | end |
| 136 | document heap_find |
| 137 | Find the location of a given address in V8 pages. |
| 138 | Usage: heap_find address |
| 139 | end |
| 140 | |
| 141 | set disassembly-flavor intel |
| 142 | set disable-randomization off |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 143 | |
| 144 | # Install a handler whenever the debugger stops due to a signal. It walks up the |
| 145 | # stack looking for V8_Dcheck and moves the frame to the one above it so it's |
| 146 | # immediately at the line of code that triggered the DCHECK. |
| 147 | python |
| 148 | def dcheck_stop_handler(event): |
| 149 | frame = gdb.selected_frame() |
| 150 | select_frame = None |
| 151 | message = None |
| 152 | count = 0 |
| 153 | # limit stack scanning since they're usually shallow and otherwise stack |
| 154 | # overflows can be very slow. |
| 155 | while frame is not None and count < 5: |
| 156 | count += 1 |
| 157 | if frame.name() == 'V8_Dcheck': |
| 158 | frame_message = gdb.lookup_symbol('message', frame.block())[0] |
| 159 | if frame_message: |
| 160 | message = frame_message.value(frame).string() |
| 161 | select_frame = frame.older() |
| 162 | break |
| 163 | if frame.name() is not None and frame.name().startswith('V8_Fatal'): |
| 164 | select_frame = frame.older() |
| 165 | frame = frame.older() |
| 166 | |
| 167 | if select_frame is not None: |
| 168 | select_frame.select() |
| 169 | gdb.execute('frame') |
| 170 | if message: |
| 171 | print('DCHECK error: {}'.format(message)) |
| 172 | |
| 173 | gdb.events.stop.connect(dcheck_stop_handler) |
| 174 | end |
| 175 | |
| 176 | # Code imported from chromium/src/tools/gdb/gdbinit |
| 177 | python |
| 178 | |
| 179 | import os |
| 180 | import subprocess |
| 181 | import sys |
| 182 | |
| 183 | compile_dirs = set() |
| 184 | |
| 185 | |
| 186 | def get_current_debug_file_directories(): |
| 187 | dir = gdb.execute("show debug-file-directory", to_string=True) |
| 188 | dir = dir[ |
| 189 | len('The directory where separate debug symbols are searched for is "' |
| 190 | ):-len('".') - 1] |
| 191 | return set(dir.split(":")) |
| 192 | |
| 193 | |
| 194 | def add_debug_file_directory(dir): |
| 195 | # gdb has no function to add debug-file-directory, simulates that by using |
| 196 | # `show debug-file-directory` and `set debug-file-directory <directories>`. |
| 197 | current_dirs = get_current_debug_file_directories() |
| 198 | current_dirs.add(dir) |
| 199 | gdb.execute( |
| 200 | "set debug-file-directory %s" % ":".join(current_dirs), to_string=True) |
| 201 | |
| 202 | |
| 203 | def newobj_handler(event): |
| 204 | global compile_dirs |
| 205 | compile_dir = os.path.dirname(event.new_objfile.filename) |
| 206 | if not compile_dir: |
| 207 | return |
| 208 | if compile_dir in compile_dirs: |
| 209 | return |
| 210 | compile_dirs.add(compile_dir) |
| 211 | |
| 212 | # Add source path |
| 213 | gdb.execute("dir %s" % compile_dir) |
| 214 | |
| 215 | # Need to tell the location of .dwo files. |
| 216 | # https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html |
| 217 | # https://crbug.com/603286#c35 |
| 218 | add_debug_file_directory(compile_dir) |
| 219 | |
| 220 | # Event hook for newly loaded objfiles. |
| 221 | # https://sourceware.org/gdb/onlinedocs/gdb/Events-In-Python.html |
| 222 | gdb.events.new_objfile.connect(newobj_handler) |
| 223 | |
| 224 | gdb.execute("set environment V8_GDBINIT_SOURCED=1") |
| 225 | |
| 226 | end |