python/gdbpp/gdbpp/smartptr.py
author Wei-Cheng Pan <wpan@mozilla.com>
Wed, 31 Aug 2016 18:35:01 +0800
changeset 313151 074f9de56ac0420e644b2bab7cade77d736d67b9
parent 311797 f64c40aedc48f79b9334fe8daa79dcb5023b06f5
child 315285 9fec8d313c491cd8437315a30079fd200ddde3ee
permissions -rw-r--r--
Bug 1298775 - Fix nullptr dereferencing. r=tbsaunde This is a follow up to fix nullptr printing. MozReview-Commit-ID: KDjBsHHiDxv

# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import gdb
from gdbpp import GeckoPrettyPrinter

@GeckoPrettyPrinter('nsWeakPtr', '^nsCOMPtr<nsIWeakReference>$')
class weak_ptr_printer(object):
    def __init__(self, value):
        self.value = value

    def to_string(self):
        proxy = self.value['mRawPtr']
        if not proxy:
            return '[(%s) 0x0]' % proxy.type

        ref_type = proxy.dynamic_type
        weak_ptr = proxy.cast(ref_type).dereference()['mReferent']
        if not weak_ptr:
            return '[(%s) %s]' % (weak_ptr.type, weak_ptr)

        return '[(%s) %s]' % (weak_ptr.dynamic_type, weak_ptr)

@GeckoPrettyPrinter('mozilla::StaticAutoPtr', '^mozilla::StaticAutoPtr<.*>$')
@GeckoPrettyPrinter('mozilla::StaticRefPtr', '^mozilla::StaticRefPtr<.*>$')
@GeckoPrettyPrinter('nsAutoPtr', '^nsAutoPtr<.*>$')
@GeckoPrettyPrinter('nsCOMPtr', '^nsCOMPtr<.*>$')
@GeckoPrettyPrinter('RefPtr', '^RefPtr<.*>$')
class smartptr_printer(object):
    def __init__(self, value):
        self.value = value['mRawPtr']

    def to_string(self):
        if not self.value:
            type_name = str(self.value.type)
        else:
            type_name = str(self.value.dereference().dynamic_type.pointer())

        return '[(%s) %s]' % (type_name, str(self.value))

@GeckoPrettyPrinter('mozilla::StyleSheetHandle::RefPtr', '^mozilla::HandleRefPtr<mozilla::StyleSheetHandle>$')
class sheetptr_printer(object):
    def __init__(self, value):
        self.value = 0
        if (value['mHandle'] and
            value['mHandle']['mPtr'] and
            value['mHandle']['mPtr']['mValue']):
            self.value = int(value['mHandle']['mPtr']['mValue'])

    def to_string(self):
        if self.value == 0:
            type_name = 'mozilla::StyleSheet *'
            value = 0
        else:
            value = int(self.value)
            if value & 0x1:
                value = value & ~0x1
                type_name = 'mozilla::ServoStyleSheet *'
            else:
                type_name = 'mozilla::CSSStyleSheet *'

        return '[(%s) %s]' % (type_name, hex(value))