Home > Articles > Programming > Windows Programming

Debugging Tools for Windows: SOSEX Is Now CLR 4.0 Ready

With the advent of CLR 4.0, Steve Johnson has not only updated his SOSEX debugger extension to work on the new version of the CLR but also added a bunch of new and useful commands. Mario Hewardt details some of these new commands and how they work in relationship to a faulty application.
Like this article? We recommend

Like this article? We recommend

Introduction

The SOS managed code debugger extension has long been used by engineers to troubleshoot pesky bugs in managed code applications. While SOS contains a plethora of extremely powerful commands, there were some debugging tasks that were still tedious to perform. Luckily, Steve Johnson identified these tasks and wrote a debugger extension called SOSEX that addressed some of these deficiencies. Since debugger extensions are, generally speaking, strictly tied to the operating system or runtime version, a new release is typically made available with the release of a new version of the operating system or runtime. With the advent of CLR 4.0, Steve has not only updated his SOSEX debugger extension to work on the new version of the CLR but also added a bunch of new and useful commands. In this article, I'll detail some of these new commands and how they work in relationship to a faulty application.

Installation

The SOSEX v4.0 debugger extension can be downloaded from

http://www.stevestechspot.com/SOSEXV40NowAvailable.aspx

The installation process is very simple and comes in the form of a ZIP file (x86 or x64) that contains the sosex.dll. Simply extract the flavor you are interested in and place it in your favorite debugger extensions folder (I typically install it into the default debugger folder). You can then load the debugger extension in any of your debug sessions using the .load debugger command as shown below:

.load sosex.dll

New Commands

SOSEX provides a slew of new commands as well as enhancements to existing commands. In this section of the article I will describe some of the new commands that are available as part of this new release.

Enhancements to Existing Commands

The dlk command of SOSEX is incredibly useful when analyzing potential deadlock problems in your code. Rather than having to manually do lots and lots of analysis (dumping out potentially offending stacks, correlating the locks that are taken, etc.), the dlk command automates much of this process for you. In SOSEX v2.0 the dlk command was able to do deadlock analysis based on sync blocks and with version 4.0 it now extends that functionality to include the common ReaderWriterLock. Simply run the dlk command (without any parameters) and it will grovel through all the threads looking for potential deadlocks. For example, the below output is run on an application exhibiting a deadlock using a ReaderWriterLock:

0:004> !dlk
Examining SyncBlocks...
Scanning for ReaderWriterLocks...
Scanning for lock holders on ReaderWriterLocks...
Scanning for threads waiting on SyncBlocks...
Scanning for threads waiting on ReaderWriterLocks...
Deadlock detected:
CLR thread 1 holds a Writer lock on ReaderWriterLock 00000000023236c0
             is waiting for a Writer lock on ReaderWriterLock 0000000002323680
CLR thread 3 holds a Writer lock on ReaderWriterLock 0000000002323680
             is waiting for a Writer lock on ReaderWriterLock 00000000023236c0
CLR Thread 1 is waiting at Advanced.NET.Debugging.Chapter6.Deadlock.Main(System.String[])(+0x0 IL)(+0x0 Native)
CLR Thread 3 is waiting at Advanced.NET.Debugging.Chapter6.Deadlock.ThreadProc()(+0x0 IL)(+0x0 Native)

The mk command displays a call stack of the currently selected thread (including both managed and unmanaged frames). The command has now been extended to support the -a switch which outputs both the local variables as well as parameters (combination of -l and -p switches):

0:000> !mk -a

Thread 0:
     ESP              EIP
00:U 000000000022e278 00000000771e046a ntdll!ZwWaitForMultipleObjects+0xa 
01:U 000000000022e280 000007fefd1a13a6 KERNELBASE!WaitForMultipleObjectsEx+0xe8 
02:U 000000000022e380 0000000077093143 KERNEL32!WaitForMultipleObjectsExImplementation+0xb3
03:U 000000000022e410 000007fef83765f9 mscorwks!WaitForMultipleObjectsEx_SO_TOLERANT+0xc1 
04:U 000000000022e4b0 000007fef837bac5 mscorwks!Thread::DoAppropriateAptStateWait+0x41 
05:U 000000000022e510 000007fef83bbe9d mscorwks!Thread::DoAppropriateWaitWorker+0x191 
06:U 000000000022e610 000007fef83975c0 mscorwks!Thread::DoAppropriateWait+0x5c 
07:U 000000000022e680 000007fef8469afa mscorwks!CLREvent::WaitEx+0xbe 
08:U 000000000022e730 000007fef8990407 mscorwks!CRWLock::RWWaitForSingleObject+0x57 
09:U 000000000022e7e0 000007fef876695b mscorwks!CRWLock::StaticAcquireWriterLock+0x3a761f
0a:U 000000000022e840 000007fef8992446 mscorwks!CRWLock::StaticAcquireWriterLockPublic+0x106 
0b:M 000000000022ea10 000007ff00170328 Advanced.NET.Debugging.Chapter6.Deadlock.Main(System.String[])
  (+0x84 IL)(+0x208 Native) [C:\Zone\Deadlock\Deadlock\Program.cs, @ 51,13]
     [A0]:args:0x00000000023235a0 (System.String[])
     [L0]:newThread:0x0000000002323770 (System.Threading.Thread)
