View Issue Details

IDProjectCategoryView StatusLast Update
0000573LDMud 3.3Efunspublic2008-12-14 15:13
Reporterzesstra Assigned Tozesstra  
PrioritylowSeverityfeatureReproducibilityN/A
Status resolvedResolutionfixed 
Fixed in Version3.3.718 
Summary0000573: Efun returning the current depth of the control stack
DescriptionI would like to discuss an efun (or extension of debug_info) which just returns the current (control) stack depth.
We have some recursive functions in the lib where it would be nice to detect if it approaches the maximum recursion depth (__MAX_RECURSION__). That way it could terminate in a controlled way or maybe event switch to a different (non-recursive) strategy.
A possible alternative may be to catch() the recursive calls and react on errors, but I don't like that very much because then it is only one level below the limit, and the return value has to be parsed in order to find out the type of error. Additionally it might spam the error log (or one misses other errors in the recursive function).
TagsNo tags attached.
Attached Files
stack_depth.diff (1,835 bytes)   
Index: func_spec
===================================================================
--- func_spec	(Revision 2411)
+++ func_spec	(Arbeitskopie)
@@ -487,6 +487,7 @@
 
 object *caller_stack(int default: F_CONST0);
 int     caller_stack_depth();
+int     stack_depth();
 int     call_resolved(mixed &, null|object|string, string, ...);
 int     call_direct_resolved(mixed &, null|object|string, string, ...);
 object  previous_object(int);
Index: interpret.h
===================================================================
--- interpret.h	(Revision 2433)
+++ interpret.h	(Arbeitskopie)
@@ -214,6 +214,7 @@
 extern svalue_t *v_funcall (svalue_t *sp, int num_arg);
 extern svalue_t *v_call_direct_resolved (svalue_t *sp, int num_arg);
 extern svalue_t *v_call_resolved (svalue_t *sp, int num_arg);
+extern svalue_t *f_stack_depth (svalue_t *sp);
 extern svalue_t *f_caller_stack_depth (svalue_t *sp);
 extern svalue_t *f_caller_stack (svalue_t *sp);
 extern svalue_t *f_get_eval_cost (svalue_t *sp);
Index: interpret.c
===================================================================
--- interpret.c	(Revision 2433)
+++ interpret.c	(Arbeitskopie)
@@ -19537,6 +19537,24 @@
 #endif /* TRACE_CODE */
 
 /*-------------------------------------------------------------------------*/
+svalue_t *
+f_stack_depth (svalue_t *sp)
+/* EFUN stack_depth()
+ *
+ *   int stack_depth(void)
+ *
+ * Returns the number of frames on the control stack. Can be used to estimate
+ * the still available stack depth in recursive code.
+ */
+
+{
+    push_number(sp, (p_int)(csp - CONTROL_STACK) + 1);
+    
+    return sp;
+} /* f_stack_depth() */
+
+
+/*-------------------------------------------------------------------------*/
 static INLINE int
 caller_stack_depth(void)
 /* static helper function for f_caller_stack_depth() and f_caller_stack() for
stack_depth.diff (1,835 bytes)   
stack_depth-V2.diff (2,177 bytes)   
Index: src/efuns.c
===================================================================
--- src/efuns.c	(Revision 2441)
+++ src/efuns.c	(Arbeitskopie)
@@ -8376,6 +8376,10 @@
             put_string(&res, new_mstring(sbuf.buf));
             strbuf_free(&sbuf);
         }
+        else if (sp->u.number == DIT_CURRENT_DEPTH)
+        {
+            put_number(&res, control_stack_depth());
+        }
         else
             errorf("bad arg 2 to debug_info(): %"PRIdPINT", expected 0..2\n"
                  , sp->u.number);
Index: src/interpret.c
===================================================================
--- src/interpret.c	(Revision 2441)
+++ src/interpret.c	(Arbeitskopie)
@@ -19537,6 +19537,15 @@
 #endif /* TRACE_CODE */
 
 /*-------------------------------------------------------------------------*/
+int control_stack_depth (void)
+  /* Returns the number of frames on the control stack. Can be used to estimate
+   * the still available stack depth in recursive code.
+   */
+{
+    return (csp - CONTROL_STACK) + 1; 
+} /* control_stack_depth() */
+
+/*-------------------------------------------------------------------------*/
 static INLINE int
 caller_stack_depth(void)
 /* static helper function for f_caller_stack_depth() and f_caller_stack() for
Index: src/interpret.h
===================================================================
--- src/interpret.h	(Revision 2441)
+++ src/interpret.h	(Arbeitskopie)
@@ -246,5 +246,6 @@
 extern void count_interpreter_refs(void);
 #endif
 
+extern int  stack_depth(void);
 
 #endif /* INTERPRET_H__ */
Index: mudlib/sys/debug_info.h
===================================================================
--- mudlib/sys/debug_info.h	(Revision 2441)
+++ mudlib/sys/debug_info.h	(Arbeitskopie)
@@ -25,6 +25,7 @@
 #define DIT_ERROR          1  /* Return the last error call chain as an array */
 #define DIT_UNCAUGHT_ERROR 2  /* Return the last uncaught error call chain */
 #define DIT_STR_CURRENT    3  /* Return the current call chain as a string */
+#define DIT_CURRENT_DEPTH  4  /* Return the current control stack depth */
 
 /* Indices into the array resulting from debug_info(DINFO_DATA, DID_STATUS)
  */
stack_depth-V2.diff (2,177 bytes)   

Activities

zesstra

2008-10-05 10:56

administrator   ~0000804

The attached patch introduces an efun stack_depth(), which just returns the number of frames used on the control stack.
Please comment, especially if you would prefer to have the functionality in debug_info() (or not at all).

zesstra

2008-12-12 16:28

administrator   ~0000814

We just had a discussion on -d-code about this and agreed to put this functionality into debug_info().
I revised the patch and uploaded it as stack_depth-V2.diff. The control stack depth is returned upon calling debug_info(DINFO_TRACE,DIT_CURRENT_DEPTH).

zesstra

2008-12-14 15:13

administrator   ~0000818

Applied in r2447. ;-)

Issue History

Date Modified Username Field Change
2008-09-13 14:04 zesstra New Issue
2008-09-13 14:04 zesstra Status new => assigned
2008-09-13 14:04 zesstra Assigned To => zesstra
2008-10-05 10:55 zesstra File Added: stack_depth.diff
2008-10-05 10:56 zesstra Note Added: 0000804
2008-12-12 16:26 zesstra File Added: stack_depth-V2.diff
2008-12-12 16:28 zesstra Note Added: 0000814
2008-12-14 15:13 zesstra Status assigned => resolved
2008-12-14 15:13 zesstra Fixed in Version => 3.3.718
2008-12-14 15:13 zesstra Resolution open => fixed
2008-12-14 15:13 zesstra Note Added: 0000818