Finding Template Execution Stack in ColdFusion

Posted July 5, 2006 at 2:39 PM

Tags: ColdFusion

There have been times when I wanted to get a list of all the ColdFusion templates that were executed during a single page call. I wanted to get a list similar to that of the debugging information that shows at the bottom of the page when debugging output is turned on. To figure it out, I looked at the template that handles the current debugging. When you look in the file, getting this information is actually really simple. To get a stack trace, you just have to create a ColdFusion service factory and ask for the debugging service:

 Launch code in new window » Download code as text file »

  • <!--- Create ColdFusion service factory instance. --->
  • <cfset objFactory = CreateObject(
  • "java",
  • "coldfusion.server.ServiceFactory"
  • ) />
  •  
  • <!--- Get the debugging service from the service factory. --->
  • <cfset objDebugging = objFactory.GetDebuggingService() />
  •  
  • <!---
  • Get the events table. This includes all events
  • that have taken place, not just template executions.
  • This is returned as a query.
  • --->
  • <cfset qEvents = objDebugging.GetDebugger().GetData() />
  •  
  • <!---
  • Now that we have all the events in query format, do a
  • query of queries to get only events that were template
  • executions event.
  • --->
  • <cfquery name="qTemplates" dbtype="query">
  • SELECT
  • line,
  • parent,
  • template,
  • endtime,
  • starttime
  • FROM
  • qEvents
  • WHERE
  • type = 'Template'
  • ORDER BY
  • template ASC
  • </cfquery>
  •  
  • <!--- Dump out the query of template. --->
  • <cfdump var="#qTemplates#" />

That's all there is to it. As far as I know, the ServiceFactory stuff was not documented in ColdFusion MX 6, and was considered "unsupported", but I think that in ColdFusion 7, this is a fully supported factory object and is available for programmers to leverage.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Oct 30, 2008 at 2:36 PM // reply »
5 Comments

Hi Ben,

Thanks for posting this -- just this morning I needed code to solve this problem (for my debugging library) and your sample works great. I've refactored it into a UDF, and made a few tweaks. Would you mind if I submitted the UDF to CFLib.org? I'll of course give you credit for the original work.

Thanks again.
Nolan


Oct 30, 2008 at 4:22 PM // reply »
6,516 Comments

@Nolan,

I am sure I learned this trick somewhere else (not sure I deserve credit).


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »