stack trace

Ketan Jetty
enthusiasm for technology

stack trace

To get the stack trace we dip into the coldfusion service factory object and get the data from the debugging service. This function gathers the data and displays the data in a neat orderly format in a saved htm save.

Coldfusion stack trace code
<!--- how to use the stack trace function --->
<cfset monitorObj = CreateObject("component","monitor")>
<cfset monitorObj.getStackTrace()>

<!--- get a complete stack trace of a request and write to a file --->
<cffunction name="getStackTrace" access="public" returntype="void" output="true">
	<cfset var objFactory = CreateObject( "java", "coldfusion.server.ServiceFactory" ) />
	<cfset var objDebugging = objFactory.GetDebuggingService() />
	<cfset var qEvents = objDebugging.GetDebugger().GetData() />
	<cfset var retVal = "no data" />

	<!---cfquery name="qTemplates" dbtype="query">
		SELECT 		parent, template, body, name, line, starttime, attributes,
					(endtime - starttime) as exeTime, stacktrace as stacktracedata
		FROM		qEvents
		ORDER BY	starttime ASC, line ASC
	<cfquery name="qTemplates" dbtype="query">
		SELECT 		parent, template, body, name, line, attributes,
					starttime, endtime, stacktrace as stacktracedata
		FROM		qEvents

	<cfquery name="qDistinctTemplates" dbtype="query">
		SELECT 		DISTINCT template
		FROM		qEvents
		ORDER BY	template

	<cfsavecontent variable="retVal">
		<script language="javascript">
			function toggleStack(obj)
			{	if (  == 'hidden' &&  == 'none') {  = 'visible';  = 'block';
				} else {  = 'hidden';  = 'none';

		<table style="font-family:verdana;font-size:9px;">
			<td></td><td colspan="5"><cfoutput>#DateFormat(now(),"full")#    #TimeFormat(now(),"full")#</cfoutput></td>
		<tr bgcolor="ffccff">
		<cfset prevStartTime = 0>
		<cfset currStartTime = 0>
		<cfset totalexetime = 0>
		<cfset color1 = "ffffff">
		<cfset color2 = "ffffee">
		<cfset tempColor = color1>

		<cfoutput query="qTemplates">
			<cfset currStartTime = starttime>
			<cfset exeTime = val(endtime) - val(starttime)>
			<cfset totalexetime = totalexetime + exeTime>

			<cfif (prevStartTime NEQ 0) AND (prevStartTime NEQ currStartTime)>
				<cfif tempColor EQ color1>
					<cfset tempColor = color2>
					<cfset tempColor = color1>

			<tr bgcolor="#tempColor#">
				<td valign="top" bgcolor="ffccff">#qTemplates.currentrow#</td>
				<td valign="top">#parent#</td>
				<td valign="top">#Template#</td>
				<td valign="top"><font color="blue">#exeTime# ms</font>  #name#<br>#Body#<br><cfif IsArray(attributes) AND ArrayLen(attributes)><cfdump var="#attributes#"></cfif></td>
				<td valign="top">#Line#</td>
				<td valign="top" onclick="toggleStack(id#qTemplates.currentrow#)">
					<table width="90%" bgcolor="8080c0" style="font-family:verdana;font-size:9px;"><tr><td nowrap="nowrap">[+/-]</td></tr></table>
					<div id="id#qTemplates.currentrow#" style="visibility:hidden; display:none;">
						<cfdump var="#stacktracedata#">

			<cfset prevStartTime = currStartTime>
		<tr style="font-family:verdana;font-size:10px;">
			<td bgcolor="ffccff"></td><td bgcolor="ffccff"><b>Total Execution time: <cfoutput>#totalexetime#</cfoutput> ms</b></td><td colspan="4"> </td>
		<table style="font-family:verdana;font-size:9px;">
		<cfoutput query="qDistinctTemplates">
			<td>#CurrentRow#</td><td> </td><td>#template#</td>
		<cfdump var="#arguments#">
	<!--- thread sleep for 100ms, to avoid overwriting the stacktrace file --->
	<cfset thread = CreateObject("java", "java.lang.Thread")>
	<cfset thread.sleep(100)>
	<!--- make sure that the c:/dumps folder has been created --->
	<cfif NOT DirectoryExists("c:/dump")>
		<cfdirectory action="create" directory="c:/dump">

	<cffile action="write" file="c:/dump/zstacktrace_#DateFormat(now(),'yyyy-mm-dd-')##TimeFormat(now(),'HH-mm-ss-lll')#.htm" output="#retVal#" />

Resulting Stack Trace sample file
For a sample stack trace, click here [this is a 576kb htm file, download time will be slow]

CF Quick Reference

Ginger CMS
the future of cms, a simple, easy and intutive content management system ... more

cf prototyping engine, generates boilerplate code and ... more

Jrun monitor
monitor and timely auto-restart to avoid Jrun hang ... more

Inheritance Config.
uses OOPs inheritance to create configuration file ... more

Real Estate App.
complete real estate application using data from MLS ... more

Search Engine Lite
create your own search engine for your web site ... more