Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

c - Continuously update watchpoint while traversing linked list gdb

Lets say I have the following simplified code to loop through a linked list. Each node has a data pointer and next pointer.

1 while (pNode->next != NULL)
2 {
3    pNode->data = newData;
4
5    pNode = pNode->next;
6 }

How would I go about automatically setting up a watchpoint on the address of the current pNode->data and update it every time pNode changes? So have something like on line 1 delete the watchpoint and set a new watchpoint on pNode->data. And then since this doesn't happen always I would need to clear all watchpoints after so I don't halt unnecessarily so on line 7 clear all watchpoints.

This code is running in a multithreaded application and pNode list is shared so I am trying to find out who is changing my pNode->data between lines 3 and 5. I do have a latch that should be preventing changing the list while I iterate but because of this error we must be changing it somewhere without the latch and I want to find out where.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am trying to find out who is changing my pNode->data between lines 3 and 5.

What makes you think that data is getting modified between lines 3 and 5?
If the list is shared, pNode->data could be modified 5 iterations later, or when the loop is already done.

In general, debugging multithreaded programs with debugger is hard. You really need to prove that the program is correct by construction, that is: you need to confirm that every time pNode->data is modified, some mutex is held (I assume that's what you meant by "latch").

That said, you can achieve what you asked for.

Find the address of instruction immediately after line 3 ends, and set a breakpoint on that instruction (use b *0xaddress to do so). Attach commands to that breakpoint:

commands 1
silent
watch -l pNode->data
cont
end

Likewise, find the instruction just before pNode is assigned on line 5 and set a breakpoint there. Attach commands:

commands 2
silent
delete $bpnum
cont
end

This uses $bpnum convenience variable, which is set to the number of breakpoint that was last set.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...