16

いくつかの基本クラス A と 2 つの派生クラス B および C があるとします。クラス A には f() というメソッドがあります。

私の 'this' が実際にクラス C のインスタンスである場合にのみヒットされる Visual Studio の A::f() に条件付きブレークポイントを設定する方法はありますか?

例えば


    void A::f()
    {
    some code and a breakpoint 
    }

    void foo(A* a)
    {
       a->f();
    }

    void bar()
    {
       A a;
       B b;
       C c;
       foo(&a); // breakpoint isn't hit
       foo(&b); // breakpoint isn't hit
       foo(&c); // breakpoint is hit
    }

ブレークポイントの状態で仮想テーブルポインターをテストすることでそれを達成することができましたが、より良い(より簡単な)方法が必要です。

前もって感謝します。

編集:コメントで提案されているようにソースコードを変更することは、私が探している解決策ではありません。これは、VC++ デバッガーを使用してのみ実行する必要があります。

4

4 に答える 4

6

あなたはできる。まず、チェックしたいタイプの仮想テーブルのアドレスを決定します。次に、 (arg).__vfptr != 0x01188298 のような条件でブレークポイントを設定します。ここで、0x01188298 はタイプ vtable のアドレスです。それでおしまい。

于 2011-09-13T12:41:23.760 に答える
3

これがdynamic_cast目的です。

void A::f() {
    ...
    if (dynamic_cast<C*>(this)) {
        __debugbreak();
    }
    ...
}

デバッガーで実行する必要がある場合は、中断dynamic_castし、イミディエイト ウィンドウで型を使用して確認し、そうでない場合は続行する必要があります。

于 2011-07-09T15:08:09.073 に答える
1

At the call site (i.e. where foo call is being made), it is nearly impossible to do that. In the function itself, you may do this by having a virtual function, one of them would return true. All others would return false. If function returns true, you can call DebugBreak function itself. Or put its return value into some bool variable, and set conditional breakpoint. Yes, of course, this requires a virtual function to be added in all classes (or some). Additionally you can have one simple bool variable in base class, which will be assigned by derived class. The appropriate derived class (C in your case) can assign it as true. You can do this virtual/variable stuff in debug mode only using _DEBUG macro.

Another solution is to have a macro, which would call foo function. The implementation requires a virtual-function/member-variable in base class as described above, but foo won't be modified.

#define CALL_FOO(obj) \
  if(obj->member_variable_test) DebugBreak(); \
  foo(&obj);

And call CALL_FOO in place of foo:

CALL_FOO(&c); // breakpoint is hit

Though, this maybe unacceptable, but works. And breakpoint is hit exactly where you need! You can make this macro work only in debug-build.

于 2011-07-09T04:43:48.527 に答える
1

この投稿は、Visual Studio でのプログラムによるデバッグの可能性を示唆しているようです。

または、gcc でソースをコンパイルできる場合、または何らかの方法で gdb を利用できる場合は、python スクリプト機能を利用して複雑な条件付きブレークポイントを作成できます。

ここにいくつかの gdb python チュートリアルがあります。 関連する gdb ドキュメントは次のとおりです。

于 2011-07-09T14:45:15.833 に答える