GDB (xrefs)
Loading...
Searching...
No Matches
scopes.py
Go to the documentation of this file.
1# Copyright 2022-2023 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16import gdb
17
18from .frames import frame_for_id
19from .startup import in_gdb_thread
20from .server import request
21from .varref import BaseReference
22
23
24# Map DAP frame IDs to scopes. This ensures that scopes are re-used.
25frame_to_scope = {}
26
27
28# When the inferior is re-started, we erase all scope references. See
29# the section "Lifetime of Objects References" in the spec.
30@in_gdb_thread
31def clear_scopes(event):
32 global frame_to_scope
33 frame_to_scope = {}
34
35
36gdb.events.cont.connect(clear_scopes)
37
38
39# A helper function to compute the value of a symbol. SYM is either a
40# gdb.Symbol, or an object implementing the SymValueWrapper interface.
41# FRAME is a frame wrapper, as produced by a frame filter. Returns a
42# tuple of the form (NAME, VALUE), where NAME is the symbol's name and
43# VALUE is a gdb.Value.
44@in_gdb_thread
45def symbol_value(sym, frame):
46 inf_frame = frame.inferior_frame()
47 # Make sure to select the frame first. Ideally this would not
48 # be needed, but this is a way to set the current language
49 # properly so that language-dependent APIs will work.
50 inf_frame.select()
51 name = str(sym.symbol())
52 val = sym.value()
53 if val is None:
54 # No synthetic value, so must read the symbol value
55 # ourselves.
56 val = sym.symbol().value(inf_frame)
57 elif not isinstance(val, gdb.Value):
58 val = gdb.Value(val)
59 return (name, val)
60
61
63 def __init__(self, name, hint, frame, var_list):
64 super().__init__(name)
65 self.hint = hint
66 self.frame = frame
67 self.inf_frame = frame.inferior_frame()
68 self.func = frame.function()
69 self.line = frame.line()
70 # VAR_LIST might be any kind of iterator, but it's convenient
71 # here if it is just a collection.
72 self.var_list = tuple(var_list)
73
74 def to_object(self):
75 result = super().to_object()
76 result["presentationHint"] = self.hint
77 # How would we know?
78 result["expensive"] = False
79 result["namedVariables"] = len(self.var_list)
80 if self.line is not None:
81 result["line"] = self.line
82 # FIXME construct a Source object
83 return result
84
85 def has_children(self):
86 return True
87
88 def child_count(self):
89 return len(self.var_list)
90
91 @in_gdb_thread
92 def fetch_one_child(self, idx):
93 return symbol_value(self.var_list[idx], self.frame)
94
95
97 def __init__(self, name, frame):
98 super().__init__(
99 name, "registers", frame, frame.inferior_frame().architecture().registers()
100 )
101
102 @in_gdb_thread
103 def fetch_one_child(self, idx):
104 return (
105 self.var_list[idx].name,
106 self.inf_frame.read_register(self.var_list[idx]),
107 )
108
109
110# Helper function to create a DAP scopes for a given frame ID.
111@in_gdb_thread
112def _get_scope(id):
113 global frame_to_scope
114 if id in frame_to_scope:
115 scopes = frame_to_scope[id]
116 else:
117 frame = frame_for_id(id)
118 scopes = []
119 # Make sure to handle the None case as well as the empty
120 # iterator case.
121 args = tuple(frame.frame_args() or ())
122 if args:
123 scopes.append(_ScopeReference("Arguments", "arguments", frame, args))
124 # Make sure to handle the None case as well as the empty
125 # iterator case.
126 locs = tuple(frame.frame_locals() or ())
127 if locs:
128 scopes.append(_ScopeReference("Locals", "locals", frame, locs))
129 scopes.append(_RegisterReference("Registers", frame))
130 frame_to_scope[id] = scopes
131 return [x.to_object() for x in scopes]
132
133
134@request("scopes")
135def scopes(*, frameId: int, **extra):
136 return {"scopes": _get_scope(frameId)}
__init__(self, name, frame)
Definition scopes.py:97
__init__(self, name, hint, frame, var_list)
Definition scopes.py:63
clear_scopes(event)
Definition scopes.py:31
symbol_value(sym, frame)
Definition scopes.py:45
Definition value.h:130