OLD | NEW |
1 # Copyright (C) 2010, Google Inc. All rights reserved. | 1 # Copyright (C) 2010, Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 23 matching lines...) Expand all Loading... |
34 sys.path.insert(0, "/path/to/tools/gdb/") | 34 sys.path.insert(0, "/path/to/tools/gdb/") |
35 import webkit | 35 import webkit |
36 """ | 36 """ |
37 | 37 |
38 from __future__ import print_function | 38 from __future__ import print_function |
39 | 39 |
40 import gdb | 40 import gdb |
41 import re | 41 import re |
42 import struct | 42 import struct |
43 | 43 |
44 | |
45 def guess_string_length(ptr): | 44 def guess_string_length(ptr): |
46 """Guess length of string pointed by ptr. | 45 """Guess length of string pointed by ptr. |
47 | 46 |
48 Returns a tuple of (length, an error message). | 47 Returns a tuple of (length, an error message). |
49 """ | 48 """ |
50 # Try to guess at the length. | 49 # Try to guess at the length. |
51 for i in xrange(0, 2048): | 50 for i in range(0, 2048): |
52 try: | 51 try: |
53 if int((ptr + i).dereference()) == 0: | 52 if int((ptr + i).dereference()) == 0: |
54 return i, '' | 53 return i, '' |
55 except RuntimeError: | 54 except RuntimeError: |
56 # We indexed into inaccessible memory; give up. | 55 # We indexed into inaccessible memory; give up. |
57 return i, ' (gdb hit inaccessible memory)' | 56 return i, ' (gdb hit inaccessible memory)' |
58 return 256, ' (gdb found no trailing NUL)' | 57 return 256, ' (gdb found no trailing NUL)' |
59 | 58 |
60 | 59 |
61 def ustring_to_string(ptr, length=None): | 60 def ustring_to_string(ptr, length=None): |
62 """Convert a pointer to UTF-16 data into a Python string encoded with utf-8. | 61 """Convert a pointer to UTF-16 data into a Python string encoded with utf-8. |
63 | 62 |
64 ptr and length are both gdb.Value objects. | 63 ptr and length are both gdb.Value objects. |
65 If length is unspecified, will guess at the length.""" | 64 If length is unspecified, will guess at the length.""" |
66 error_message = '' | 65 error_message = '' |
67 if length is None: | 66 if length is None: |
68 length, error_message = guess_string_length(ptr) | 67 length, error_message = guess_string_length(ptr) |
69 else: | 68 else: |
70 length = int(length) | 69 length = int(length) |
71 char_vals = [int((ptr + i).dereference()) for i in xrange(length)] | 70 char_vals = [int((ptr + i).dereference()) for i in range(length)] |
72 string = struct.pack('H' * length, *char_vals).decode('utf-16', 'replace').e
ncode('utf-8') | 71 string = struct.pack('H' * length, *char_vals).decode('utf-16', 'replace').e
ncode('utf-8') |
73 return string + error_message | 72 return string + error_message |
74 | 73 |
75 | 74 |
76 def lstring_to_string(ptr, length=None): | 75 def lstring_to_string(ptr, length=None): |
77 """Convert a pointer to LChar* data into a Python (non-Unicode) string. | 76 """Convert a pointer to LChar* data into a Python (non-Unicode) string. |
78 | 77 |
79 ptr and length are both gdb.Value objects. | 78 ptr and length are both gdb.Value objects. |
80 If length is unspecified, will guess at the length.""" | 79 If length is unspecified, will guess at the length.""" |
81 error_message = '' | 80 error_message = '' |
82 if length is None: | 81 if length is None: |
83 length, error_message = guess_string_length(ptr) | 82 length, error_message = guess_string_length(ptr) |
84 else: | 83 else: |
85 length = int(length) | 84 length = int(length) |
86 string = ''.join([chr((ptr + i).dereference()) for i in xrange(length)]) | 85 string = ''.join([chr((ptr + i).dereference()) for i in range(length)]) |
87 return string + error_message | 86 return string + error_message |
88 | 87 |
89 | 88 |
90 class StringPrinter(object): | 89 class StringPrinter(object): |
91 "Shared code between different string-printing classes" | 90 "Shared code between different string-printing classes" |
92 def __init__(self, val): | 91 def __init__(self, val): |
93 self.val = val | 92 self.val = val |
94 | 93 |
95 def display_hint(self): | 94 def display_hint(self): |
96 return 'string' | 95 return 'string' |
(...skipping 16 matching lines...) Expand all Loading... |
113 def to_string(self): | 112 def to_string(self): |
114 return self.val['m_string'] | 113 return self.val['m_string'] |
115 | 114 |
116 | 115 |
117 class WTFCStringPrinter(StringPrinter): | 116 class WTFCStringPrinter(StringPrinter): |
118 "Print a WTF::CString" | 117 "Print a WTF::CString" |
119 def to_string(self): | 118 def to_string(self): |
120 # The CString holds a buffer, which is a refptr to a WTF::CStringBuffer. | 119 # The CString holds a buffer, which is a refptr to a WTF::CStringBuffer. |
121 data = self.val['m_buffer']['m_ptr']['m_data'].cast(gdb.lookup_type('cha
r').pointer()) | 120 data = self.val['m_buffer']['m_ptr']['m_data'].cast(gdb.lookup_type('cha
r').pointer()) |
122 length = self.val['m_buffer']['m_ptr']['m_length'] | 121 length = self.val['m_buffer']['m_ptr']['m_length'] |
123 return ''.join([chr((data + i).dereference()) for i in xrange(length)]) | 122 return ''.join([chr((data + i).dereference()) for i in range(length)]) |
124 | 123 |
125 | 124 |
126 class WTFStringImplPrinter(StringPrinter): | 125 class WTFStringImplPrinter(StringPrinter): |
127 "Print a WTF::StringImpl" | 126 "Print a WTF::StringImpl" |
128 def get_length(self): | 127 def get_length(self): |
129 return self.val['m_length'] | 128 return self.val['m_length'] |
130 | 129 |
131 def to_string(self): | 130 def to_string(self): |
132 chars_start = self.val.address + 1 | 131 chars_start = self.val.address + 1 |
133 if self.is_8bit(): | 132 if self.is_8bit(): |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 padding = '' | 375 padding = '' |
377 while len(stack) > 0: | 376 while len(stack) > 0: |
378 pair = stack.pop() | 377 pair = stack.pop() |
379 print(padding, pair[1], pair[0]) | 378 print(padding, pair[1], pair[0]) |
380 padding = padding + ' ' | 379 padding = padding + ' ' |
381 else: | 380 else: |
382 print('Sorry: I don\'t know how to deal with %s yet.' % target_type) | 381 print('Sorry: I don\'t know how to deal with %s yet.' % target_type) |
383 | 382 |
384 | 383 |
385 PrintPathToRootCommand() | 384 PrintPathToRootCommand() |
OLD | NEW |