0c:U 000000000022eaa0 000007fef853d502 mscorwks!CallDescrWorker+0x82 
0d:U 000000000022eaf0 000007fef83f9fd3 mscorwks!CallDescrWorkerWithHandler+0xd3 
0e:U 000000000022eb90 000007fef840a3af mscorwks!MethodDesc::CallDescr+0x24f 
0f:U 000000000022ede0 000007fef837dc7f mscorwks!ClassLoader::RunMain+0x22b 
10:U 000000000022f040 000007fef8361c74 mscorwks!Assembly::ExecuteMainMethod+0xbc 
11:U 000000000022f330 000007fef8399955 mscorwks!SystemDomain::ExecuteMainMethod+0x491 
12:U 000000000022f900 000007fef84adb07 mscorwks!ExecuteEXE+0x47 
13:U 000000000022f950 000007fef836855c mscorwks!_CorExeMain+0xac 
14:U 000000000022f9b0 000007fef8d4253e MSCOREE!_CorExeMain+0x3e 
15:U 000000000022f9e0 000000007708f56d KERNEL32!BaseThreadInitThunk+0xd 
16:U 000000000022fa10 00000000771c3281 ntdll!RtlUserThreadStart+0x1d 

The mdt command (nicely aligned with the native dt command) has also received a facelift by adding support for the following:

  1. Specify the recursion level by using the -r switch. For example, to specify a recursion level of 2 simply use the -r 2 switch.
  2. In addition to types, the mdt command now supports addressing individual fields using the dot notation. For example, specifying !mdt type1.field1 displays the field only. This part of the mdt command is incredibly useful when wanting to avoid navigating complicated hierarchies.
  3. In addition to "regular" types, the mdt command now understands how to traverse certain types of collections using the -e switch. More specifically, it has support for Array, ArrayList, List, HashTable and Dictionary.

The mx command (also nicely aligned with the native X command) now outputs far more information than previously including type specifics and auxiliary information. The generalized form of the mx output is shown below:

[type_specifics] <type_name> (<auxiliary_type_information)

The relationship between type speficics and auxiliary type information is detailed in Table 1.

Table 1: Relationship between type specifics and auxiliary type information

Type specifics

Description

Auxiliary type information

T

The item is a type


VT

The item is a value type

Method table

RT

The item is a reference type

Method table

I

The item is an interface


F

The item is a field

Type of field

M

The item is a method

Method descriptor

SF

The item is a static field

Type of field

SM

The item is a static method

Method descriptor

New Commands

A finalizer is a very common way to properly release native resources. At a high level, an object that implements a finalizer goes on what is known as finalization queue that the CLR uses in order to know which objects have finalizers. Please note that an object with a finalizer (rooted or not), will be placed on the finalization queue. Once an object becomes rootless, the object is moved to another data structure called the f-reachable queue where it will live until the CLR invokes the objects finalize method and the object is considered dead (excluding resurrection scenarios). The ability to inspect both the finalization queue (i.e., knowing which objects have finalizers) as well as the f-reachable queue is extremely important when debugging memory related issues. SOSEX v4.0 introduces two new commands that can be used for this inspection.

The finq command (finalization queue) lists all the objects that are on the finalization queue. An example is shown below:

0:003> !finq
Generation 0:
Address            Size Type
---------------------------------------------------------
00000000025a2b78            104 System.Threading.Thread
00000000025a36c0             24 Advanced.NET.Debugging.Chapter5.NativeEvent
00000000025a51b0             32 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
00000000025a51d0             32 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
00000000025a53b0             32 Microsoft.Win32.SafeHandles.SafeFileHandle
00000000025a5978             32 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
00000000025a5998             32 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
7 objects, 288 bytes
Generation 1:
Address            Size Type
---------------------------------------------------------
0 objects, 0 bytes
Generation 2:
Address            Size Type
---------------------------------------------------------
0 objects, 0 bytes
TOTAL: 7 objects, 288 bytes

The output of the above command shows that we have 7 objects (total size of 288 bytes) on the finalization queue all belonging to generation 0. If you are only interested in the high level statistics (not on a per object basis), you can use the -stat switch. Alternatively, if you are only interested in a specific generation, you can simply specify the generation number as a parameter.

The frq command (f-reachable queue) on the other hand, lists all objects that are on the f-reachable queue as shown below:

0:003> !frq -stat
Freachable Queue:
       Count      Total Size      Type
---------------------------------------------------------
           1             32       Microsoft.Win32.SafeHandles.SafePEFileHandle
         998          23952       TestFin.Fin
999 objects, 23984 bytes

Summary

In this article we took a look at the new release of the SOSEX managed code debugger extension. SOSEX contains a plethora of new powerful commands as well as extensions to the existing commands. Best of all, the latest version is now fully compatible with CLR v2.0 as well as CLR v4.0. While we only looked at some of the extended commands and some of the more commonly used new commands, there is more to the debugger extension and I urge the reader to take a closer look at some of the other commands it has to offer.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020