<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fooling Around &#187; debug</title>
	<atom:link href="http://alax.info/blog/tag/debug/feed" rel="self" type="application/rss+xml" />
	<link>http://alax.info/blog</link>
	<description>// Software Production Line</description>
	<lastBuildDate>Fri, 03 Feb 2012 22:49:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Hardware assisted memory corruption detection</title>
		<link>http://alax.info/blog/1319</link>
		<comments>http://alax.info/blog/1319#comments</comments>
		<pubDate>Sat, 19 Nov 2011 17:26:25 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[corruption]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[protection]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1319</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1319" title="Hardware assisted memory corruption detection"></a>So you got a memory corruption issue with a piece of software. It comes in a unique scenario along the line of having a huge pile of weird code running well most of the time and then, right out of &#8230;<p class="read-more"><a href="http://alax.info/blog/1319">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1319" title="Hardware assisted memory corruption detection"></a><p>So you got a memory corruption issue with a piece of software. It comes in a unique scenario along the line of having a huge pile of weird code running well most of the time and then, right out of the blue, a corruption takes place followed by unexpected code execution and unstable software state in general.</p>
<p>The biggest problem with memory corruption is that a fragment of code is modifying a memory block which it does not own, and it has no idea who actually is the owner of the block, while the real owner has no timely way to detect the modification. You only face the consequences being unable to capture the modification moment in first place.</p>
<p>To get back to the original cause, an engineer has to drop into a time machine, turn back time and step back to where the trouble took originally place. As developers are not actually given state-of-the-art time machines, the time turning step is speculative.</p>
<h4>CVirtualHeapPtr Class: Memory with Exception-on-Write access mode</h4>
<p>At the same time a Windows platform developer is or might be aware of <a href="http://msdn.microsoft.com/en-us/library/ms810627.aspx">virtual memory API</a> which among other things provides user mode application with capabilities to define memory protection modes. Having this on hands opens unique opportunity to apply read-only protection (PAGE_READONLY) onto a memory block and have exception raised at the very moment of unexpected memory modification, having call stack showing up a source of the problem. I refer to this mode of operation as &#8220;hardware assisted&#8221; because the access violation exception/condition would be generated purely in hardware without any need to additionally do any address comparison in code.</p>
<p>Needless to say that this way is completely convenient for the developer as he does not need to patch the monstrous application all around in order to compare access addresses against read-only fragment. Instead, a block defined as read-only will be immediately available as such for the whole process almost without any performance overhead.</p>
<p>As ATL provides a set of memory allocator templates (<a href="http://msdn.microsoft.com/en-us/library/3by29yh0%28v=vs.80%29.aspx">CHeapPtr</a> for heap backed memory blocks, allocated with <em>CCRTAllocator</em>, alternate options include <a href="http://msdn.microsoft.com/en-us/library/80zw33a6%28v=vs.80%29.aspx">CComHeapPtr</a> with <em>CComAllocator</em> wrapping <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms692727%28v=vs.85%29.aspx">CoTaskMemAlloc</a>/<em>CoTaskMemFree</em> API), let us make an alternate allocator option that mimic well-known class interface and would facilitate corruption detection.</p>
<p>Because virtual memory allocation unit is a page, and protection mode is defined for the whole page, this would be the allocation granularity. For a single allocated byte we would need to request <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958%28v=vs.85%29.aspx">SYSTEM_INFO::dwPageSize</a> bytes of virtual memory. Unlike normal memory heap manager, we have no way to share pages between allocations as we would be unable to effectively apply protection modes. This would definitely increase application pressure onto virtual memory, but is still acceptable for the sacred task of troubleshooting.</p>
<p>We define a <em>CVirtualAllocator</em> class to be compatible with ATL&#8217;s <em>CCRTAllocator</em>, however based on <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx">VirtualAlloc</a>/<em>VirtualFree</em> API. The smart pointer class over memory pointer would be defined as follows:</p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">template</span> <span style="color: #800080;">&lt;</span><span style="color: #800000; font-weight: bold;">typename</span> T<span style="color: #800080;">&gt;</span>
<span style="color: #800000; font-weight: bold;">class</span> CVirtualHeapPtr <span style="color: #800080;">:</span>
    <span style="color: #800000; font-weight: bold;">public</span> CHeapPtr<span style="color: #800080;">&lt;</span>T<span style="color: #808030;">,</span> CVirtualAllocator<span style="color: #800080;">&gt;</span>
<span style="color: #800080;">{</span>
<span style="color: #800000; font-weight: bold;">public</span><span style="color: #e34adc;">:</span>
<span style="color: #696969;">// CVirtualHeapPtr</span>
    CVirtualHeapPtr<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throw</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">explicit</span> CVirtualHeapPtr<span style="color: #808030;">(</span>_In_ T<span style="color: #808030;">*</span> pData<span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throw</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #603000;">VOID</span> SetProtection<span style="color: #808030;">(</span><span style="color: #603000;">DWORD</span> nProtection<span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        <span style="color: #696969;">// </span><span style="color: #ffffff; background: #808000;">TODO: ...</span>
    <span style="color: #800080;">}</span>
<span style="color: #800080;">}</span><span style="color: #800080;">;</span></pre>
<p>The <em>SetProtection</em> method is to define memory protection for the memory block. Full code for the classes <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.h#L9">is available on Trac here</a> (lines 9-132):</p>
<ul>
<li><em>CGlobalVirtualAllocator</em> class is a singleton querying operating system for virtual memory page size, and provides alignment method</li>
<li><em>CVirtualAllocator</em> class is a <em>CCRTAllocator</em>-compatible allocator class</li>
<li><em>CVirtualHeapPtr</em> class is smart template class wrapping a pointer to allocated memory</li>
</ul>
<p>Use case code will be as follows. &#8220;SetProtection(PAGE_READONLY)&#8221; enables protection on memory block and turns on exception generation at the moment memory block modification attempt. &#8220;SetProtection(PAGE_READWRITE)&#8221; would restore normal mode of memory operation.</p>
<pre style="color: #000000; background: #ffffff;">CVirtualHeapPtr<span style="color: #800080;">&lt;</span><span style="color: #603000;">BYTE</span><span style="color: #800080;">&gt;</span> p<span style="color: #800080;">;</span>
p<span style="color: #808030;">.</span>Allocate<span style="color: #808030;">(</span><span style="color: #008c00;">2</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
p<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #008000;">0x01</span><span style="color: #800080;">;</span>
p<span style="color: #808030;">.</span>SetProtection<span style="color: #808030;">(</span>PAGE_READONLY<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #696969;">// NOTE: Compile with /EHa on order to catch the exception</span>
_ATLTRY
<span style="color: #800080;">{</span>
    p<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #008000;">0x02</span><span style="color: #800080;">;</span>
    <span style="color: #696969;">// NOTE: We never reach here due to exception</span>
<span style="color: #800080;">}</span>
_ATLCATCHALL<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    <span style="color: #696969;">// NOTE: Catching the access violation for now to be able to continue execution</span>
<span style="color: #800080;">}</span>
p<span style="color: #808030;">.</span>SetProtection<span style="color: #808030;">(</span>PAGE_READWRITE<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
p<span style="color: #808030;">[</span><span style="color: #008c00;">1</span><span style="color: #808030;">]</span> <span style="color: #808030;">=</span> <span style="color: #008000;">0x03</span><span style="color: #800080;">;</span></pre>
<p>Given the information what data gets corrupt, the pointer allocator provides an efficient opportunity to detect the violation attempt. The only thing remained is to keep memory read-only, and temporarily revert to write access when the &#8220;legal&#8221; memory modification code is about to be executed.</p>
<p><span id="more-1319"></span></p>
<h5>One-shot Read/Write Protection with Guard Pages</h5>
<p>Another option granted by memory protection modes is brought by PAGE_GUARD flag. MSDN says:</p>
<blockquote><p>A guard page provides a one-shot alarm for memory page access. This can be useful for an application that needs to monitor the growth of large dynamic data structures. For example, there are operating systems that use guard pages to implement automatic stack checking.</p></blockquote>
<p>Setting a guard page mode provides an additional option to trigger an exception with even read access to a protected memory block.</p>
<pre style="color: #000000; background: #ffffff;">p<span style="color: #808030;">.</span>SetProtection<span style="color: #808030;">(</span>PAGE_READWRITE <span style="color: #808030;">|</span> PAGE_GUARD<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #603000;">BYTE</span> n <span style="color: #808030;">=</span> p<span style="color: #808030;">[</span><span style="color: #008c00;">0</span><span style="color: #808030;">]</span><span style="color: #800080;">;</span></pre>
<h4>CDebugHeapPtr Class: More Options to Catch Memory Corruption Conditions</h4>
<p>While setting memory protection attributes on a memory block of interest provides unique troubleshooting opportunities, it still does not cover important typical problems with memory misuse scenarios. Those are writing immediately before the allocated block, and writing immediately after. Having array of N items, this would be writing to indices -1 and N respectively.</p>
<p>To address this scenarios of misuse we can extend CVirtualHeapPtr class so that it could additionally provide &#8220;sanity pages&#8221; with PAGE_NOACCESS protection at the boundary of allocation. Because virtual memory allocation is granular, we will have to have padding bytes that extend our block to the page boundary, however we have an option to put the padding bytes before or after the payload data block in order to capture after or before memory block writes respectively.</p>
<p>The figure below shows memory layout for the data:</p>
<p><img class="alignnone size-full wp-image-1320" title="CDebugHeapPtr Memory Layout" src="http://alax.info/blog/wp-content/uploads/2011/11/image.png" alt="" width="509" height="541" /></p>
<p>Source code for the <em>CDebugHeapPtr</em> class <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/VirtualHeapPtr/VirtualHeapPtr.h#L155">is available on Trac</a> (lines 155-). The sanity pages create a block of inaccessible addresses which immediately cause access violation exception on either read of write access attempt. Under debugger, those are shows with question marks:</p>
<p><img class="alignnone size-full wp-image-1321" title="PAGE_NOACCESS Data" src="http://alax.info/blog/wp-content/uploads/2011/11/Image0011.png" alt="" width="673" height="288" /></p>
<p>The padding space is pre-initialized with hardcoded value 0&#215;77, and the space is checked for integrity at release of memory block call.</p>
<h4>Catching the Exceptions</h4>
<p>Having the exceptions generated on run-time, they immediately alter application execution code path and are easy to track and catch. There is no need to bring the feature rich debugger, such as Visual Studio to the production site in order to catch the exception and environment, instead a way simpler tool such as <a href="http://alax.info/blog/1248">LogProcessExceptions</a> would be able to create a minidump file and write the state of the application. The minidump can be transferred into debugger-enabled environment for detailed check.</p>
<p>Visual C++ .NET 2010 <a href="http://www.alax.info/trac/public/browser/trunk/Utilities/VirtualHeapPtr">source code</a> is available from SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1319/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LogProcessExceptions: Minidumps on User Request</title>
		<link>http://alax.info/blog/1248</link>
		<comments>http://alax.info/blog/1248#comments</comments>
		<pubDate>Tue, 19 Jul 2011 14:20:57 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.DMP]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[minidump]]></category>
		<category><![CDATA[support]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1248</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1248" title="LogProcessExceptions: Minidumps on User Request"></a>An updated version of LogProcessExceptions utility is given an additional option to create minidump .DMP files for debugged process on user request. This is in particular useful in conjunction with flag choices (on the previous page of the wizard). Partial &#8230;<p class="read-more"><a href="http://alax.info/blog/1248">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1248" title="LogProcessExceptions: Minidumps on User Request"></a><p>An updated version of <a href="http://alax.info/blog/1211">LogProcessExceptions</a> utility is given an additional option to create <a href="http://msdn.microsoft.com/en-us/library/d5zhxt22.aspx">minidump .DMP files</a> for debugged process on user request. This is in particular useful in conjunction with flag choices (on the previous page of the wizard).</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2011/07/Image0032.png"><img class="alignnone size-full wp-image-1249" title="Minidump Creation Link" src="http://alax.info/blog/wp-content/uploads/2011/07/Image0032.png" alt="" width="513" height="400" /></a></p>
<p>Partial Visual C++ .NET 2010 source code is <a href="http://code.assembla.com/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions">available from SVN</a>, release binary included (<a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe?format=raw">Win32</a>, <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe?format=raw">x64</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1248/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LogProcessExceptions: Automatically Create Minidump Files on C++ Exception in Monitored Process</title>
		<link>http://alax.info/blog/1211</link>
		<comments>http://alax.info/blog/1211#comments</comments>
		<pubDate>Wed, 18 May 2011 16:47:41 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[ATL]]></category>
		<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.DMP]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[minidump]]></category>
		<category><![CDATA[support]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1211</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1211" title="LogProcessExceptions: Automatically Create Minidump Files on C++ Exception in Monitored Process"></a>LogProcessExceptions utility implements a very basic debugger which attaches (see DebugActiveProcess on MSDN) to a running process and monitors its exceptions. Once exception takes place the utility creates a minidump file for the process (see MiniDumpWriteDump on MSDN) so that &#8230;<p class="read-more"><a href="http://alax.info/blog/1211">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1211" title="LogProcessExceptions: Automatically Create Minidump Files on C++ Exception in Monitored Process"></a><p>LogProcessExceptions utility implements a very basic debugger which attaches (see <a href="http://msdn.microsoft.com/en-us/library/ms679295%28VS.85%29.aspx">DebugActiveProcess</a> on MSDN) to a running process and monitors its exceptions. Once exception takes place the utility creates a minidump file for the process (see <a href="http://msdn.microsoft.com/en-us/library/ms680360%28VS.85%29.aspx">MiniDumpWriteDump</a> on MSDN) so that exception condition could be investigated off-site using debugger.</p>
<p>If you throw C++ exceptions in your C++ code in exceptional cases which indicate necessity to log the condition and possibly check it later, the utility will get the vital information for the application running at production location in environment without fully featured debugger (note that the utility is a simple download-and-run &#8220;wizard&#8221; style application, with no installation required), or will simply track the error letting the application continue execution without pretty much of an interruption.</p>
<p>The tool will prompt for debuggee process, and follow with minidump type choices and the debugging operation.</p>
<p><img class="alignnone size-full wp-image-1212" title="Log Process Exceptions - Processes" src="http://alax.info/blog/wp-content/uploads/2011/05/Image001.png" alt="" width="513" height="400" /></p>
<p><span id="more-1211"></span></p>
<p><img class="alignnone size-full wp-image-1213" title="Log Process Exceptions - Minidupm Type" src="http://alax.info/blog/wp-content/uploads/2011/05/Image0031.png" alt="" width="513" height="400" /> <img class="alignnone size-full wp-image-1214" title="Log Process Exceptions - Operation" src="http://alax.info/blog/wp-content/uploads/2011/05/Image0041.png" alt="" width="513" height="400" /></p>
<p>Minidump files are created automatically with the name file including:</p>
<ul>
<li>Excecutable Name</li>
<li>Process Identifier</li>
<li>Ordinal Number</li>
<li>Exception Code</li>
<li>C++/ATL Exception HRESULT Code (esp. for <a href="http://msdn.microsoft.com/en-us/library/9b1a94tx%28VS.80%29.aspx">CAtlException</a> exceptions)</li>
</ul>
<p>The utility is expected to work with Windows XP operating system and more recent.</p>
<p>Partial Visual C++ .NET 2010 source code is <a href="http://code.assembla.com/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions">available  from SVN</a>, release binary included (<a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/Win32/Release/LogProcessExceptions.exe?format=raw">Win32</a>,  <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/LogProcessExceptions/_Bin/x64/Release/LogProcessExceptions.exe?format=raw">x64</a>).</p>
<p>Artwork credits: <a href="http://cristoildiablo.deviantart.com/">http://cristoildiablo.deviantart.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1211/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get HMODULE of msvcr90.dll</title>
		<link>http://alax.info/blog/1155</link>
		<comments>http://alax.info/blog/1155#comments</comments>
		<pubDate>Mon, 08 Nov 2010 08:02:11 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[psapi]]></category>
		<category><![CDATA[runtime]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1155</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1155" title="How to get HMODULE of msvcr90.dll"></a>Because Microsoft C++ Runtime library is distributed as a side-by-side assembly, GetModuleHandle and LoadLibrary is no longer available to locate the library within the process. Both API calls would return NULL handle. HMODULE hModule = GetModuleHandle(_T("msvcr90.dll")); HMODULE hModule = LoadLibrary(_T("msvcr90.dll")); &#8230;<p class="read-more"><a href="http://alax.info/blog/1155">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1155" title="How to get HMODULE of msvcr90.dll"></a><p>Because Microsoft C++ Runtime library is distributed as a <a href="http://msdn.microsoft.com/en-us/library/ms229072%28VS.80%29.aspx">side-by-side assembly</a>, <a href="http://msdn.microsoft.com/en-us/library/ms683199%28VS.85%29.aspx">GetModuleHandle</a> and <a href="http://msdn.microsoft.com/en-us/library/ms684175%28VS.85%29.aspx">LoadLibrary</a> is no longer available to locate the library within the process. Both API calls would return NULL handle.</p>
<pre style="color: #000000; background: none repeat scroll 0% 0% #ffffff;"><span style="color: #603000;">HMODULE</span> hModule <span style="color: #808030;">=</span> <span style="color: #400000;">GetModuleHandle</span><span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">msvcr90.dll</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #603000;">HMODULE</span> hModule <span style="color: #808030;">=</span> <span style="color: #400000;">LoadLibrary</span><span style="color: #808030;">(</span>_T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">msvcr90.dll</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span></pre>
<p>Still, how to locate the modules in order to, for example, be able to check DLL version? <a href="http://msdn.microsoft.com/en-us/library/ms684884%28VS.85%29.aspx">Process Status API (PSAPI)</a> can help:</p>
<pre style="color: #000000; background: none repeat scroll 0% 0% #ffffff;"><span style="color: #004a43;">#</span><span style="color: #004a43;">include </span><span style="color: #800000;">&lt;</span><span style="color: #40015a;">psapi.h</span><span style="color: #800000;">&gt;</span>
<span style="color: #004a43;">#</span><span style="color: #004a43; font-weight: bold;">pragma </span><span style="color: #bb7977; font-weight: bold;">comment(lib, </span><span style="color: #0000e6; font-weight: bold;">"psapi.lib"</span><span style="color: #bb7977; font-weight: bold;">)</span>

<span style="color: #808030;">.</span><span style="color: #808030;">.</span><span style="color: #808030;">.</span>

<span style="color: #603000;">DWORD</span> nDataSize <span style="color: #808030;">=</span> <span style="color: #008c00;">4</span> <span style="color: #808030;">&lt;</span><span style="color: #808030;">&lt;</span> <span style="color: #008c00;">10</span><span style="color: #800080;">;</span> <span style="color: #696969;">// 4K</span>
EnumProcessModules<span style="color: #808030;">(</span><span style="color: #400000;">GetCurrentProcess</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> <span style="color: #7d0045;">NULL</span><span style="color: #808030;">,</span> <span style="color: #008c00;">0</span><span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>nDataSize<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
nDataSize <span style="color: #808030;">+</span><span style="color: #808030;">=</span> nDataSize <span style="color: #808030;">/</span> <span style="color: #008c00;">2</span><span style="color: #800080;">;</span>
CTempBuffer<span style="color: #800080;">&lt;</span><span style="color: #603000;">HMODULE</span><span style="color: #800080;">&gt;</span> phModules<span style="color: #800080;">;</span>
ATLENSURE_THROW<span style="color: #808030;">(</span>phModules<span style="color: #808030;">.</span>AllocateBytes<span style="color: #808030;">(</span>nDataSize<span style="color: #808030;">)</span><span style="color: #808030;">,</span> E_OUTOFMEMORY<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span>EnumProcessModules<span style="color: #808030;">(</span><span style="color: #400000;">GetCurrentProcess</span><span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> phModules<span style="color: #808030;">,</span> nDataSize<span style="color: #808030;">,</span> <span style="color: #808030;">&amp;</span>nDataSize<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
<span style="color: #800080;">{</span>
    <span style="color: #800000; font-weight: bold;">const</span> SIZE_T nCount <span style="color: #808030;">=</span> nDataSize <span style="color: #808030;">/</span> <span style="color: #800000; font-weight: bold;">sizeof</span> <span style="color: #808030;">*</span>phModules<span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">for</span><span style="color: #808030;">(</span>SIZE_T nIndex <span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span> nIndex <span style="color: #808030;">&lt;</span> nCount<span style="color: #800080;">;</span> nIndex<span style="color: #808030;">+</span><span style="color: #808030;">+</span><span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        CPath sPath <span style="color: #808030;">=</span> _VersionInfoHelper<span style="color: #800080;">::</span>GetModulePath<span style="color: #808030;">(</span>phModules<span style="color: #808030;">[</span>nIndex<span style="color: #808030;">]</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span> <span style="color: #696969;">// uses GetModuleFileName</span>
        <span style="color: #800000; font-weight: bold;">if</span><span style="color: #808030;">(</span>_tcsicmp<span style="color: #808030;">(</span>FindFileName<span style="color: #808030;">(</span>sPath<span style="color: #808030;">)</span><span style="color: #808030;">,</span> _T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">msvcr90.dll</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #008c00;">0</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #696969;">// NOTE: Here we found it</span>
            ATLTRACE2<span style="color: #808030;">(</span>atlTraceGeneral<span style="color: #808030;">,</span> <span style="color: #008c00;">2</span><span style="color: #808030;">,</span> _T<span style="color: #808030;">(</span><span style="color: #800000;">"</span><span style="color: #0000e6;">msvcr90.dll version is </span><span style="color: #0f69ff;">%s</span><span style="color: #0f69ff;">\n</span><span style="color: #800000;">"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> _VersionInfoHelper<span style="color: #800080;">::</span>GetVersionString<span style="color: #808030;">(</span>_VersionInfoHelper<span style="color: #800080;">::</span>GetFileVersion<span style="color: #808030;">(</span>sPath<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">break</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>
    <span style="color: #800080;">}</span>
<span style="color: #800080;">}</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1155/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ProcessSnapshot: Create process minidump for port-mortem debugging</title>
		<link>http://alax.info/blog/1119</link>
		<comments>http://alax.info/blog/1119#comments</comments>
		<pubDate>Wed, 24 Mar 2010 22:17:42 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.DMP]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[minidump]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[snapshot]]></category>
		<category><![CDATA[Source]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[WTL]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=1119</guid>
		<description><![CDATA[<a href="http://alax.info/blog/1119" title="ProcessSnapshot: Create process minidump for port-mortem debugging"></a>ProcessSnapshot is a utility to take a snapshot of process call stacks, and the snapshot taken is written into a human friendly text file. Additionally to this, the utility has been given a capability to create process minidump files, on &#8230;<p class="read-more"><a href="http://alax.info/blog/1119">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/1119" title="ProcessSnapshot: Create process minidump for port-mortem debugging"></a><p><a href="http://alax.info/blog/665">ProcessSnapshot</a> is a utility to take a snapshot of process call stacks, and the snapshot taken is written into a human friendly text file.</p>
<p><a href="http://alax.info/blog/wp-content/uploads/2010/03/24-Image001.png"><img class="alignnone size-medium wp-image-1120" title="ProcessSnapshot is taking process minidump files" src="http://alax.info/blog/wp-content/uploads/2010/03/24-Image001-320x189.png" alt="ProcessSnapshot is taking process minidump files" width="320" height="189" /></a></p>
<p>Additionally to this, the utility has been given a capability to create process <a href="http://msdn.microsoft.com/en-us/library/ms680369%28VS.85%29.aspx">minidump files</a>, on user request. The minidump files can be used with debugger to analyze the context of the process using feature rich debug environment, esp. Microsoft Visual Studio. To create a minidump for a process, check a corresponding box and press &#8220;Take a Dump&#8221; button. A file named &#8220;&lt;process-image-name&gt; &#8211; &lt;date&gt; &lt;time&gt;.dmp&#8221; will be created in the directory of the utility executable.</p>
<p>See also:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/ms680369%28VS.85%29.aspx">Minidump Files (MSDN)</a></li>
<li><a href="http://support.microsoft.com/kb/315263">How to read the small memory dump files that Windows creates for debugging</a></li>
<li><a href="http://www.codeproject.com/KB/debug/postmortemdebug_standalone1.aspx">Post-Mortem Debugging Your Application with Minidumps and Visual Studio .NET</a></li>
<li><a href="http://www.pchell.com/support/minidumps.shtml">How to View Windows Minidump Files</a></li>
</ul>
<p>A binary [<a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/ProcessSnapshot/Win32/Release/ProcessSnapshot.exe?format=raw">Win32</a>, <a href="http://www.assembla.com/code/roatl-utilities/subversion/nodes/trunk/ProcessSnapshot/x64/Release/ProcessSnapshot.exe?format=raw">x64</a>] and partial Visual C++ .NET 2008 source code <a href="http://trac2.assembla.com/roatl-utilities/browser/trunk/ProcessSnapshot/Release/ProcessSnapshot.exe">are  available from SVN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/1119/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ahead Nero&#8217;s NeResize DirectShow Filter</title>
		<link>http://alax.info/blog/967</link>
		<comments>http://alax.info/blog/967#comments</comments>
		<pubDate>Mon, 29 Jun 2009 22:34:26 +0000</pubDate>
		<dc:creator>Roman</dc:creator>
				<category><![CDATA[Seriously]]></category>
		<category><![CDATA[access violation]]></category>
		<category><![CDATA[ahead]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[crap]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[DirectShow]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[NeResize]]></category>
		<category><![CDATA[nero]]></category>

		<guid isPermaLink="false">http://alax.info/blog/?p=967</guid>
		<description><![CDATA[<a href="http://alax.info/blog/967" title="Ahead Nero&#039;s NeResize DirectShow Filter"></a>Another example of a negligence with a cost of incompatibility and enormous amount of support time. Ahead Nero installs a number of DirectShow filters into $(Program Files)\Common Files\Ahead\DSFilter directory. One of the files is NeResize.ax and it hosts a Nero &#8230;<p class="read-more"><a href="http://alax.info/blog/967">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://alax.info/blog/967" title="Ahead Nero&#039;s NeResize DirectShow Filter"></a><p>Another example of a negligence with a cost of incompatibility and enormous amount of support time. Ahead Nero installs a number of DirectShow filters into <em>$(Program Files)\Common Files\Ahead\DSFilter</em> directory. One of the files is <em>NeResize.ax</em> and it hosts a <em>Nero Resize</em> filter. Let us take a closer look:</p>
<p>CLSID: <strong>{30002E0C-C574-481E-A5DE-90AE54A79E10}</strong> (note that Nero 8 ships the same buggy stuff with new CLSID of <strong>{3D0A27C9-B4D6-487B-AFE4-E3CABD4B81F9}</strong><em> &#8211; 11.05.2010</em>)<br />
Merit: <strong>0&#215;00400000</strong> (<a href="http://msdn.microsoft.com/en-us/library/dd388793(VS.85).aspx">MERIT_UNLIKELY</a>)<br />
Input Pin&#8217;s Media Type: major type GUID_NULL, subtype GUID_NULL<br />
Output Pin&#8217;s Media Type: major type GUID_NULL, subtype GUID_NULL</p>
<p>The filter is clearly a video filter:</p>
<p><img class="alignnone size-full wp-image-969" title="Ahead Nero Resize Filter's Property Page" src="http://alax.info/blog/wp-content/uploads/2009/06/29-image001.png" alt="Ahead Nero Resize Filter's Property Page" width="376" height="292" /></p>
<p>So the filter register itself under a merit that allows taking it during <a href="http://msdn.microsoft.com/en-us/library/dd390342(VS.85).aspx">Intelligent Connect</a>, it registers using media type wildcard which is clearly widely than the filter can affectively operate with and the most interesting part is: with certain video media types the filter crashes (memory access violation) during pin connection negotiation process. That is, inaccurate filter <span style="text-decoration: underline;">may be crashing third party software it has nothing to deal with at all</span>.</p>
<pre>*** Unhandled Exception
Process: 0x000001d4, Thread: 0x00000ce4, Date: 6/29/2009, Time: 11:20:56 AM, Application: C:\Program Files\...
Module: C:\..., Product Version: 1.7.1.1, File Version: 1.7.1.20014, File Time: 23.06.2009, 19:02
Code: 0xc0000005, Flags: 0x00000000, Address: 0x05fc6c65
Parameters: 0x00000001, 0x15be9030

** Call Stack
NeResize!05fc6c65 DllUnregisterServer +21909 @05fc0000
NeResize!05fc7888 DllUnregisterServer +25016 @05fc0000
NeResize!05fc7204 DllUnregisterServer +23348 @05fc0000</pre>
<p>Additionally to that the filter does not allow its insertion in debugging environment, and it seems even with Visual Studio running without a debugging session active. Which means that developer may be unaware of issues until incompatibility comes up at a later stage such as testing, or at production site.</p>
<p>It is not the first Nero filter which is bringing real problems. Basically any user who want to keep his system far from issues while still having Nero installed, needs to do find <em>$(Program Files)\Common Files\Ahead\DSFilter</em> directory and immediately rename it to some <em>~DSFilter</em> in order to invalidate all Nero filters registration.</p>
<p>A few quotes from <a href="http://msdn.microsoft.com/en-us/library/dd388793(VS.85).aspx">Guidelines for Registering Filters</a>:</p>
<p style="padding-left: 30px;">Avoid specifying MEDIATYPE_None, MEDIASUBTYPE_None, or GUID_NULL in the <a id="ctl00_MTContentSelector1_mainContentContainer_ctl04" onclick="javascript:Track('ctl00_MTContentSelector1_mainContentContainer_ctl00|ctl00_MTContentSelector1_mainContentContainer_ctl04',this);" href="http://msdn.microsoft.com/en-us/library/dd373438%28VS.85%29.aspx"><strong>AMOVIESETUP_MEDIATYPE</strong></a> information for a pin. <strong>IFilterMapper2</strong> treats these as wildcards, which can slow the graph-building process.</p>
<p>Nero Resize does specify and obviously slows the system down.</p>
<p style="padding-left: 30px;">Choose the lowest merit value possible. Here are some guidelines:<br />
&#8230;<br />
Special purpose filter; any filter that is created directly by the application: MERIT_DO_NOT_USE</p>
<p>Nero Resize uses higher value and thus affects proper applications.</p>
<p>Software developers will be safer to prevent from <a href="http://msdn.microsoft.com/en-us/library/dd373399(VS.85).aspx">DirectShow Filter Graph Manager</a> considering the buggy filter to be used during <a href="http://msdn.microsoft.com/en-us/library/dd390342(VS.85).aspx">Intelligent Connect</a> by implementing <a href="http://msdn.microsoft.com/en-us/library/dd389376(VS.85).aspx">IAMGraphBuilderCallback</a> interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://alax.info/blog/967/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

