Search

Provide function to retrieve the entire call stack by Erland Sommarskog

Active

100
0
Sign in
to vote
Type: Suggestion
ID: 124537
Opened: 12/21/2005 1:54:01 PM
Access Restriction: Public
2
Workaround(s)
We have several log tables where among other things save the name of the current stored procedure. Typically, there is a dedicated stored procedure that writes to a log table, and we pass @@procid to a parameter.

But there are cases where this is not really enough. Maybe the stored procedure that called the logging procedure itself is a general procedure. Or if we want log something by means of a trigger, we want to know the stored procedure that fired the trigger.

The purpose for this information, is mainly for tracking down problems in the application, and how some funny data may have ended up.

So what would be nedded would be a way to retrieve the entire call stack. At a minimum, we need database, schema and module (preferrably as names, rather than ids). Dynamic SQL would be represented by some placeholder.
Real nice would also be to have the line number where the next item on the stack was invoked, and for a real luxury actual parameter values would also be included.

Note that here is an important security observation: since the stored procedure are typically run by non-priv users, it must not be the case that such a function returns nothing, because the user does not have VIEW DEFINITION on the objects. This needs to be solved, for instance by relying on ownership chaining. Or from the simple fact, that if the module is on the call stack, the user indeeds has some permission to execute them.
Details (expand)
Product Language
English
Version
SQL Server 2005 - Developer Edition (32)
Category
SQL Engine
Operating System
Windows XP SP2 Professional
Operating System Language
English
Proposed Solution
Data could be returned by table-valued function, DMV, or an XML document. Since we probably would want to store the entire call stack in one column, the XML document is a bit appealing.
Benefits
Improved Security
Improved Administration
Other Benefits
Improved Administration
File Attachments
0 attachments
Sign in to post a comment.
Posted by Shawn Eary on 2/23/2010 at 8:19 PM
In addition to a standard T-SQL callstack of functions/stored procedures, it would also be useful if the "call stack" could also "magically" indicate which if any *.NET methods triggered the stored procedure.

Suppose I have a VB.NET function like
Public Function GetDataSource() As DataView
' return result of T-SQL Stored Called sp_makeInventoryDataSource
End Function

Then suppose sp_makeInventoryDataSource called a T-SQL function called fn_getItemName(itemID).

Then the calls stack for fn_getItemName should not only contain sp_makeInventoryDataSource but it should also show the VB.NET function GetDataSource and might look like:
[T-SQL CAll] fn_getItemName
[T-SQL Call] sp_makeInventoryDataSource
[VB Call] GetDataSource
Posted by Rajib.Bahar on 10/15/2009 at 6:32 AM
I'd find it useful for debugging purpose. Also, debugging option in SQL 2008 meets my need to some degree.
Posted by John Couch on 8/5/2009 at 4:44 AM
I have to agree with Erland. A callstack would be awesome. Someway of logging it outto an eventlog or table. I have been trying to figure out how to create such a thing to show the where the error occured back up through to the originating call.
Posted by Anton Plotnikov on 6/26/2009 at 12:33 AM
It is a very useful feature, especially for debugging and tech supporting
Posted by aamirghanchi on 3/18/2009 at 10:03 AM
Not sure if you have added this feature to SQL 2008, but please do add it to next release of SQL 2005.
Thanks!

Aamir
Posted by Bjorn D. Jensen on 10/5/2008 at 3:28 AM
yes, it would be really helpfull if we could do things like
using dbms_stack or dbms_trace in Oracle PL/SQL.
/B. D. Jensen
Posted by Jamie Thomson on 4/7/2008 at 12:18 PM
This would be of use in the scenario I describe here: https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=189211

-Jamie
Posted by Erland Sommarskog on 10/19/2007 at 2:15 PM
Any update on this? I happened to see this in the event Blocked Process Report:

<executionStack>
    <frame line="17" stmtstart="1332" stmtend="2906" sqlhandle="0x03000d00399f00558c840601a29800000100000000000000"/>
    <frame line="1" sqlhandle="0x01000d00638e300f302e10ac000000000000000000000000"/>
</executionStack>

That is, here the call stack is available. Through the sqlhandle it is possible to retrieve
the objectids of the calling procedures.

If if it's available this way, it can't be too difficult to expose this in a DMV. Only exposing the
sqlhandle would not match with my observation with permissions above, but since you probably
only want this code in a few places, this could be dealt with adding extra privs with
certificate signing.
Posted by Microsoft on 10/16/2006 at 11:24 AM
Hi Erland,

Thanks for bringing this suggestion to our attention. This would definitly be a useful feature and we are considering implementing it in the next version of SQL Server.

Thanks,
Tomer Verona
SQL Engine Development
Sign in to post a workaround.
Posted by KJS714 on 5/27/2009 at 10:31 PM
Using CONTEXT_INFO solves the trigger problem, and possibly the others. It is 128 bytes, so if your call stack is too deep, you might not be able to keep everything you need in it.
Posted by Gabriel McAdams on 2/2/2010 at 6:24 PM
I created a couple of functions and procedures that take advantage of CONTEXT_INFO to create a call stack. I added it as a tip to my blog at http://www.thecodepage.com/post/TIP-A-Call-Stack-in-SQL-Server.aspx