update credits
[LibreOffice.git] / solenv / gdb / libreoffice / sal.py
blobc7f4d7c6de4bafed1686686f4a9f75c0944f8744
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import gdb
11 import gdb.types
13 from libreoffice.util import printing
14 from libreoffice.util.string import StringPrinterHelper
16 class RtlStringPrinter(StringPrinterHelper):
17 '''Prints rtl_String or rtl_uString'''
19 def __init__(self, typename, val, encoding = None):
20 super(RtlStringPrinter, self).__init__(typename, val, encoding)
22 def data(self):
23 return self.val['buffer']
25 def length(self):
26 return self.val['length']
28 class StringPrinter(StringPrinterHelper):
29 '''Prints rtl:: strings and string buffers'''
31 def __init__(self, typename, val, encoding = None):
32 super(StringPrinter, self).__init__(typename, val, encoding)
34 def valid(self):
35 return self.val['pData']
37 def data(self):
38 assert self.val['pData']
39 return self.val['pData'].dereference()['buffer']
41 def length(self):
42 assert self.val['pData']
43 return self.val['pData'].dereference()['length']
45 class SalUnicodePrinter(StringPrinterHelper):
46 '''Prints a sal_Unicode*'''
48 def __init__(self, typename, val):
49 super(SalUnicodePrinter, self).__init__(typename, val, 'utf-16')
51 def data(self):
52 return self.val
54 @staticmethod
55 def query(type):
56 type = type.unqualified()
57 if type.code != gdb.TYPE_CODE_PTR:
58 return False
59 return str(type.target()) == 'sal_Unicode'
61 class RtlReferencePrinter(object):
62 '''Prints rtl::Reference'''
64 def __init__(self, typename, val):
65 self.typename = typename
66 self.val = val
68 def to_string(self):
69 pointee = self.val['m_pBody']
70 if pointee:
71 val = pointee.dereference()
72 return '%s to %s' % (self.typename, str(val))
73 else:
74 return "empty %s" % self.typename
76 class OslFileStatusPrinter(object):
77 '''Prints oslFileStatus'''
79 def __init__(self, typename, val):
80 self.val = val
82 def to_string(self):
83 osl_file_type = gdb.lookup_type('oslFileType').strip_typedefs()
84 fields_to_enum_val = gdb.types.make_enum_dict(osl_file_type)
86 etype = self.field_val_if_valid('eType')
87 if etype is not None:
88 pretty_etype = '<unknown type>' # in case it's not one of the fields
90 for field_name, field_val in fields_to_enum_val.iteritems():
91 if etype == field_val:
92 pretty_etype = self.pretty_file_type(field_name)
93 else:
94 pretty_etype = '<invalid type>'
96 file_url = self.field_val_if_valid('ustrFileURL')
97 if file_url is not None:
98 pretty_file_url = str(file_url.dereference())
99 else:
100 pretty_file_url = '<invalid file url>'
102 pretty_file_status = pretty_etype + ': ' + pretty_file_url
104 # for links append the link target if valid
105 if etype == fields_to_enum_val['osl_File_Type_Link']:
106 link_target = self.field_val_if_valid('ustrLinkTargetURL')
107 if link_target is None:
108 pretty_link_target = '<invalid link target>'
109 else:
110 pretty_link_target = str(link_target.dereference())
112 pretty_file_status += ' -> ' + pretty_link_target
114 return pretty_file_status
116 def pretty_file_type(self, file_type_name):
117 if file_type_name != 'osl_File_Type_Regular':
118 return file_type_name.replace('osl_File_Type_', '').lower()
119 else:
120 return 'file' # regular is not very descriptive, file is better
122 def field_val_if_valid(self, field):
123 mask_for_field = {'eType': 0x00000001,
124 'uAttributes': 0x00000002,
125 'aCreationTime': 0x00000010,
126 'aAccessTime': 0x00000020,
127 'aModifyTime': 0x00000040,
128 'uFileSize': 0x00000080,
129 'ustrFileName': 0x00000100,
130 'ustrFileURL': 0x00000200,
131 'ustrLinkTargetURL': 0x00000400}
133 valid_fields = self.val['uValidFields']
134 if valid_fields & mask_for_field[field]:
135 return self.val[field]
136 else:
137 return None
139 printer = None
141 def build_pretty_printers():
142 global printer
144 printer = printing.Printer("libreoffice/sal")
146 # strings and string buffers
147 printer.add('_rtl_String', RtlStringPrinter)
148 printer.add('_rtl_uString', lambda name, val: RtlStringPrinter(name,
149 val, 'utf-16le'))
150 printer.add('rtl::OString', StringPrinter)
151 printer.add('rtl::OUString', lambda name, val: StringPrinter(name, val, 'utf-16'))
152 printer.add('rtl::OStringBuffer', StringPrinter)
153 printer.add('rtl::OUStringBuffer', lambda name, val: StringPrinter(name, val, 'utf-16'))
154 printer.add('sal_Unicode', SalUnicodePrinter, SalUnicodePrinter.query)
156 # other stuff
157 printer.add('rtl::Reference', RtlReferencePrinter)
158 printer.add('_oslFileStatus', OslFileStatusPrinter)
160 return printer
162 def register_pretty_printers(obj):
163 printing.register_pretty_printer(printer, obj)
165 build_pretty_printers()
167 # vim:set shiftwidth=4 softtabstop=4 expandtab: