changeset 16842:035b5fe157ad

Add new dblist command to debugger (bug #38953) * NEWS: Add dblist to list of new functions for 3.8. * doc/interpreter/debug.txi: Add dblist to manual. * libinterp/interpfcn/debug.cc(Fdblist): New function to print lines of code surrounding current execution line. * libinterp/interpfcn/debug.cc(Fdbwhere, Fdbtype): Add @seealso links to new dblist function.
author Rik <rik@octave.org>
date Mon, 24 Jun 2013 20:06:31 -0700
parents 2ce1ddead134
children e7ee313b3ff3
files NEWS doc/interpreter/debug.txi libinterp/interpfcn/debug.cc
diffstat 3 files changed, 82 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS
+++ b/NEWS
@@ -176,11 +176,11 @@
 
  ** Other new functions added in 3.8.0:
 
-      betaincinv   ellipj    findfigs     lines          strjoin
-      cmpermute    ellipke   fminsearch   polyeig        tetramesh
-      cmunique     erfcinv   gallery      rgbplot        waterfall
-      colorcube    erfi      importdata   shrinkfaces
-      dawson       expint    iscolormap   splinefit  
+      betaincinv   dblist    expint       iscolormap    splinefit
+      cmpermute    ellipj    findfigs     lines         strjoin
+      cmunique     ellipke   fminsearch   polyeig       tetramesh
+      colorcube    erfcinv   gallery      rgbplot       waterfall
+      dawson       erfi      importdata   shrinkfaces
 
  ** Deprecated functions.
 
--- a/doc/interpreter/debug.txi
+++ b/doc/interpreter/debug.txi
@@ -158,7 +158,7 @@
 @node Debug Mode
 @section Debug Mode
 
-There are two additional support functions that allow the user to
+There are three additional support functions that allow the user to
 interrogate where in the execution of a script Octave entered the debug
 mode and to print the code in the script surrounding the point where
 Octave entered debug mode.
@@ -167,6 +167,8 @@
 
 @DOCSTRING(dbtype)
 
+@DOCSTRING(dblist)
+
 You may also use @code{isdebugmode} to determine whether the debugger is
 currently active.
 
--- a/libinterp/interpfcn/debug.cc
+++ b/libinterp/interpfcn/debug.cc
@@ -1036,6 +1036,80 @@
   return retval;
 }
 
+DEFUN (dblist, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn  {Command} {} dblist\n\
+@deftypefnx {Command} {} dblist var{n}\n\
+In debugging mode, list @var{n} lines of the function being debugged\n\
+centered around the the current line to be executed.  If unspecified @var{n}\n\
+defaults to 10 (+/- 5 lines)\n\
+@seealso{dbwhere, dbtype}\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int n = 10;
+
+  if (args.length () == 1)
+    {
+      octave_value arg = args(0);
+
+      if (arg.is_string ())
+        {
+          std::string s_arg = arg.string_value ();
+
+          n = atoi (s_arg.c_str ());
+        }
+      else
+        n = args(0).int_value ();
+
+      if (n < 0)
+        error ("dblist: N must be a non-negative integer");
+    }
+
+  octave_user_code *dbg_fcn = get_user_code ();
+
+  if (dbg_fcn)
+    {
+      bool have_file = true;
+
+      std::string name = dbg_fcn->fcn_file_name ();
+
+      if (name.empty ())
+        {
+          have_file = false;
+          name = dbg_fcn->name ();
+        }
+
+      int l = octave_call_stack::caller_user_code_line ();
+
+      if (l > 0)
+        {
+          if (have_file)
+            {
+              int l_min = std::max (l - n/2, 0);
+              int l_max = l + n/2;
+              do_dbtype (octave_stdout, dbg_fcn->name (), l_min, l-1);
+
+              std::string line = get_file_line (name, l);
+              if (! line.empty ())
+                octave_stdout << l << "-->\t" << line << std::endl;
+
+              do_dbtype (octave_stdout, dbg_fcn->name (), l+1, l_max);
+            }
+        }
+      else
+        {
+          octave_stdout << "dblist: unable to determine source code line"
+                        << std::endl;
+        }
+    }
+  else
+    error ("dblist: must be inside a user function to use dblist\n");
+
+  return retval;
+}
+
 static octave_value_list
 do_dbstack (const octave_value_list& args, int nargout, std::ostream& os)
 {