Bug #44428 [Com]: "file" and "line" missing in debug_backtrace() output

From: Date: Sun, 31 Jan 2021 13:31:48 +0000
Subject: Bug #44428 [Com]: "file" and "line" missing in debug_backtrace() output
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-231853@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=44428&edit=1

 ID:                 44428
 Comment by:         fgm at osinet dot fr
 Reported by:        thuejk at gmail dot com
 Summary:            "file" and "line" missing in debug_backtrace()
                     output
 Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        5.2.5
 Assigned To:        dmitry
 Block user comment: N
 Private report:     N

 New Comment:

I tested this fragment with all versions from 5.3 to 7.4.

The issue is present until 5.6, included; but absent starting with 7.0.


Previous Comments:
------------------------------------------------------------------------
[2008-09-02 09:54:34] dmitry@php.net

The debug backtrace shows filename and lineno of calling script. In case function is called from
inside internal function (may be as callback) no filename and lineno may be set.

------------------------------------------------------------------------
[2008-03-30 17:56:28] php at xuefeng dot org

Index: zend_builtin_functions.c
===================================================================
RCS file: /repository/ZendEngine2/zend_builtin_functions.c,v
retrieving revision 1.277.2.12.2.25.2.14
diff -u -r1.277.2.12.2.25.2.14 zend_builtin_functions.c
--- zend_builtin_functions.c	10 Mar 2008 22:02:41 -0000	1.277.2.12.2.25.2.14
+++ zend_builtin_functions.c	30 Mar 2008 17:54:29 -0000
@@ -2000,6 +2000,19 @@
 	} else if (ptr->function_state.function->common.scope) {
 		add_assoc_string_ex(stack_frame, "class", sizeof("class"),
ptr->function_state.function->common.scope->name, 1);
 		add_assoc_string_ex(stack_frame, "type", sizeof("type"), "::", 1);
+
+	} else if (ptr->function_state.function->common.type == ZEND_USER_FUNCTION) {
+
+		if (ptr->function_state.function->op_array.filename) {
+			add_assoc_string_ex(stack_frame, "file", sizeof("file"), 
+								ptr->function_state.function->op_array.filename, 1
+			);
+		}
+		if (ptr->function_state.function->op_array.opcodes) {
+			add_assoc_long_ex(stack_frame, "line", sizeof("line"), 
+								ptr->function_state.function->op_array.opcodes->lineno
+			);
+		}
 	}
 
 	if ((! ptr->opline) || ((ptr->opline->opcode == ZEND_DO_FCALL_BY_NAME) ||
(ptr->opline->opcode == ZEND_DO_FCALL))) {

------------------------------------------------------------------------
[2008-03-13 12:03:59] thuejk at gmail dot com

Description:
------------
In all cases (but the one below one), "file" and "line" indexes are defined in
each frame of the output of debug_backtrace.

But when using call_user_function() or call_user_function_array, "file" and
"line" is not defined for one of the frames.

It would be nice if I could count on file and line always being defined when using the output of
debug_backtrace, thereby avoiding code for special cases.

For the missing file and line in the example code below, when calling f() inside
call_user_function(), I suggest using the file and line of the call_user_function() call.

See also bug 38047 and 24405.

Reproduce code:
---------------
<?php
error_reporting(E_ALL | E_STRICT);


function f() {
  $a = $b;
  var_dump(debug_backtrace());
}

//The backtrace generated here will not have file and line defined
call_user_func("f", Array());

//The backtrace generated here will have file and line defined.
f();

?>

Expected result:
----------------
The frame with index 0 of the first debug_backtrace_call should contain indexes "file"
=> "test.php" and "line" => 6

Actual result:
--------------
array(2) {
  [0]=>
  array(2) {
    ["function"]=>
    string(1) "f"
    ["args"]=>
    array(1) {
      [0]=>
      &array(0) {
      }
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(18) "/home/tjk/test.php"
    ["line"]=>
    int(6)
    ["function"]=>
    string(14) "call_user_func"
    ["args"]=>
    array(2) {
      [0]=>
      &string(1) "f"
      [1]=>
      &array(0) {
      }
    }
  }
}
array(1) {
  [0]=>
  array(4) {
    ["file"]=>
    string(18) "/home/tjk/test.php"
    ["line"]=>
    int(8)
    ["function"]=>
    string(1) "f"
    ["args"]=>
    array(0) {
    }
  }
}



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=44428&edit=1


Thread (4 messages)

« previous php.bugs (#231853) next »