phpob-start

Trapping line of code that emits first character


Suddenly, an application isn't any longer able to output ZIP files. An inspection revealed the cause: The first character of the ZIP is a blank, which breaks the ZIP format spec.

To track down this problem, I enabled CStatementTracer, which prints each line of executed code to a log file. Didn't help. [Remark: declare(ticks=1); doesn't seem to trap each line of executed code]

I then set an output handler like so:

function callback( $buffer )  {

    $deb = print_r( debug_backtrace(), TRUE );
    file_put_contents( './statementTrager.log', $deb );

    return $buffer;

}

ob_start("callback", 1 );

Unfortunately, this handler isn't called at all.

Q: Does a generic / canonical solution exists, which identifies the file / line of PHP-code, which emits the first character.

A solution, that finds the loc whatever other code gets executed.

Remarks:

Meanwhile I found the suspicious like of code: A blank in front of a starting

Still, I'd like to get hints regarding a programmatic solution. Preferrably a solution written in pure PHP.


Solution

  • https://linux.die.net/man/1/strace is probably the most reliable tool to find out where the output comes from. Assuming you are on Linux. There must be similar tools for other platforms.

    Although it will not give you the line of the php code, you can analyse the context of system calls made before and after the offensive character was sent. Usually it is enough to identify where the problem originates.

    It is quite time consuming process though. Should be used as the last resort.