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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3895
|
27 #include <iostream> |
|
28 #include <fstream> |
|
29 #include <strstream> |
|
30 #include <string> |
|
31 #include <stdlib.h> |
|
32 |
3805
|
33 #include "defun.h" |
|
34 #include "error.h" |
|
35 #include "input.h" |
|
36 #include "pager.h" |
|
37 #include "oct-obj.h" |
|
38 #include "utils.h" |
|
39 #include "parse.h" |
|
40 #include "symtab.h" |
|
41 #include "gripes.h" |
|
42 #include "ov.h" |
|
43 #include "ov-usr-fcn.h" |
|
44 #include "ov-fcn.h" |
|
45 #include "pt-pr-code.h" |
|
46 #include "pt.h" |
|
47 #include "pt-bp.h" |
|
48 #include "pt-stmt.h" |
|
49 #include "toplev.h" |
|
50 #include "unwind-prot.h" |
|
51 #include "variables.h" |
|
52 |
|
53 octave_user_function * |
|
54 get_user_function (std::string str = "") |
|
55 { |
|
56 octave_user_function *dbg_fcn = NULL; |
|
57 |
3895
|
58 if (str.compare ("")) |
3805
|
59 { |
|
60 symbol_record *ptr = curr_sym_tab->lookup (str); |
|
61 |
|
62 if (ptr && ptr->is_user_function ()) |
|
63 { |
|
64 octave_value tmp = ptr->def (); |
|
65 dbg_fcn = static_cast<octave_user_function *> (tmp.function_value ()); |
|
66 } |
|
67 else |
|
68 { |
|
69 symbol_record *ptr = lookup_by_name (str, false); |
|
70 |
|
71 if (ptr && ptr->is_user_function ()) |
|
72 { |
|
73 octave_value tmp = ptr->def (); |
|
74 dbg_fcn = static_cast<octave_user_function *> (tmp.function_value ()); |
|
75 } |
|
76 } |
|
77 } |
3895
|
78 else if (curr_function) |
|
79 { |
|
80 dbg_fcn = curr_function; |
|
81 } |
3805
|
82 |
|
83 return dbg_fcn; |
|
84 } |
|
85 |
3895
|
86 |
|
87 DEFUN_TEXT (dbstop, args, , |
3805
|
88 "-*- texinfo -*-\n\ |
3895
|
89 @deftypefn {Loadable Function} {rline =} dbstop (func, line)\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\ |
|
96 Line you would like the breakpoint to be set on\n\ |
|
97 @end table\n\ |
|
98 \n\ |
|
99 The rline returned is the real line that the breakpoint was set at.\n\ |
|
100 \n\ |
|
101 @end deftypefn\n\ |
3895
|
102 @seealso{dbclear, dbtatus, dbnext}") |
3805
|
103 { |
|
104 octave_value retval; |
|
105 |
|
106 int result = -1; |
|
107 int nargin = args.length (); |
|
108 |
3895
|
109 string_vector argv = args.make_argv ("dbstop"); |
3805
|
110 |
|
111 if (error_state) |
|
112 return retval; |
|
113 |
|
114 if (nargin == 2) |
|
115 { |
|
116 std::string symbol_name = argv[1]; |
|
117 |
|
118 std::string line_number = argv[2]; |
|
119 |
|
120 int line = atoi (line_number.c_str ()); |
|
121 |
|
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 |
|
130 error ("unable to find the function requested\n"); |
|
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 (); |
|
139 |
|
140 if (dbg_fcn) |
|
141 { |
|
142 tree_statement_list *cmds = dbg_fcn->body (); |
|
143 result = cmds->set_breakpoint (line); |
|
144 } |
|
145 else |
|
146 error ("unable to find the function requested\n"); |
|
147 } |
|
148 else |
|
149 error ("one argument when in a function and two when not\n"); |
|
150 |
|
151 retval = static_cast<double> (result); |
|
152 |
|
153 return retval; |
|
154 } |
|
155 |
3895
|
156 DEFUN_TEXT (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\ |
|
169 @end deftypefn\n\ |
3895
|
170 @seealso{dbstop, dbstatus, dbwhere}") |
3805
|
171 { |
|
172 octave_value retval; |
|
173 |
|
174 std::string symbol_name = ""; |
3895
|
175 std::string line_number; |
3805
|
176 int line = -1; |
|
177 int nargin = args.length (); |
|
178 |
|
179 if (nargin != 1 && nargin != 2) |
|
180 { |
|
181 error ("need one or two arguements\n"); |
|
182 return retval; |
|
183 } |
|
184 |
3895
|
185 string_vector argv = args.make_argv ("dbclear"); |
3805
|
186 |
|
187 if (error_state) |
|
188 return retval; |
|
189 |
|
190 if (nargin == 2) |
|
191 { |
|
192 octave_stdout << "2 input arguments\n"; |
|
193 symbol_name = argv[1]; |
|
194 |
|
195 octave_stdout << argv[1] << std::endl; |
3895
|
196 line_number = argv[2]; |
3805
|
197 |
|
198 } |
|
199 else if (nargin == 1) |
|
200 { |
|
201 octave_stdout << "1 input argument\n"; |
3895
|
202 line_number = argv[1]; |
3805
|
203 } |
|
204 else |
|
205 { |
|
206 error ("need one or two arguements\n"); |
|
207 return retval; |
|
208 } |
|
209 |
3895
|
210 |
|
211 if (line_number.compare("all") && line_number.compare("ALL")) |
|
212 line = atoi (line_number.c_str ()); |
|
213 else |
|
214 line = -1; |
|
215 |
3805
|
216 octave_stdout << "symbol_name = " << symbol_name << std::endl; |
|
217 octave_user_function *dbg_fcn = get_user_function (symbol_name); |
|
218 |
|
219 if (dbg_fcn) |
|
220 { |
|
221 tree_statement_list *cmds = dbg_fcn->body (); |
|
222 cmds->delete_breakpoint (line); |
|
223 } |
|
224 else |
|
225 error ("unable to find the function requested\n"); |
|
226 |
|
227 return retval; |
|
228 } |
|
229 |
3895
|
230 DEFUN_TEXT (dbstatus, args, , |
3805
|
231 "-*- texinfo -*-\n\ |
3895
|
232 @deftypefn {Loadable Function} {lst =} dbstatus ([func])\n\ |
3805
|
233 Return a vector containing the lines on which a function has \n\ |
|
234 breakpoints set.\n\ |
|
235 @table @code\n\ |
|
236 @item func\n\ |
|
237 String representing the function name. When already in debug\n\ |
|
238 mode this should be left out.\n\ |
|
239 @end table\n\ |
|
240 @end deftypefn\n\ |
3895
|
241 @seealso{dbclear, dbwhere}") |
3805
|
242 { |
|
243 octave_value retval; |
|
244 |
|
245 int nargin = args.length (); |
|
246 |
|
247 if (nargin != 0 && nargin != 1) |
|
248 { |
|
249 error ("only zero or one arguements accepted\n"); |
|
250 return retval; |
|
251 } |
|
252 |
|
253 std::string symbol_name = ""; |
|
254 |
|
255 if (nargin == 1) |
|
256 { |
|
257 if (args(0).is_string ()) |
|
258 symbol_name = args(0).string_value (); |
|
259 else |
3895
|
260 gripe_wrong_type_arg ("dbstatus", args(0)); |
3805
|
261 } |
|
262 |
|
263 octave_user_function *dbg_fcn = get_user_function (symbol_name); |
|
264 |
|
265 if (dbg_fcn) |
|
266 { |
|
267 tree_statement_list *cmds = dbg_fcn->body (); |
|
268 |
|
269 octave_value_list lst = cmds->list_breakpoints (); |
|
270 |
|
271 RowVector vec (lst.length (), 0.0); |
|
272 |
|
273 for (int i = 0; i < lst.length (); i++) |
|
274 { |
|
275 vec(i) = lst(i).double_value (); |
|
276 |
|
277 if (error_state) |
|
278 panic_impossible (); |
|
279 } |
|
280 |
|
281 retval = octave_value (vec); |
|
282 } |
|
283 else |
|
284 error ("unable to find the function you requested\n"); |
|
285 |
|
286 return retval; |
|
287 } |
|
288 |
3895
|
289 DEFUN_TEXT (dbwhere, , , |
3805
|
290 "-*- texinfo -*-\n\ |
3895
|
291 @deftypefn {Loadable Function} {} dbwhere ()\n\ |
3805
|
292 Show where we are in the code\n\ |
|
293 @end deftypefn\n\ |
3895
|
294 @seealso{dbclear, dbstatus, dbstop}") |
3805
|
295 { |
|
296 octave_value retval; |
|
297 |
|
298 octave_user_function *dbg_fcn = curr_function; |
|
299 |
|
300 if (dbg_fcn) |
|
301 { |
|
302 std::string name = dbg_fcn->function_name (); |
|
303 |
|
304 octave_stdout << name << ":"; |
|
305 |
|
306 const tree *dbg_stmt = tree::break_statement; |
|
307 |
|
308 if (dbg_stmt) |
|
309 { |
|
310 octave_stdout << "line " << dbg_stmt->line () << ", "; |
|
311 octave_stdout << "column " << dbg_stmt->column () << std::endl; |
|
312 } |
|
313 else |
|
314 octave_stdout << "-1\n"; |
|
315 } |
|
316 else |
3895
|
317 error ("must be inside of a user function to use dbwhere\n"); |
|
318 |
|
319 return retval; |
|
320 } |
|
321 |
|
322 // Copied and modified from the do_type command in help.cc |
|
323 // Maybe we could share some code? |
|
324 void |
|
325 do_dbtype(std::ostream& os, const std::string& name, int start, int end) |
|
326 { |
|
327 std::string ff = fcn_file_in_path (name); |
|
328 |
|
329 if (! ff.empty ()) |
|
330 { |
|
331 std::ifstream fs (ff.c_str (), std::ios::in); |
|
332 |
|
333 if (fs) |
|
334 { |
|
335 char ch; |
|
336 int line = 1; |
|
337 |
|
338 if (line >= start && line <= end) |
|
339 os << line << "\t"; |
|
340 |
|
341 while (fs.get (ch)) |
|
342 { |
|
343 if (line >= start && line <= end) |
|
344 { |
|
345 os << ch; |
|
346 } |
|
347 |
|
348 if (ch == '\n') |
|
349 { |
|
350 line++; |
|
351 if (line >= start && line <= end) |
|
352 os << line << "\t"; |
|
353 } |
|
354 } |
|
355 } |
|
356 else |
|
357 os << "unable to open `" << ff << "' for reading!\n"; |
|
358 } |
|
359 else |
|
360 os << "unkown function"; |
|
361 |
|
362 } |
|
363 |
|
364 DEFUN_TEXT (dbtype, args, , |
|
365 "-*- texinfo -*-\n\ |
|
366 @deftypefn {Loadable Function} {} dbtype ()\n\ |
|
367 List script file with line numbers.\n\ |
|
368 @end deftypefn\n\ |
|
369 @seealso{dbclear, dbstatus, dbstop}") |
|
370 { |
|
371 octave_value retval; |
|
372 octave_user_function *dbg_fcn; |
|
373 |
|
374 int nargin = args.length (); |
|
375 string_vector argv = args.make_argv ("dbtype"); |
|
376 |
|
377 if (! error_state) |
|
378 { |
|
379 switch (nargin) |
|
380 { |
|
381 case 0: // dbtype |
|
382 dbg_fcn = get_user_function (); |
|
383 |
|
384 if (dbg_fcn) |
|
385 do_dbtype(octave_stdout,dbg_fcn->function_name (), 0, INT_MAX); |
|
386 else |
|
387 error("must be in a user function to give no arguments to dbtype\n"); |
|
388 |
|
389 break; |
|
390 case 1: // (dbtype func) || (dbtype start:end) |
|
391 dbg_fcn = get_user_function (argv[1].c_str ()); |
|
392 |
|
393 if (dbg_fcn) |
|
394 do_dbtype(octave_stdout,dbg_fcn->function_name (), 0, INT_MAX); |
|
395 else |
|
396 { |
|
397 dbg_fcn = get_user_function (""); |
|
398 |
|
399 if (dbg_fcn) |
|
400 { |
|
401 char *str = (char *)malloc(strlen(argv[1].c_str ()) + 1); |
|
402 |
|
403 if (str) |
|
404 memcpy(str, argv[1].c_str (), strlen(argv[1].c_str ()) + 1); |
|
405 else |
|
406 error("croaked\n"); |
|
407 |
|
408 char *ind = index(str,':'); |
|
409 |
|
410 if (ind) |
|
411 *ind = '\0'; |
|
412 else |
|
413 { |
|
414 free(str); |
|
415 error("if you specify lines it must be like `start:end`"); |
|
416 } |
|
417 ind++; |
|
418 |
|
419 int start = atoi(str); |
|
420 int end = atoi(ind); |
|
421 |
|
422 free(str); |
|
423 str = NULL; |
|
424 ind = NULL; |
|
425 |
|
426 octave_stdout << "got start and end\n"; |
|
427 |
|
428 if (start > end) |
|
429 error("the start line must be less than the end line\n"); |
|
430 |
|
431 octave_stdout << "doing dbtype\n"; |
|
432 do_dbtype(octave_stdout, dbg_fcn->function_name (), start, end); |
|
433 octave_stdout << "did dbtype\n"; |
|
434 } |
|
435 } |
|
436 break; |
|
437 case 2: // (dbtype func start:end) |
|
438 dbg_fcn = get_user_function (argv[1].c_str ()); |
|
439 |
|
440 if (dbg_fcn) |
|
441 { |
|
442 |
|
443 char *str = (char *)malloc(strlen(argv[2].c_str ()) + 1); |
|
444 |
|
445 if (str) |
|
446 memcpy(str, argv[2].c_str (), strlen(argv[2].c_str ()) + 1); |
|
447 else |
|
448 error("not enough memory\n"); |
|
449 |
|
450 |
|
451 char *ind = index(str,':'); |
|
452 |
|
453 if (ind) |
|
454 *ind = '\0'; |
|
455 else |
|
456 { |
|
457 free(str); |
|
458 error("if you specify lines it must be like `start:end`"); |
|
459 } |
|
460 ind++; |
|
461 |
|
462 int start = atoi(str); |
|
463 int end = atoi(ind); |
|
464 |
|
465 free(str); |
|
466 ind = NULL; |
|
467 str = NULL; |
|
468 |
|
469 if (start > end) |
|
470 error("the start line must be less than the end line\n"); |
|
471 |
|
472 do_dbtype(octave_stdout, dbg_fcn->function_name (), start, end); |
|
473 } |
|
474 |
|
475 break; |
|
476 default: |
|
477 error("unacceptable number of arguments\n"); |
|
478 } |
|
479 } |
3805
|
480 |
|
481 return retval; |
|
482 } |
|
483 |
|
484 /* |
|
485 ;;; Local Variables: *** |
|
486 ;;; mode: C++ *** |
|
487 ;;; End: *** |
|
488 */ |