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--->
	
	<cfquery name="qTemplates" dbtype="query">
		SELECT 		parent, template, body, name, line, attributes,
					starttime, endtime, stacktrace as stacktracedata
		FROM		qEvents
	</cfquery>

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

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

		<table style="font-family:verdana;font-size:9px;">
		<tr>
			<td></td><td colspan="5"><cfoutput>#DateFormat(now(),"full")#    #TimeFormat(now(),"full")#</cfoutput></td>
		</tr>
		<tr bgcolor="ffccff">
			<td></td><td>parent</td><td>template</td><td>body</td><td>line</td><td>stacktrace</td>
		</tr>
		<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>
				<cfelse>
					<cfset tempColor = color1>
				</cfif>
			</cfif>

			<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>
					<br>
					<div id="id#qTemplates.currentrow#" style="visibility:hidden; display:none;">
						<cfdump var="#stacktracedata#">
					</div>
				</td>
			</tr>

			<cfset prevStartTime = currStartTime>
		</cfoutput>
		<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>
		</tr>
		</table>
		<br>
		<table style="font-family:verdana;font-size:9px;">
		<cfoutput query="qDistinctTemplates">
		<tr>
			<td>#CurrentRow#</td><td> </td><td>#template#</td>
		</tr>
		</cfoutput>
		</table>
		<hr>
		<cfdump var="#arguments#">
	</cfsavecontent>
	
	<!--- 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">
	</cfif>

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

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


CF Quick Reference


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


CFTurbine
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