我在使用基本 Valgrind 工具时出现意外行为。对于基本块的每个 Put 语句,我使用 unsafeIRDirty_0_N 注册一个脏助手。问题是脏助手被调用的次数比预期的要多,例如,一个基本块中有 5 个 Put 语句,但脏助手被调用了 13 次。我需要在我的工具中使用脏帮手,但我现在被卡住了,任何帮助都将不胜感激。
这是代码:
unsigned int g_Put_cnt; // the number of Put statements in the basic block
unsigned int g_helper_Put_cnt; // the number of times helper_Put() was
called
static VG_REGPARM(0) void helper_print_counters()
{
VG_(printf)("g_Put_cnt: %d - g_helper_Put_cnt: %d (%d)\n", g_Put_cnt,
g_helper_Put_cnt, g_helper_Put_cnt-g_Put_cnt);
}
static VG_REGPARM(0) void helper_Put()
{
g_helper_Put_cnt++;
}
static
IRSB* fz_instrument ( VgCallbackClosure* closure,
IRSB* sb_in,
VexGuestLayout* layout,
VexGuestExtents* vge,
IRType gWordTy, IRType hWordTy )
{
IRSB* sb_out;
Int i;
if (gWordTy != hWordTy) {
/* We don't currently support this case. */
VG_(tool_panic)("host/guest word size mismatch");
}
/* Set up sb_out */
sb_out = deepCopyIRSBExceptStmts(sb_in);
// Copy verbatim any IR preamble preceding the first IMark
i = 0;
while (i < sb_in->stmts_used && sb_in->stmts[i]->tag != Ist_IMark) {
addStmtToIRSB(sb_out, sb_in->stmts[i]);
i++;
}
g_Put_cnt = 0;
g_helper_Put_cnt = 0;
for (/*use current i*/; i < sb_in->stmts_used; i++)
{
IRStmt* st = sb_in->stmts[i];
IRDirty* di;
if (!st)
continue;
switch (st->tag)
{
case Ist_Put:
di = unsafeIRDirty_0_N(0,
"helper_Put",
VG_(fnptr_to_fnentry)(&helper_Put),
mkIRExprVec_0());
addStmtToIRSB(sb_out, IRStmt_Dirty(di));
g_Put_cnt++;
break;
}
addStmtToIRSB(sb_out, st);
}
IRDirty* di = unsafeIRDirty_0_N(0, "helper_print_counters",
VG_(fnptr_to_fnentry)(&helper_print_counters), mkIRExprVec_0());
addStmtToIRSB(sb_out, IRStmt_Dirty(di));
return sb_out;
}
和输出:
g_Put_cnt: 22 - g_helper_Put_cnt: 22 (0)
g_Put_cnt: 21 - g_helper_Put_cnt: 21 (0)
g_Put_cnt: 8 - g_helper_Put_cnt: 8 (0)
g_Put_cnt: 5 - g_helper_Put_cnt: 13 (8)
g_Put_cnt: 5 - g_helper_Put_cnt: 18 (13)
g_Put_cnt: 6 - g_helper_Put_cnt: 6 (0)
g_Put_cnt: 6 - g_helper_Put_cnt: 6 (0)
g_Put_cnt: 9 - g_helper_Put_cnt: 9 (0)
g_Put_cnt: 9 - g_helper_Put_cnt: 22 (13)
g_Put_cnt: 9 - g_helper_Put_cnt: 35 (26)
g_Put_cnt: 9 - g_helper_Put_cnt: 48 (39)
g_Put_cnt: 9 - g_helper_Put_cnt: 61 (52)
g_Put_cnt: 9 - g_helper_Put_cnt: 74 (65)
g_Put_cnt: 9 - g_helper_Put_cnt: 87 (78)
g_Put_cnt: 9 - g_helper_Put_cnt: 100 (91)
g_Put_cnt: 9 - g_helper_Put_cnt: 113 (104)
g_Put_cnt: 9 - g_helper_Put_cnt: 126 (117)
g_Put_cnt: 9 - g_helper_Put_cnt: 139 (130)
g_Put_cnt: 9 - g_helper_Put_cnt: 152 (143)
g_Put_cnt: 9 - g_helper_Put_cnt: 157 (148)
...
先感谢您。