WinDBG and Vista strange behaviour while debugging driver...any reason why??

Hi All,
I have seen a strange behaviour while debugging
my WDM driver. Here is the description.

I am doing the following calculation to calculate
and then assign pointer:

=====================================================
PDEVICE_EXTENSION pDevExt=ALLOCATED MEMORY; ------1

pDevExt->pHwExten = (PHW_EXTENSION)(pDevExt +

DEVICE_EXTENSION_SIZE); -------------------2

pDevExt->pHwExten->pDevExt = pDevExt;--------3

=====================================================

The step 2 is obviously wrong because I should be
using
a PUCHAR to point my hardware extension pointer [which
is immidiately following my device extension and
allocated properly in IoCreateDevice] to correct
location.

Somehow when I execute the code step by step using
WinDbg I see that the pointers are assigned correctly
[verified in debugger] and my driver works correctly.

As soon as I remove all the breakpoints and let it run
on it’s own, the thing crashes left and right.

I fixed this problem [using a PUCHAR pointer inplace
of pDevExt in step 2] and let the code run and it
works like a champ.

I am not able to think of any explanation regarding
this. So if any one can put some light on it would be
highly appriciated.

Why some code would behave differently in debugger
and differently while not debugging.

Thanks guys,
Really appriciate your help.

  • Driver Coder.

Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

A classic rule of the C language.

Using “+ N” on a pointer of type T* increments the address value by N *
sizeof(T) bytes.
Cast to UCHAR if you need to increment by N bytes, or use ( pDevExt + 1 )

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

----- Original Message -----
From: “Driver Coder”
To: “Windows System Software Devs Interest List”
Sent: Friday, June 02, 2006 10:29 AM
Subject: [ntdev] WinDBG and Vista strange behaviour while debugging
driver…any reason why??

> Hi All,
> I have seen a strange behaviour while debugging
> my WDM driver. Here is the description.
>
> I am doing the following calculation to calculate
> and then assign pointer:
>
> =====================================================
> PDEVICE_EXTENSION pDevExt=ALLOCATED MEMORY; ------1
>
> pDevExt->pHwExten = (PHW_EXTENSION)(pDevExt +
>
> DEVICE_EXTENSION_SIZE); -------------------2
>
> pDevExt->pHwExten->pDevExt = pDevExt;--------3
>
> =====================================================
>
> The step 2 is obviously wrong because I should be
> using
> a PUCHAR to point my hardware extension pointer [which
> is immidiately following my device extension and
> allocated properly in IoCreateDevice] to correct
> location.
>
> Somehow when I execute the code step by step using
> WinDbg I see that the pointers are assigned correctly
> [verified in debugger] and my driver works correctly.
>
> As soon as I remove all the breakpoints and let it run
> on it’s own, the thing crashes left and right.
>
> I fixed this problem [using a PUCHAR pointer inplace
> of pDevExt in step 2] and let the code run and it
> works like a champ.
>
> I am not able to think of any explanation regarding
> this. So if any one can put some light on it would be
> highly appriciated.
>
> Why some code would behave differently in debugger
> and differently while not debugging.
>
> Thanks guys,
> Really appriciate your help.
> - Driver Coder.
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Driver Coder wrote:

Hi All,
I have seen a strange behaviour while debugging
my WDM driver. Here is the description.

I am doing the following calculation to calculate
and then assign pointer:

=====================================================
PDEVICE_EXTENSION pDevExt=ALLOCATED MEMORY; ------1

pDevExt->pHwExten = (PHW_EXTENSION)(pDevExt +

DEVICE_EXTENSION_SIZE); -------------------2

pDevExt->pHwExten->pDevExt = pDevExt;--------3

=====================================================

Going to PUCHAR isn’t strictly necessary. Consider, for example,
letting the compiler help you:

PDEVICE_EXTENSION pDevExt = xxxx;
pDevExt->pHwExten = (PHW_EXTENSION)(pDevExt+1);
pDevExt->pHwExten->pDevExt = pDevExt;

Somehow when I execute the code step by step using
WinDbg I see that the pointers are assigned correctly
[verified in debugger] and my driver works correctly.

I’m dubious. The more likely answer is that you “fixed” one of the
registers by hand, and the forgot about it.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.