473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

System.Diagnostics.StackTrace is wrong sometims

I use System.Diagnostics.StackTrace to get the stack trace of the
current function, to print out the function's name to the log, and
sometimes it shows that a function that doesn't even write to the log
is what is writing to the log. This just can't be, so
System.Diagnostics.StackTrace must be returning the wrong information
(from another thread, i suppose).

Is this normal, or expected? Can it be reliable?

Zytan

Apr 25 '07 #1
12 3794
I doubt it. What's more than likely is you are not synchronizing access
to the log, so you are writing stack traces that are intermingled with each
other.

What you need to do is synchronize access to the log file, writing out
the stack traces, or, have some sort of correlation id associated with each
stack trace that you write, which will allow you to group all the
information later.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zytan" <zy**********@gmail.comwrote in message
news:11**********************@b40g2000prd.googlegr oups.com...
>I use System.Diagnostics.StackTrace to get the stack trace of the
current function, to print out the function's name to the log, and
sometimes it shows that a function that doesn't even write to the log
is what is writing to the log. This just can't be, so
System.Diagnostics.StackTrace must be returning the wrong information
(from another thread, i suppose).

Is this normal, or expected? Can it be reliable?

Zytan

Apr 25 '07 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
I doubt it. What's more than likely is you are not synchronizing
access to the log, so you are writing stack traces that are intermingled
with each other.
I doubt that the StackTrace is wrong too. However, Zytan's point is that
function names wind up in the log that should *never* be in the log. It's
not that they're in the wrong order. It's that they should just never
appear at all.

For that, I have no explanation.

Zytan, if you can provide a concise example of code that reliably reproduces
the problem, that would be helpful.

Pete

Apr 25 '07 #3
I doubt it. What's more than likely is you are not synchronizing access
to the log, so you are writing stack traces that are intermingled with each
other.
That is likely. I am using TextWriter, which is synchronized, but if
I make two calls to my LogWrite function, then they could get mixed
up. The problem is that I can see when such a thing happens, and my
issues occur so often, it must be something else. Although, your
point is taken, and I'll solve this issue ANYWAY, even though it
hardly happens, to remove that as a possibility.

I'll reply to Peter's post with what I think is the real issue...

thanks,
Zytan

Apr 26 '07 #4
I doubt that the StackTrace is wrong too.

I know, how can it be wrong, when so many people use it?
However, Zytan's point is that
function names wind up in the log that should *never* be in the log. It's
not that they're in the wrong order. It's that they should just never
appear at all.

For that, I have no explanation.
I have narrowed the issue down:

It works perfectly, always, in debug mode.
It doesn't work, in release mode.

I MAY have deleted the .pdb files from the release mode directory for
EACH time this issue occurred. Would that cause this to happen?
Would it be left to guess (incorrectly) without the PDB?

Zytan

Apr 26 '07 #5
Since I cannot see my own post yet, due to Google Groups being REALLY
slow the past 2 days, I will append here:

I have the PDB file in the release directory of my program, and the
bug still occurs. So, the presence or lack of the PDB file is not
what is causing this.

I'll try and make a small program that shows the error, and maybe I'll
solve this myself in the process.

Zytan

Apr 26 '07 #6
System.Diagnostics.StackTrace callStack = new
System.Diagnostics.StackTrace();

returns different information whether you're in debug or release mode.

Zytan

