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