开发者

conditional display in gdb

开发者 https://www.devze.com 2023-03-14 05:08 出处:网络
I\'m using gdb to debug some c++ code. At the moment the code I\'m looking at iterates through an array of pointers, which are either a pointer to some object or a NULL pointer.

I'm using gdb to debug some c++ code. At the moment the code I'm looking at iterates through an array of pointers, which are either a pointer to some object or a NULL pointer.

If I just display list[index]->member it'll complain when list[index] is null. Is there anyway to display the member only if list[index] is not null? I know you can set conditional breakpoints (condition <bp-num>开发者_StackOverflow社区 <exp>) but I'm not sure how that'd help.

The code in question is:

for (int i=0;i<BSIZE*BSIZE;i++){
  if (vms[i]==target) {valid=true; break;}
}

where vms is the array of pointers.


Since display accepts arbitrary expressions, you can try something like the following display command:

display (list[index]) ? list[index]->member : "null"

I'm not sure if that cleans things up well enough for what you want - you'll still get a display, but it won't be a complaint.


Basically the condition works like this:

#include <iostream>

int main() {
        for (int i=0; i<10; ++i) {
                std::cerr << i << std::endl;
        }
}

You can debug it like this:

(gdb) break 5
Breakpoint 1 at 0x100000d0e: file foobar.cpp, line 5.
(gdb) condition 1 i==3
(gdb) r
Starting program: /private/tmp/foobar 
Reading symbols for shared libraries ++. done
0
1
2

Breakpoint 1, main () at foobar.cpp:5
5           std::cerr << i << std::endl;
0

精彩评论

暂无评论...
验证码 换一张
取 消