How to print the formatted backtrace of function calls in Drupal

Problem:

Drupal sometimes shows errors as follows 

Message EntityMetadataWrapperException: Unknown data property field_activity_status. in EntityStructureWrapper->getPropertyInfo() (line 355 of /Users/nmeegama/work_repo/clubwaka/profiles/waka/modules/contrib/entity/includes/entity.wrapper.inc)

Once we read the error we know that the exception is thrown at this line, but where is the previous call to access the mentioned property (in this case it is field_activity_status) . We need the line where the property is accessed to solve the problem

 

Solution

The solution is pretty easy we can use the 

function ddebug_backtrace()

This function is provided by the devel  module so make sure this module is installed and you have necessary permission. Once the prerequisites are fulfilled you can add a line of code on top of the error line mentioned in the error. In this case it is 

line 355 of /Users/nmeegama/work_repo/clubwaka/profiles/waka/modules/contrib/entity/includes/entity.wrapper.inc

 

The line of code to add on top of the mentioned line

ddebug_backtrace(false, 2);

This will print the  previous 2 function calls (including the first and last it will be 4) which you can use to track your function calls and find the problem causing line of code