|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -# Copyright (C) 2009, 2013, 2015, 2018-2019 Rocky Bernstein |
| 2 | +# Copyright (C) 2009, 2013, 2015, 2018-2020 Rocky Bernstein |
3 | 3 | # |
4 | 4 | # This program is free software: you can redistribute it and/or modify |
5 | 5 | # it under the terms of the GNU General Public License as published by |
|
13 | 13 | # |
14 | 14 | # You should have received a copy of the GNU General Public License |
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | | -import os |
| 16 | + |
17 | 17 | from getopt import getopt, GetoptError |
18 | 18 |
|
19 | 19 | # Our local modules |
20 | | -from trepan.processor.command import base_cmd as Mbase_cmd |
| 20 | +from trepan.processor.command.base_cmd import DebuggerCommand |
21 | 21 | from trepan.lib import stack as Mstack |
22 | 22 |
|
23 | 23 |
|
24 | | -class BacktraceCommand(Mbase_cmd.DebuggerCommand): |
| 24 | +class BacktraceCommand(DebuggerCommand): |
25 | 25 | """**backtrace** [*options*] [*count*] |
26 | 26 |
|
27 | 27 | Print backtrace of all stack frames, or innermost *count* frames. |
@@ -57,107 +57,111 @@ class BacktraceCommand(Mbase_cmd.DebuggerCommand): |
57 | 57 | `frame`, `locals`, `global`, `deparse`, `list`. |
58 | 58 | """ |
59 | 59 |
|
60 | | - aliases = ('bt', 'where') |
61 | | - category = 'stack' |
62 | | - min_args = 0 |
63 | | - max_args = 4 |
64 | | - name = os.path.basename(__file__).split('.')[0] |
65 | | - need_stack = True |
66 | | - short_help = 'Print backtrace of stack frames' |
| 60 | + aliases = ("bt", "where") |
| 61 | + short_help = "Print backtrace of stack frames" |
| 62 | + |
| 63 | + DebuggerCommand.setup(locals(), category="stack", max_args=4, need_stack=True) |
67 | 64 |
|
68 | 65 | def run(self, args): |
69 | 66 |
|
70 | 67 | try: |
71 | | - opts, args = getopt(args[1:], "hfds", |
72 | | - "help deparse full source".split()) |
| 68 | + opts, args = getopt(args[1:], "hfds", "help deparse full source".split()) |
73 | 69 | except GetoptError as err: |
74 | 70 | # print help information and exit: |
75 | 71 | print(str(err)) # will print something like "option -a not recognized" |
76 | 72 | return |
77 | 73 |
|
78 | | - bt_opts = {'width': self.settings['width']} |
| 74 | + bt_opts = {"width": self.settings["width"]} |
79 | 75 | for o, a in opts: |
80 | 76 | if o in ("-h", "--help"): |
81 | | - self.proc.commands['help'].run(['help', 'backtrace']) |
| 77 | + self.proc.commands["help"].run(["help", "backtrace"]) |
82 | 78 | return |
83 | 79 | elif o in ("-d", "--deparse"): |
84 | | - bt_opts['deparse'] = True |
| 80 | + bt_opts["deparse"] = True |
85 | 81 | elif o in ("-f", "--full"): |
86 | | - bt_opts['full'] = True |
| 82 | + bt_opts["full"] = True |
87 | 83 | elif o in ("-s", "--source"): |
88 | | - bt_opts['source'] = True |
| 84 | + bt_opts["source"] = True |
89 | 85 | else: |
90 | 86 | self.errmsg("unhandled option '%s'" % o) |
91 | 87 | pass |
92 | 88 |
|
93 | | - |
94 | 89 | if len(args) > 0: |
95 | 90 | at_most = len(self.proc.stack) |
96 | 91 | if at_most == 0: |
97 | 92 | self.errmsg("Stack is empty.") |
98 | 93 | return False |
99 | | - min_value = - at_most + 1 |
100 | | - count = self.proc.get_int(args[0], min_value = min_value, |
101 | | - cmdname = 'backtrace', |
102 | | - default=0, at_most = at_most) |
103 | | - if count is None: return False |
| 94 | + min_value = -at_most + 1 |
| 95 | + count = self.proc.get_int( |
| 96 | + args[0], |
| 97 | + min_value=min_value, |
| 98 | + cmdname="backtrace", |
| 99 | + default=0, |
| 100 | + at_most=at_most, |
| 101 | + ) |
| 102 | + if count is None: |
| 103 | + return False |
104 | 104 | if count < 0: |
105 | | - count = at_most - count |
| 105 | + count = at_most - count |
106 | 106 | pass |
107 | | - elif 0 == count: count = None |
| 107 | + elif 0 == count: |
| 108 | + count = None |
108 | 109 | else: |
109 | 110 | count = None |
110 | 111 | pass |
111 | 112 |
|
112 | 113 | if not self.proc.curframe: |
113 | 114 | self.errmsg("No stack.") |
114 | 115 | return False |
115 | | - Mstack.print_stack_trace(self.proc, count, |
116 | | - color=self.settings['highlight'], |
117 | | - opts=bt_opts) |
| 116 | + Mstack.print_stack_trace( |
| 117 | + self.proc, count, color=self.settings["highlight"], opts=bt_opts |
| 118 | + ) |
118 | 119 | return False |
119 | 120 |
|
120 | 121 | pass |
121 | 122 |
|
122 | | -if __name__ == '__main__': |
| 123 | + |
| 124 | +if __name__ == "__main__": |
123 | 125 | from trepan.processor import cmdproc |
124 | 126 | from trepan import debugger |
125 | | - d = debugger.Debugger() |
126 | | - cp = d.core.processor |
127 | | - command = BacktraceCommand(cp) |
128 | | - command.run(['backtrace', 'wrong', 'number', 'of', 'args']) |
| 127 | + |
| 128 | + d = debugger.Debugger() |
| 129 | + cp = d.core.processor |
| 130 | + command = BacktraceCommand(cp) |
| 131 | + command.run(["backtrace", "wrong", "number", "of", "args"]) |
129 | 132 |
|
130 | 133 | def nest_me(cp, command, i): |
131 | 134 | import inspect |
| 135 | + |
132 | 136 | if i > 1: |
133 | 137 | cp.curframe = inspect.currentframe() |
134 | | - cp.stack, cp.curindex = cmdproc.get_stack(cp.curframe, None, None, |
135 | | - cp) |
136 | | - print('-' * 10) |
137 | | - command.run(['backtrace']) |
138 | | - print('-' * 10) |
139 | | - command.run(['backtrace', '1']) |
| 138 | + cp.stack, cp.curindex = cmdproc.get_stack(cp.curframe, None, None, cp) |
| 139 | + print("-" * 10) |
| 140 | + command.run(["backtrace"]) |
| 141 | + print("-" * 10) |
| 142 | + command.run(["backtrace", "1"]) |
140 | 143 | else: |
141 | | - nest_me(cp, command, i+1) |
| 144 | + nest_me(cp, command, i + 1) |
142 | 145 | return |
143 | 146 |
|
144 | 147 | def ignore_me(cp, command, i): |
145 | | - print('=' * 10) |
| 148 | + print("=" * 10) |
146 | 149 | nest_me(cp, command, 1) |
147 | | - print('=' * 10) |
| 150 | + print("=" * 10) |
148 | 151 | cp.core.add_ignore(ignore_me) |
149 | 152 | nest_me(cp, command, 1) |
150 | 153 | return |
| 154 | + |
151 | 155 | cp.forget() |
152 | | - command.run(['backtrace']) |
153 | | - print('-' * 10) |
| 156 | + command.run(["backtrace"]) |
| 157 | + print("-" * 10) |
154 | 158 | ignore_me(cp, command, 1) |
155 | | - command.run(['backtrace', '1']) |
156 | | - print('-' * 10) |
157 | | - command.run(['backtrace', '-1']) |
158 | | - print('-' * 10) |
159 | | - command.run(['backtrace', '3']) |
160 | | - print('-' * 10) |
161 | | - command.run(['backtrace', '-2']) |
162 | | - print('-' * 10) |
| 159 | + command.run(["backtrace", "1"]) |
| 160 | + print("-" * 10) |
| 161 | + command.run(["backtrace", "-1"]) |
| 162 | + print("-" * 10) |
| 163 | + command.run(["backtrace", "3"]) |
| 164 | + print("-" * 10) |
| 165 | + command.run(["backtrace", "-2"]) |
| 166 | + print("-" * 10) |
163 | 167 | pass |
0 commit comments