Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...