3805
|
1 /* |
|
2 |
|
3 Copyright (C) 2001 Ben Sapp |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3805
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3895
|
28 #include <iostream> |
|
29 #include <fstream> |
3948
|
30 #include <string> |
3895
|
31 |
3805
|
32 #include "defun.h" |
|
33 #include "error.h" |
|
34 #include "input.h" |
|
35 #include "pager.h" |
|
36 #include "oct-obj.h" |
|
37 #include "utils.h" |
|
38 #include "parse.h" |
|
39 #include "symtab.h" |
|
40 #include "gripes.h" |
|
41 #include "ov.h" |
|
42 #include "ov-usr-fcn.h" |
|
43 #include "ov-fcn.h" |
|
44 #include "pt-pr-code.h" |
|
45 #include "pt.h" |
|
46 #include "pt-bp.h" |
|
47 #include "pt-stmt.h" |
|
48 #include "toplev.h" |
|
49 #include "unwind-prot.h" |
|
50 #include "variables.h" |
|
51 |
5743
|
52 // Return a pointer to the user-defined function FNAME. If FNAME is |
|
53 // empty, search backward for the first user-defined function in the |
|
54 // current call stack. |
|
55 |
3949
|
56 static octave_user_function * |
5743
|
57 get_user_function (std::string fname = "") |
3805
|
58 { |
3956
|
59 octave_user_function *dbg_fcn = 0; |
3805
|
60 |
5743
|
61 if (fname == "") |
|
62 dbg_fcn = octave_call_stack::caller_script (); |
|
63 else |
3805
|
64 { |
5743
|
65 symbol_record *ptr = curr_sym_tab->lookup (fname); |
3946
|
66 |
3805
|
67 if (ptr && ptr->is_user_function ()) |
|
68 { |
|
69 octave_value tmp = ptr->def (); |
4346
|
70 dbg_fcn = dynamic_cast<octave_user_function *> (tmp.function_value ()); |
3805
|
71 } |
|
72 else |
|
73 { |
5743
|
74 ptr = lookup_by_name (fname, false); |
3946
|
75 |
3805
|
76 if (ptr && ptr->is_user_function ()) |
|
77 { |
|
78 octave_value tmp = ptr->def (); |
4346
|
79 dbg_fcn = dynamic_cast<octave_user_function *> (tmp.function_value ()); |
3805
|
80 } |
|
81 } |
|
82 } |
|
83 |
|
84 return dbg_fcn; |
|
85 } |
|
86 |
3895
|
87 |
4208
|
88 DEFCMD (dbstop, args, , |
3805
|
89 "-*- texinfo -*-\n\ |
3895
|
90 @deftypefn {Loadable Function} {rline =} dbstop (func, line)\n\ |
3805
|
91 Set a breakpoint in a function\n\ |
|
92 @table @code\n\ |
|
93 @item func\n\ |
|
94 String representing the function name. When already in debug\n\ |
|
95 mode this should be left out and only the line should be given.\n\ |
|
96 @item line\n\ |
|
97 Line you would like the breakpoint to be set on\n\ |
|
98 @end table\n\ |
|
99 \n\ |
|
100 The rline returned is the real line that the breakpoint was set at.\n\ |
5642
|
101 @seealso{dbclear, dbstatus, dbnext}\n\ |
|
102 @end deftypefn") |
3805
|
103 { |
|
104 octave_value retval; |
|
105 |
|
106 int result = -1; |
|
107 int nargin = args.length (); |
3946
|
108 |
3895
|
109 string_vector argv = args.make_argv ("dbstop"); |
3805
|
110 |
|
111 if (error_state) |
|
112 return retval; |
|
113 |
|
114 if (nargin == 2) |
3946
|
115 { |
3805
|
116 std::string symbol_name = argv[1]; |
|
117 |
|
118 std::string line_number = argv[2]; |
|
119 |
|
120 int line = atoi (line_number.c_str ()); |
3946
|
121 |
3805
|
122 octave_user_function *dbg_fcn = get_user_function (symbol_name); |
|
123 |
|
124 if (dbg_fcn) |
|
125 { |
|
126 tree_statement_list *cmds = dbg_fcn->body (); |
|
127 result = cmds->set_breakpoint (line); |
|
128 } |
|
129 else |
3948
|
130 error ("dbstop: unable to find the function requested\n"); |
3805
|
131 } |
|
132 else if (nargin == 1) |
|
133 { |
|
134 std::string line_number = argv[1]; |
|
135 |
|
136 int line = atoi (line_number.c_str ()); |
|
137 |
|
138 octave_user_function *dbg_fcn = get_user_function (); |
3946
|
139 |
3805
|
140 if (dbg_fcn) |
|
141 { |
|
142 tree_statement_list *cmds = dbg_fcn->body (); |
|
143 result = cmds->set_breakpoint (line); |
|
144 } |
|
145 else |
3948
|
146 error ("dbstop: unable to find the function requested\n"); |
3805
|
147 } |
|
148 else |
3948
|
149 error ("dbstop: one argument when in a function and two when not\n"); |
3805
|
150 |
4233
|
151 retval = result; |
3805
|
152 |
|
153 return retval; |
|
154 } |
|
155 |
4208
|
156 DEFCMD (dbclear, args, , |
3805
|
157 "-*- texinfo -*-\n\ |
3895
|
158 @deftypefn {Loadable Function} {} dbclear (func, line)\n\ |
3805
|
159 Delete a breakpoint in a function\n\ |
|
160 @table @code\n\ |
|
161 @item func\n\ |
|
162 String representing the function name. When already in debug\n\ |
|
163 mode this should be left out and only the line should be given.\n\ |
|
164 @item line\n\ |
|
165 Line where you would like to remove the the breakpoint\n\ |
|
166 @end table\n\ |
|
167 No checking is done to make sure that the line you requested is really\n\ |
|
168 a breakpoint. If you get the wrong line nothing will happen.\n\ |
5642
|
169 @seealso{dbstop, dbstatus, dbwhere}\n\ |
|
170 @end deftypefn") |
3805
|
171 { |
|
172 octave_value retval; |
|
173 |
|
174 std::string symbol_name = ""; |
3895
|
175 std::string line_number; |
3949
|
176 |
3805
|
177 int line = -1; |
|
178 int nargin = args.length (); |
3946
|
179 |
3895
|
180 string_vector argv = args.make_argv ("dbclear"); |
3805
|
181 |
|
182 if (error_state) |
|
183 return retval; |
|
184 |
3948
|
185 if (nargin == 1 || nargin == 2) |
3805
|
186 { |
3948
|
187 if (nargin == 2) |
|
188 { |
|
189 symbol_name = argv[1]; |
|
190 |
|
191 octave_stdout << argv[1] << std::endl; |
|
192 line_number = argv[2]; |
|
193 } |
|
194 else if (nargin == 1) |
|
195 { |
|
196 line_number = argv[1]; |
|
197 } |
3946
|
198 |
3948
|
199 if (line_number.compare ("all") && line_number.compare ("ALL")) |
|
200 line = atoi (line_number.c_str ()); |
|
201 else |
|
202 line = -1; |
|
203 |
|
204 octave_stdout << "symbol_name = " << symbol_name << std::endl; |
|
205 octave_user_function *dbg_fcn = get_user_function (symbol_name); |
3805
|
206 |
3948
|
207 if (dbg_fcn) |
|
208 { |
|
209 tree_statement_list *cmds = dbg_fcn->body (); |
|
210 cmds->delete_breakpoint (line); |
|
211 } |
|
212 else |
|
213 error ("dbclear: unable to find the function requested\n"); |
3805
|
214 } |
|
215 else |
3948
|
216 error ("dbclear: expecting one or two arguements\n"); |
3805
|
217 |
|
218 return retval; |
|
219 } |
|
220 |
4208
|
221 DEFCMD (dbstatus, args, , |
3805
|
222 "-*- texinfo -*-\n\ |
3895
|
223 @deftypefn {Loadable Function} {lst =} dbstatus ([func])\n\ |
3805
|
224 Return a vector containing the lines on which a function has \n\ |
|
225 breakpoints set.\n\ |
|
226 @table @code\n\ |
|
227 @item func\n\ |
|
228 String representing the function name. When already in debug\n\ |
|
229 mode this should be left out.\n\ |
|
230 @end table\n\ |
5642
|
231 @seealso{dbclear, dbwhere}\n\ |
|
232 @end deftypefn") |
3805
|
233 { |
|
234 octave_value retval; |
|
235 |
|
236 int nargin = args.length (); |
|
237 |
|
238 if (nargin != 0 && nargin != 1) |
|
239 { |
3948
|
240 error ("dbstatus: only zero or one arguements accepted\n"); |
3805
|
241 return retval; |
|
242 } |
|
243 |
|
244 std::string symbol_name = ""; |
|
245 |
|
246 if (nargin == 1) |
|
247 { |
|
248 if (args(0).is_string ()) |
|
249 symbol_name = args(0).string_value (); |
|
250 else |
3895
|
251 gripe_wrong_type_arg ("dbstatus", args(0)); |
3805
|
252 } |
|
253 |
|
254 octave_user_function *dbg_fcn = get_user_function (symbol_name); |
3946
|
255 |
3805
|
256 if (dbg_fcn) |
|
257 { |
|
258 tree_statement_list *cmds = dbg_fcn->body (); |
|
259 |
|
260 octave_value_list lst = cmds->list_breakpoints (); |
|
261 |
|
262 RowVector vec (lst.length (), 0.0); |
|
263 |
|
264 for (int i = 0; i < lst.length (); i++) |
3946
|
265 { |
3805
|
266 vec(i) = lst(i).double_value (); |
|
267 |
|
268 if (error_state) |
|
269 panic_impossible (); |
|
270 } |
|
271 |
|
272 retval = octave_value (vec); |
|
273 } |
|
274 else |
3948
|
275 error ("dbstatus: unable to find the function you requested\n"); |
3805
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
4208
|
280 DEFCMD (dbwhere, , , |
3805
|
281 "-*- texinfo -*-\n\ |
3895
|
282 @deftypefn {Loadable Function} {} dbwhere ()\n\ |
3805
|
283 Show where we are in the code\n\ |
5642
|
284 @seealso{dbclear, dbstatus, dbstop}\n\ |
5645
|
285 @end deftypefn") |
3805
|
286 { |
|
287 octave_value retval; |
3946
|
288 |
5743
|
289 octave_user_function *dbg_fcn = get_user_function (); |
3805
|
290 |
|
291 if (dbg_fcn) |
|
292 { |
4748
|
293 std::string name = dbg_fcn->name (); |
3805
|
294 |
|
295 octave_stdout << name << ":"; |
|
296 |
|
297 const tree *dbg_stmt = tree::break_statement; |
|
298 |
|
299 if (dbg_stmt) |
|
300 { |
3946
|
301 octave_stdout << "line " << dbg_stmt->line () << ", "; |
3805
|
302 octave_stdout << "column " << dbg_stmt->column () << std::endl; |
|
303 } |
|
304 else |
|
305 octave_stdout << "-1\n"; |
|
306 } |
|
307 else |
3948
|
308 error ("dbwhere: must be inside of a user function to use dbwhere\n"); |
3895
|
309 |
|
310 return retval; |
|
311 } |
|
312 |
|
313 // Copied and modified from the do_type command in help.cc |
3946
|
314 // Maybe we could share some code? |
|
315 void |
3949
|
316 do_dbtype (std::ostream& os, const std::string& name, int start, int end) |
3895
|
317 { |
|
318 std::string ff = fcn_file_in_path (name); |
|
319 |
|
320 if (! ff.empty ()) |
|
321 { |
|
322 std::ifstream fs (ff.c_str (), std::ios::in); |
3946
|
323 |
3895
|
324 if (fs) |
3946
|
325 { |
3895
|
326 char ch; |
|
327 int line = 1; |
3946
|
328 |
3895
|
329 if (line >= start && line <= end) |
|
330 os << line << "\t"; |
3946
|
331 |
3895
|
332 while (fs.get (ch)) |
|
333 { |
|
334 if (line >= start && line <= end) |
|
335 { |
|
336 os << ch; |
|
337 } |
|
338 |
|
339 if (ch == '\n') |
|
340 { |
|
341 line++; |
|
342 if (line >= start && line <= end) |
3946
|
343 os << line << "\t"; |
3895
|
344 } |
|
345 } |
|
346 } |
3946
|
347 else |
3949
|
348 os << "dbtype: unable to open `" << ff << "' for reading!\n"; |
3895
|
349 } |
|
350 else |
3949
|
351 os << "dbtype: unkown function"; |
3895
|
352 |
|
353 } |
|
354 |
4208
|
355 DEFCMD (dbtype, args, , |
3895
|
356 "-*- texinfo -*-\n\ |
|
357 @deftypefn {Loadable Function} {} dbtype ()\n\ |
|
358 List script file with line numbers.\n\ |
5642
|
359 @seealso{dbclear, dbstatus, dbstop}\n\ |
|
360 @end deftypefn") |
3895
|
361 { |
|
362 octave_value retval; |
|
363 octave_user_function *dbg_fcn; |
3946
|
364 |
3895
|
365 int nargin = args.length (); |
|
366 string_vector argv = args.make_argv ("dbtype"); |
3946
|
367 |
3895
|
368 if (! error_state) |
|
369 { |
|
370 switch (nargin) |
|
371 { |
|
372 case 0: // dbtype |
|
373 dbg_fcn = get_user_function (); |
|
374 |
|
375 if (dbg_fcn) |
4748
|
376 do_dbtype (octave_stdout, dbg_fcn->name (), 0, INT_MAX); |
3895
|
377 else |
3948
|
378 error ("dbtype: must be in a user function to give no arguments to dbtype\n"); |
3946
|
379 break; |
3895
|
380 |
3946
|
381 case 1: // (dbtype func) || (dbtype start:end) |
3948
|
382 dbg_fcn = get_user_function (argv[1]); |
3895
|
383 |
|
384 if (dbg_fcn) |
4748
|
385 do_dbtype (octave_stdout, dbg_fcn->name (), 0, INT_MAX); |
3895
|
386 else |
|
387 { |
|
388 dbg_fcn = get_user_function (""); |
|
389 |
|
390 if (dbg_fcn) |
|
391 { |
3948
|
392 std::string arg = argv[1]; |
|
393 |
|
394 size_t ind = arg.find (':'); |
3895
|
395 |
3948
|
396 if (ind != NPOS) |
|
397 { |
|
398 std::string start_str = arg.substr (0, ind); |
|
399 std::string end_str = arg.substr (ind + 1); |
|
400 |
|
401 int start = atoi (start_str.c_str ()); |
|
402 int end = atoi (end_str.c_str ()); |
3946
|
403 |
3948
|
404 if (start < end) |
|
405 do_dbtype (octave_stdout, |
4748
|
406 dbg_fcn->name (), start, end); |
3948
|
407 else |
|
408 error ("dbtype: the start line must be less than the end line\n"); |
3895
|
409 } |
3948
|
410 else |
|
411 error ("dbtype: if you specify lines it must be like `start:end`"); |
3895
|
412 } |
|
413 } |
|
414 break; |
3946
|
415 |
|
416 case 2: // (dbtype func start:end) |
3948
|
417 dbg_fcn = get_user_function (argv[1]); |
3895
|
418 |
|
419 if (dbg_fcn) |
|
420 { |
3948
|
421 std::string arg = argv[2]; |
3895
|
422 |
3948
|
423 size_t ind = arg.find (':'); |
3895
|
424 |
3948
|
425 if (ind != NPOS) |
3895
|
426 { |
3948
|
427 std::string start_str = arg.substr (0, ind); |
|
428 std::string end_str = arg.substr (ind + 1); |
3895
|
429 |
3948
|
430 int start = atoi (start_str.c_str ()); |
|
431 int end = atoi (end_str.c_str ()); |
|
432 |
|
433 if (start < end) |
|
434 do_dbtype (octave_stdout, |
4748
|
435 dbg_fcn->name (), start, end); |
3948
|
436 else |
|
437 error ("dbtype: the start line must be less than the end line\n"); |
|
438 } |
|
439 else |
|
440 error ("dbtype: if you specify lines it must be like `start:end`"); |
3895
|
441 } |
3946
|
442 break; |
3895
|
443 |
|
444 default: |
3948
|
445 error ("dbtype: expecting zero, one, or two arguments\n"); |
3895
|
446 } |
|
447 } |
3805
|
448 |
|
449 return retval; |
|
450 } |
|
451 |
|
452 /* |
|
453 ;;; Local Variables: *** |
|
454 ;;; mode: C++ *** |
|
455 ;;; End: *** |
|
456 */ |