1

Are there any analog of the C++ this keyword, that can be used in natvis expressions when debugging C code?

I would like to do the following and have no mind how to do it without this.

Consider we have some struct in C:

typedef struct
{
  int state;
  //other fields follow
}TCB;

Also there is global variable:

TCB* Running;

I want to make natvis rules that will show the state of the object based on the state field and Running variable. If it was C++, I would write:

<Type Name="TCB">
  <DisplayString Condition="state==0">Empty</DisplayString>
  <DisplayString Condition="state==0x80 && Running!=this">Ready</DisplayString>
  <DisplayString Condition="state==0x80 && Running==this">Running</DisplayString>
</Type>

How it can be done in C?

Thanks!
P.S. natvis file is used in VSCode with gdb debugger.

Oleg Skydan
  • 681
  • 1
  • 5
  • 5
  • Since there are no objects in C, there is no (need for) `this`, either. In C++/Java, `this` is used to refer to the object using one of its methods. In C, all functions are global and not associated with structures. – DYZ Mar 04 '17 at 03:53
  • I know how `this` is used in C++/Java :), and I understand it is useless for C language, but we are talking here about natvis files for debugger, not C language expressions. I have shown a part of natvis file that needed `this` keyword. Do you know how to do it without `this`? – Oleg Skydan Mar 05 '17 at 06:26

1 Answers1

2

OK. I have found an answer for my question. We can reference the struct using a hack - the struct address is the same as the address of its first field, and all fields are available in conditinal expressions in conditional expressions in Natvis rules. So now I have the rules:

<Type Name="TCB">
    <DisplayString Condition="state==0">Empty</DisplayString>
    <DisplayString Condition="state==0x80 &amp;&amp; 'Kernel.c'::Running==&amp;state">Running</DisplayString>
    <DisplayString Condition="state==0x80">Ready</DisplayString>
    <DisplayString Condition="state==0x81">Suspended</DisplayString>
    <DisplayString Condition="state==0x84">Waiting any ev {psp.r1_0,x}</DisplayString>
    <DisplayString Condition="state==0x82">Waiting all ev {psp.r1_0,x}</DisplayString>
    <DisplayString Condition="state==0x88">Blocked by {(cs_t*)psp.r0}</DisplayString>
    <DisplayString Condition="state==0x90">Waiting for {(semaphore_t*)psp.r0}</DisplayString>
</Type>

The second rule do exactly what I need (and what I asked before). It compares the addres of the currently displayed object with the global variable Running located in the Kernel.c file.

@DYZ Here is the picture (picture of the VSCode watch window) how VS Code displays RTOS tasks information using the rules similier to the above ones (sorry I have now reputation to post pictures directly in the answer). I think now you understand why I needed such feature and why having 'this' pointer would be useful in such case.

Teivaz
  • 5,462
  • 4
  • 37
  • 75
Oleg Skydan
  • 681
  • 1
  • 5
  • 5