Forum > General

How to debug critical sections? (equal use of Enter and Leave)

<< < (2/2)

Martin_fr:
Well, here is how I solved a similar issue. It's quite some work to setup though.

Look at the FpDebug package (unit FpPascalParser).


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  FSourceTypeSymbol.AddReference{$IFDEF WITH_REFCOUNT_DEBUG}(@FSourceTypeSymbol, 'TPasParserSymbolValueMakeReftype'){$ENDIF}; 
AddReference and ReleaseReference have to be in pairs (same problem you have). Each of the calls have a unique name.

If an outer caller is responsible, I opted for a way to rename the entry: "Result.DbgRenameReference".
But it would also be possible to pass the name in over several calls....

The code that deals with it is in LazUtils/LazClasses.
It tracks all the calls => you might want to track them in a global list. Then you can at some point dump that list to a file. And you would see which "enter" have not been matched by a "leave".

Instead of keeping track in a list, you can immediately lock the enter/leave names to a file. Just count the amount of occurrences in that file.

It all relies, on passing in the required unique names, so you can identify the caller. 

----------------------
You can try to log stack traces (address only / names take way to long). But even address only can be slow....
But you then need tools (like gdb and self written scripts) to resolve the addresses and find unmatched entries....) So in the end, that is a lot of work too.


440bx:

--- Quote from: piola on January 25, 2022, 11:10:05 pm ---The problem is not entering (I can track this quite well) but leaving. Or better say: forgotten to leave. This happens from time to time because I always type the "TRY FINALLY END" en bloc and add the parts between TRY and FINALLY and between FINALLY and END later. And sometimes I forget the latter -> missing FLock.Leave.

But anyway, thanks for all your help.

--- End quote ---
I see you are using Windows.  There is a solution that after some "setup" works nicely for that kind of problem and others that need balanced enter/leave, acquire/release and so on.  The method is to have a piece of code that traps the APIs you're interested in monitoring and either logs them or displays the enter/leave on a console.  As you pointed out, the hard part is usually figuring out that a leave/release is missing. If you trap APIs then what you do is select another API that you know should only be called when the resource (lock or whathaveyou) has been released. That way you have a "bracketed" set of enter/leave and you can see where a leave is missing. 

Basically, what Martin_fr above was saying (logging) but using/trapping the API(s) directly. 

HTH.

Navigation

[0] Message Index

[*] Previous page

Go to full version