all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 3a9f17e02029b5e9657686e0c940321f7a60489b 6586 bytes (raw)
name: etc/emacs_lldb.py 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
 
# Copyright (C) 2022 Free Software Foundation, Inc.
#
# This file is part of GNU Emacs.
#
# GNU Emacs is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Emacs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

# Load this module in LLDB with
#
# (lldb) command script import emacs_lldb
#
# Available commands start with 'x' and can be seen with
#
# (lldb) help

import lldb

\f
########################################################################
#                              Utilties
########################################################################

# Return the Lisp_Type of Lisp_Object OBJ.
def get_lisp_type(obj):
    int_value = obj.GetValueAsUnsigned()
    return obj.GetFrame().EvaluateExpression(
        f"(enum Lisp_Type) ((EMACS_INT) {int_value} "
        "& (1 << GCTYPEBITS) - 1)")

# Return the Lisp_Type or pseudo-vector type of OBJ.
def get_lisp_type_or_vectorlike(obj):
    lisp_type = get_lisp_type(obj)
    if enumerator_name(lisp_type) == "Lisp_Vectorlike":
        vector = get_lisp_pointer(obj, "struct Lisp_Vector")
        header_size = vector.GetValueForExpressionPath(
            "->header.size").GetValueAsUnsigned()
        frame = obj.GetFrame()
        pseudo = frame.EvaluateExpression(
            f"{header_size} & PSEUDOVECTOR_FLAG")
        if pseudo.GetValueAsUnsigned() != 0:
            return frame.EvaluateExpression(
                f"(enum pvec_type) (({header_size} "
                "& More_Lisp_Bits::PVEC_TYPE_MASK) "
                ">> More_Lisp_Bits::PSEUDOVECTOR_AREA_BITS)")
        return frame.EvaluateExpression("pvec_type::PVEC_NORMAL_VECTOR")
    return lisp_type

# Return Lisp_Object OBJ as pointer to TYP *.
def get_lisp_pointer(obj, typ):
    return obj.GetFrame().EvaluateExpression(
        f"({typ}*) (((EMACS_INT) {obj.GetValueAsUnsigned()}) & VALMASK)")

# Return Lisp_Object OBJ as pointer to Lisp_Symbol.
def get_lisp_symbol(obj):
    ptr = get_lisp_pointer(obj, "char")
    offset = ptr.GetValueAsUnsigned()
    return obj.GetFrame().EvaluateExpression(
        f"(struct Lisp_Symbol *) ((char *) &lispsym + {offset})")

# Return Lisp_Object OBJ as pointer to Lisp_String
def get_lisp_string(obj):
    return get_lisp_pointer(obj, "struct Lisp_String")

# Return the string data of Lisp_Object OBJ which denotes a Lisp_String.
def get_lisp_string_data(obj):
    string = get_lisp_string(obj)
    return string.GetValueForExpressionPath("->u.s.data")

# Assuming OBJ denotes a Lisp_Symbol, return the name of the symbol.
def get_lisp_symbol_name(obj):
    sym = get_lisp_symbol(obj)
    name = sym.GetValueForExpressionPath("->u.s.name")
    return get_lisp_string_data(name)

# Return a string for the enuerator ENUM.
def enumerator_name(enum):
    enumerators = enum.GetType().GetEnumMembers()
    return enumerators[enum.GetValueAsUnsigned()].GetName()

\f
########################################################################
#                           LLDB Commands
########################################################################

def xbacktrace(debugger, command, ctx, result, internal_dict):
    """Print Emacs Lisp backtrace"""
    frame = ctx.GetFrame()
    n = frame.EvaluateExpression(
        "current_thread->m_specpdl_ptr - current_thread->m_specpdl")
    for i in reversed(range(0, n.GetValueAsUnsigned())):
        s = frame.EvaluateExpression(f"current_thread->m_specpdl[{i}]")
        kind = enumerator_name(s.GetChildMemberWithName("kind"))
        if kind == "SPECPDL_BACKTRACE":
            function = s.GetValueForExpressionPath(".bt.function")
            function_type = enumerator_name(get_lisp_type(function))
            if function_type == "Lisp_Symbol":
                sym_name = get_lisp_symbol_name(function)
                result.AppendMessage(str(sym_name))
            elif function_type == "Lisp_Vectorlike":
                subtype = get_lisp_type_or_vectorlike(function)
                result.AppendMessage(str(subtype))
            else:
                result.AppendMessage(function_type)

def xdebug_print(debugger, command, result, internal_dict):
    """Print Lisp_Objects using safe_debug_print()"""
    debugger.HandleCommand(f"expr safe_debug_print({command})")

\f
########################################################################
#                             Formatters
########################################################################

# Return a type summary for Lisp_Objects.
def format_Lisp_Object(obj, internal_dict):
    lisp_type = get_lisp_type_or_vectorlike(obj)
    kind = enumerator_name(lisp_type)
    summary = "-> "
    if kind == "PVEC_FRAME":
        ptr = get_lisp_pointer(obj, "struct frame")
        summary += str(ptr)
    elif kind == "PVEC_WINDOW":
        ptr = get_lisp_pointer(obj, "struct window")
        summary += str(ptr)
    return summary

\f
########################################################################
#                           Initialization
########################################################################

# Define Python FUNCTION as an LLDB command.
def define_command (debugger, function):
    lldb_command = function.__name__
    python_function = __name__ + "." + function.__name__
    debugger.HandleCommand(f"command script add "
                           f"--overwrite "
                           f"--function {python_function} "
                           f"{lldb_command}")

# Define Python FUNCTION as an LLDB type formatter.
def define_formatter(debugger, regex, function):
    python_function = __name__ + "." + function.__name__
    debugger.HandleCommand(f"type summary add "
                           f"--cascade true "
                           f'--regex "{regex}" '
                           f"--python-function {python_function}")

# This function is called by LLDB to initialize the module.
def __lldb_init_module(debugger, internal_dict):
    define_command(debugger, xbacktrace)
    define_command(debugger, xdebug_print)
    define_formatter(debugger, "Lisp_Object", format_Lisp_Object)
    print('Emacs debugging support has been installed.')

# end.

debug log:

solving 3a9f17e020 ...
found 3a9f17e020 in https://yhetil.org/emacs/AAB63EE4-18DD-40D7-A64E-DEB6729B60B3@gmail.com/

applying [1/1] https://yhetil.org/emacs/AAB63EE4-18DD-40D7-A64E-DEB6729B60B3@gmail.com/
diff --git a/etc/emacs_lldb.py b/etc/emacs_lldb.py
new file mode 100644
index 0000000000..3a9f17e020

Checking patch etc/emacs_lldb.py...
Applied patch etc/emacs_lldb.py cleanly.

index at:
100644 3a9f17e02029b5e9657686e0c940321f7a60489b	etc/emacs_lldb.py

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.