Bitwise comparison error (WinDbg questions)

This problem I am having is odd, and I’m having a hell of a time figuring it out. I suspect my inexperience with WinDbg is partially to blame.

I have the following struct:
typedef struct _SERIAL_HANDFLOW {
ULONG ControlHandShake;
ULONG FlowReplace;
LONG XonLimit;
LONG XoffLimit;
} SERIAL_HANDFLOW, *PSERIAL_HANFLOW;

As you can probably tell by the name of the members these get set while changing flow control for a serial device.

I am trying to conditionally check ControlHandShake and FlowReplace and WinDbg is showing the value of ControlHandShake as a single integer as opposed to a HEX value. I don’t know the significance of this, but this is the scenario. WinDbg shows ControlHandShake=9, SERIAL_CTS_HANDSHAKE is defined as ((ULONG)0x08) and I have the following conditional:

if ( HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE == SERIAL_CTS_HANDSHAKE )

I have tried casting the piss out of this and still haven’t been able to make the conditional TRUE.

In my world 0x09 & 0x08 has always resulted in 0x08. What in the world could be the issue here?

Oh on a similar note FlowReplace is being reported by WinDbg as 0x80 and SERIAL_RTS_HANDSHAKE is defined as ((ULONG)0x80) and the following conditional works appropriately:

if ( HandFlow->FlowReplace & SERIAL_RTS_HANDSHAKE == SERIAL_RTS_HANDSHAKE )

When I have this kind of problem I disassemble the code to find out what
the compiler is doing. In this case you need parentheses around the & part
of the statement.

Bill Wandel

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com]
On Behalf Of xxxxx@hotmail.com
Sent: Friday, January 11, 2008 1:38 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Bitwise comparison error (WinDbg questions)

This problem I am having is odd, and I’m having a hell of a time figuring it
out. I suspect my inexperience with WinDbg is partially to blame.

I have the following struct:
typedef struct _SERIAL_HANDFLOW {
ULONG ControlHandShake;
ULONG FlowReplace;
LONG XonLimit;
LONG XoffLimit;
} SERIAL_HANDFLOW, *PSERIAL_HANFLOW;

As you can probably tell by the name of the members these get set while
changing flow control for a serial device.

I am trying to conditionally check ControlHandShake and FlowReplace and
WinDbg is showing the value of ControlHandShake as a single integer as
opposed to a HEX value. I don’t know the significance of this, but this is
the scenario. WinDbg shows ControlHandShake=9, SERIAL_CTS_HANDSHAKE is
defined as ((ULONG)0x08) and I have the following conditional:

if ( HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE ==
SERIAL_CTS_HANDSHAKE )

I have tried casting the piss out of this and still haven’t been able to
make the conditional TRUE.

In my world 0x09 & 0x08 has always resulted in 0x08. What in the world
could be the issue here?


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

I’m sorry that was a typo on my part. This is what I have:

if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) == SERIAL_CTS_HANDSHAKE )

Is SERIAL_CTS_HANDSHAKE defined with #define, or is a actual const
unsigned long int? If the former, I would try:

#define SERIAL_CTS_HANDSHAKE 8UL

mm

xxxxx@hotmail.com wrote:

I’m sorry that was a typo on my part. This is what I have:

if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) == SERIAL_CTS_HANDSHAKE )

SERIAL_CTS_HANDSHAKE is actually defined in ntddser.h in the …\inc\api directory of the WDK and is defined as:

#define SERIAL_CTS_HANDSHAKE ((ULONG)0x08)

xxxxx@hotmail.com wrote:


I am trying to conditionally check ControlHandShake and FlowReplace and WinDbg is showing the value of ControlHandShake as a single integer as opposed to a HEX value. I don’t know the significance of this, but this is the scenario. WinDbg shows ControlHandShake=9, SERIAL_CTS_HANDSHAKE is defined as ((ULONG)0x08) and I have the following conditional:

if ( HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE == SERIAL_CTS_HANDSHAKE )

I have tried casting the piss out of this and still haven’t been able to make the conditional TRUE.

In my world 0x09 & 0x08 has always resulted in 0x08. What in the world could be the issue here?

Ah, children these days. They haven’t yet learned the pains of
unexpected operator precedence. In C, == binds tighter than &, so your
expression is read as:

if ( HandFlow->ControlHandShake & (SERIAL_CTS_HANDSHAKE ==
SERIAL_CTS_HANDSHAKE) )

Still, since the result of the == is 1, I would have expected that to
take the “if”, but that’s not the point. Parentheses are your friend;
they don’t cost anything, and they make sure your expressions always say
what you mean:

if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ==
SERIAL_CTS_HANDSHAKE )


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

Tim,

I am usually a little overly paren-happy, however, I erroneously typed my conditional in the first message.

As you may see in a following post I actually am using the following:

if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ==
SERIAL_CTS_HANDSHAKE )

I wish I could be offended by the children comment, but I’ve been caught myself doing foolishness of the like too often.

“Tim Roberts” wrote in message news:xxxxx@ntdev…
>
> Ah, children these days …
>
> if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ==
> SERIAL_CTS_HANDSHAKE )
>

That’s what I would think but that’s exactly what he after said:
if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ==
SERIAL_CTS_HANDSHAKE )

My best guess is he needs to reload symbols for his module or so.

/Daniel

I don’t have a solution to your problem, but you can replace
“if ((HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) == SERIAL_CTS_HANDSHAKE)”
with
“if (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE)”.

I don’t think it actually makes a difference in this case, because the optimizing compiler probably produces the same code.

You made my a little suspicious about needing to reload my symbols, so I added a couple lines to be entirerly explicit, rebooted, restarted WinDbg, and reloaded the symbols. Here are the changes made:

ULONG currFlowReplace = ((ULONG)HandFlow->FlowReplace) & SERIAL_RTS_MASK;
ULONG currControlHandShake = ((ULONG)HandFlow->ControlHandShake) & SERIAL_CTS_HANDSHAKE;
ULONG ctshandshake = ((ULONG)0x08); // Just did this b/c I was nervous SERIAL_CTS_HANDSHAKE may have been getting redefined someplace

if ( currFlowReplace == SERIAL_RTS_HANDSHAKE )
{…}
else if ( currControlHandShake == ctshandshake )
{…}

This is truely madening because if I break following the ctshandshake assignment and step down to my else if conditional and use WinDbg to watch currControlHandShake and ctshandshake, it shows each as: “unsigned long 8”. However the conditional fails.

If you have any other suggestions, I’m willing to try just about anything at this point.

Is the lhs side of that expression (ControlHandShake) actually a ULONG?

The facts presented don’t seen to make sense.

On Jan 11, 2008 2:34 PM, Martin O’Brien wrote:

> Is SERIAL_CTS_HANDSHAKE defined with #define, or is a actual const
> unsigned long int? If the former, I would try:
>
> #define SERIAL_CTS_HANDSHAKE 8UL
>
> mm
>
>
>
>
> xxxxx@hotmail.com wrote:
> > I’m sorry that was a typo on my part. This is what I have:
> >
> > if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ==
> SERIAL_CTS_HANDSHAKE )
> >
> >
> >
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>


Mark Roddy

okay… I’m making some progress, but still haven’t figured it out.

If I reverse the order of my if(), else if() statements it’s always the else if() statment that fails even if I make it else if (1). So, it is independent on the condition, and my suspicion of the whole ‘unsigned long 8’ stuff wound up to be a pile of crap as expected.

However, now my confusion only grows worse as now it seems it’s a problem executing an if, else if, else block. I grossly oversimplified it to this:

if ( some condition that I’ve already been getting into successfully){
if (1){
DbgPrint(“Made it into the if condition\n”);
}
else if (1){
DbgPrint(“Made it into the else if condition\n”);
}
else{
DbgPrint(“Made it into the else condition\n”);
}

DbgPrint(“Made it to the end\n”);
}

Even like this my else if doesn’t execute. When I step over the code it goes straight from DbgPrint(“Made it into the if condition\n”); to DbgPrint(“Made it to the end\n”);

So, something is officially whack. I don’t know if it’s trying to reload old code, or what the deal is exactly. But luckily I have another disk image sitting here that I can try on. I’ll let you know what I find.

Shane Corbin wrote:

Even like this my else if doesn’t execute. When I step over the code it goes
straight from DbgPrint(“Made it into the if condition\n”); to DbgPrint(“Made
it to the end\n”);

That’s the expected and right behaviour.
You seem to want this instead:

