Find diff of two stack traces / "relative stack trace" in PHP?

164 views Asked by At

In PHP you can get a stack trace array from \Exception::getTrace() or from debug_backtrace().

Two stack traces within the same process / request typically have some calls in common, and then some calls that are separate. Or sometimes one stack trace might be completely contained in the other, deeper, stack trace.

E.g. if you catch an exception, and then in the catch block you call debug_backtrace(), then this will generally be a sub-trace of the exception's backtrace.

For debugging purposes, it can be interesting to look only at the part of the trace that is relative to the function containing the try/catch.

What would be an elegant way to obtain this partial or relative stack trace?

I do have some idea how I would do it myself, and might post this as an answer. But I am curious if there is any better way.

One particular difficulty I expect: Two items on either trace could look the same, even if they are actually different. I assume two calls made from the same loop to be indistinguishable. I wonder if there is any trick that would make them distinguishable.

0

There are 0 answers