Apr 26 '07 #7
(I still cannot see any additional posts to this thread, including my
own so I'll post my results here)

I have a GetCallStack() function that just calls:
x = new System.Diagnostics.StackTrace();
and extracts the stack from x, and returns it as a string.

When I call this from Main(), I get:

// DEBUG: ThreadHelper.ThreadStart() --ExecutionContext.Run() -->
ThreadHelper.ThreadStart_Context() --HostProc.RunUsersAssembly() -->
AppDomain.ExecuteAssembly() --AppDomain.nExecuteAssembly() -->
Program.Main() --Program.GetCallStack()
// RELEASE: Program.Main() --Program.GetCallStack()

Note the last two in the stack are similar.
But, when I call this function from inside a class function, that
Main() calls:

// DEBUG: ThreadHelper.ThreadStart() --ExecutionContext.Run() -->
ThreadHelper.ThreadStart_Context() --HostProc.RunUsersAssembly() -->
AppDomain.ExecuteAssembly() --AppDomain.nExecuteAssembly() -->
Program.Main() --MyClass.MyFunc() --Program.GetCallStack()
// RELEASE: Program.Main() --Program.GetCallStack()

Note the last 2/3 of each one is different. MyClass.MyFunc() is no
where to be found in the release build!!!

Zytan

Apr 26 '07 #8
"Zytan" <zy**********@gmail.comwrote in message
news:11*********************@n35g2000prd.googlegro ups.com...
[...]
It works perfectly, always, in debug mode.
It doesn't work, in release mode.

I MAY have deleted the .pdb files from the release mode directory for
EACH time this issue occurred. Would that cause this to happen?
Would it be left to guess (incorrectly) without the PDB?
Can you post a short sample of code that displays a correct stack trace when
compiled as Debug and an incorrect stack trace when compiled as Release?

Pete

Apr 26 '07 #9
"Zytan" <zy**********@gmail.comwrote in message
news:11**********************@r35g2000prh.googlegr oups.com...
[...]
Note the last two in the stack are similar.
But, when I call this function from inside a class function, that
Main() calls:

// DEBUG: ThreadHelper.ThreadStart() --ExecutionContext.Run() -->
ThreadHelper.ThreadStart_Context() --HostProc.RunUsersAssembly() -->
AppDomain.ExecuteAssembly() --AppDomain.nExecuteAssembly() -->
Program.Main() --MyClass.MyFunc() --Program.GetCallStack()
// RELEASE: Program.Main() --Program.GetCallStack()

Note the last 2/3 of each one is different. MyClass.MyFunc() is no
where to be found in the release build!!!
Ah. Is it possible that the automatic function inlining built into C# is
causing your function to just "disappear"? It won't show up in a stack
trace if it no longer exists.

Pete

Apr 26 '07 #10
Ah. Is it possible that the automatic function inlining built into C# is
causing your function to just "disappear"? It won't show up in a stack
trace if it no longer exists.
I've just checked, and for that to happen, it needs to miss TWO
functions in the chain, but they ARE in the same call stack. And
these functions are not small functions! But, at least this explains
what is happening...

Unfortunately, it makes the call stack rather useless to determine
what the current function is...

Zytan

Apr 28 '07 #11
Can you post a short sample of code that displays a correct stack trace when
compiled as Debug and an incorrect stack trace when compiled as Release?
static void Main(string[] args)
{
MyFunc();
Console.ReadKey(false);
}

public static void MyFunc()
{
Console.WriteLine(Program.GetCallStack());
}

public static string GetCallStack()
{
System.Diagnostics.StackTrace callStack = new
System.Diagnostics.StackTrace();
string s = "";
int index = 0;
while (true)
{
System.Diagnostics.StackFrame frame =
callStack.GetFrame(index);
if (frame == null) break;
System.Reflection.MethodBase method = frame.GetMethod();

if (index 0) s = " --" + s;
s = method.DeclaringType.Name + "." + method.Name + "()" + s;
index++;
}
return (s);
}

Zytan

Apr 28 '07 #12
Ah. Is it possible that the automatic function inlining built into C# is
causing your function to just "disappear"? It won't show up in a stack
trace if it no longer exists.
Pete, I think I solved the issue.

My functions were large, so they were NOT being deleted from the call
stack, but..... my WriteFunction() function which my function call is
tiny. AND, it has to take measures to not print ITSELF when its
caller is asking "who am I?". So, it counts to skip itself where it
expects to be in the call stack. In release mode, since it is so
small, it gets ejected from the call stack, and thus when it chooses
to skip itself, it's really skipping the function name that I wanted!
And, furthermore, because this WriteFunction() is a wrapper, and not
the real deal, they are likely BOTH ejected from the release call
stack, which explains why TWO of my treasured functions are missing
from the display!

NOW, for the solution to this mess...

Zytan

Apr 28 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raj Dhrolia | last post by:
Hi, In one of my method, I want to know the class/function that called/invoked this method. i.e. i have a function named Method1() in class1. I want "ClassX.MethodX()" in Method1(), when...
1
by: ginee lee via .NET 247 | last post by:
(Type your message here) hi all, It seems that the arguments of System.Diagnostics.Process.Start()can only be the absolute path. The args can not be like".\abc\efg.exe" or "..\abc\efg.exe". While i...
5
by: David Conorozzo | last post by:
I am able to create new sources using CreateEventSource. I can call GetEventLogs, I can delete sources, I can see if sources exist BUT, as soon as I try a "WriteEntry", I get an exception: ...
4
by: Jiho Han | last post by:
I have the following defined in web.config under <configuration> node: <system.diagnostics> <switches> <add name="MainSwitch" value="4"/> </switches>
1
by: martin | last post by:
Hi, I'm having some problems with the System.Diagnostics.EventLog class in .NET 2.0 I need to recreate an event message source inside a new log but the messages keeps ending up in the old...
4
by: Ben | last post by:
Hello everybody I got confused by this problem for which I don't have a logical explanation. There is a Thread (ThreadA) which receives Events from another system thread (ThreadS). ThreadA then...
1
by: dwcscreenwriterextremesupreme | last post by:
I am trying to automate a mysqldump backup through C# and for some reason the System.Diagnostics.Process is just not working as expected for me. What I want is for the following code to start a...
6
by: kimiraikkonen | last post by:
Hello, I want to ask this: If i do: System.Diagnostics.Process.Start("c:\lame", "--preset standard c:\blabla.wav c:\blabla.mp3") it works. But i don't want this. I want my 2 textboxes must...
7
rhitam30111985
by: rhitam30111985 | last post by:
Hi all i am trying to compile a set of source files located in location In the following code sample i am trying to compile a visual c++ 6.0 (dsp) project from a c# application ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.