if ( some condition that I’ve already been getting into successfully){
if (1){
DbgPrint(“Made it into the if condition\n”);
}

if (1){
DbgPrint(“Made it into the (else) if condition\n”);
}
else{
DbgPrint(“Made it into the else condition\n”);
}

DbgPrint(“Made it to the end\n”);
}

Son of a gun. You know what… You’re right. I had set it up to test them independently from each other, but when I was running the test was setting both RTS and CTS control. Of course I’m only going to get into one of them this way.

What an incredible waste of time. It’s not the waste of my time that troubles me, but rather the waste of the folks that have been helping me.

Thanks again to those that offered insight. I dropped the ball on this one, don’t hold it against me; haha.

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of xxxxx@hotmail.com[SMTP:xxxxx@hotmail.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, January 11, 2008 9:39 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Bitwise comparison error (WinDbg questions)

If you have any other suggestions, I’m willing to try just about anything at this point.

As already suggested, go down to assembly level. Step to condition, open disassembly window and continue there checking instruction and registers after every single step. You can have incorrectly generated code (unlikely), wrong symbols or maybe WinDbg is just confused because of optimization (possible with release code). Maybe you should put debug print statements there to verify how code was exactly executed and maybe you’ll see everything works as expected.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of xxxxx@hotmail.com[SMTP:xxxxx@hotmail.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, January 11, 2008 10:22 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Bitwise comparison error (WinDbg questions)

okay… I’m making some progress, but still haven’t figured it out.

If I reverse the order of my if(), else if() statements it’s always the else if() statment that fails even if I make it else if (1). So, it is independent on the condition, and my suspicion of the whole ‘unsigned long 8’ stuff wound up to be a pile of crap as expected.

However, now my confusion only grows worse as now it seems it’s a problem executing an if, else if, else block. I grossly oversimplified it to this:

if ( some condition that I’ve already been getting into successfully){
if (1){
DbgPrint(“Made it into the if condition\n”);
}
else if (1){
DbgPrint(“Made it into the else if condition\n”);
}
else{
DbgPrint(“Made it into the else condition\n”);
}

DbgPrint(“Made it to the end\n”);
}

Even like this my else if doesn’t execute. When I step over the code it goes straight from DbgPrint(“Made it into the if condition\n”); to DbgPrint(“Made it to the end\n”);

And what else would you expect?!

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

xxxxx@hotmail.com wrote:

I am usually a little overly paren-happy, however, I erroneously typed my conditional in the first message.

As you may see in a following post I actually am using the following:

if ( (HandFlow->ControlHandShake & SERIAL_CTS_HANDSHAKE) ==
SERIAL_CTS_HANDSHAKE )

Yes, I saw that too late. And actually, as I pointed out, in this case
it shouldn’t have made a difference. If ControlHandShake was really
0x09, then the comparison would have been true, which is a 1, and 9 & 1
is true.

Now, this WOULD fail if you had accidentally typed && instead of &. Is
that possible?


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

xxxxx@hotmail.com wrote:

okay… I’m making some progress, but still haven’t figured it out.

If I reverse the order of my if(), else if() statements it’s always the else if() statment that fails even if I make it else if (1). So, it is independent on the condition, and my suspicion of the whole ‘unsigned long 8’ stuff wound up to be a pile of crap as expected.

However, now my confusion only grows worse as now it seems it’s a problem executing an if, else if, else block. I grossly oversimplified it to this:

if ( some condition that I’ve already been getting into successfully){
if (1){
DbgPrint(“Made it into the if condition\n”);
}
else if (1){
DbgPrint(“Made it into the else if condition\n”);
}
else{
DbgPrint(“Made it into the else condition\n”);
}

DbgPrint(“Made it to the end\n”);
}

Even like this my else if doesn’t execute. When I step over the code it goes straight from DbgPrint(“Made it into the if condition\n”); to DbgPrint(“Made it to the end\n”);

Right, that’s exactly what I would expect. Are you saying that’s NOT
what you expect? Remember that what you wrote is equivalent to this:

if ( some condition that I’ve already been getting into successfully){
if (1){
DbgPrint(“Made it into the if condition\n”);
}
else
{
if (1){
DbgPrint(“Made it into the else if condition\n”);
}
else{
DbgPrint(“Made it into the else condition\n”);
}
}

DbgPrint(“Made it to the end\n”);
}


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