Other day I blogged about the module load notifications issue in bpmd command. I also mentioned about an alternate apporach you can take by using SOSEx debugger extension developed by Steve.
The process of using SosEx in this scenario is pretty straigforward. Let’s say we have a CandleStore application that uses a third party library called InventoryManagement. In the code below, CandleStore application calls a method GetInventoryList from this third party library. The code for CandleStore application is pretty straightforward. Please note that I have added Console.Read() just to simulate loading of this third party library after a certain sequence of events. In this application, this event happened to be just a key press, whereas, in a real life application it may be certain user inputs, loading of files, waiting for certain events etc.
namespace CandleStoreManagement
{
class CandleStore
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to continue");
Console.Read();
ListInventory();
}
static void ListInventory()
{
foreach (string item in InventoryList.GetInventoryList())
Console.WriteLine(item);
}
}
}
and below is the implementation of GetInventoryList() from third party library.
namespace InventoryManagment
{
public class InventoryList
{
public static List<string> GetInventoryList()
{
List<string> = new List<string>() { "Taper", "Pillar", "Votive" };
return Inventory;
}
}
}
Let’s start running the application in Windbg. Here are the steps to get this breakpoint.
1. Load Sos/SosEx and then run sxe command to get a notification of InventoryManager module beed loaded.
.loadby sos mscorwks;.load sosex;sxe ld:InventoryManager
2. Setup a breakpoint on GetInventoryList method by using mbm from SosEx
!mbm *!InventoryManagment.InventoryList.GetInventoryList
That’s pretty much it, As I said at start nothing too complex. Now when you setup this breakpoint , you will get following warning indicating about breakpoint couldn’t be resolved and more attempts will be made as module get loaded.
The breakpoint could not be resolved immediately.
Further attempts will be made as modules are loaded.
After this just run the application. You press any key (to simulate some events happening) and then you get following notification in Windbg indicating that module has been
loaded and the breakpoint has been hit.
Breakpoint: JIT notification received for method InventoryManagment.InventoryList.GetInventoryList() in AppDomain 00564480.
Breakpoint set at InventoryManagment.InventoryList.GetInventoryList() in AppDomain 00564480.
Breakpoint 1 hit
If you check stack trace at this point, it will be on the GetInventoryList() (as you would expect)
0:000> !CLRStack OS Thread Id: 0x1694 (0) ESP EIP 0022f228 003601bb InventoryManagment.InventoryList.GetInventoryList() 0022f240 003600f4 CandleStoreManagement.CandleStore.ListInventory() 0022f29c 0036009e CandleStoreManagement.CandleStore.Main(System.String[])
Until next, happy Debugging .