Single step execution using a callback between two asm commands

Discussion in 'C++' started by Johannes Röckert, Sep 29, 2006.

  1. Hi,

    can anyone help me on how to develop a program in c++ which does single step
    execution using a callback between the asm commands? I need something like
    debuggers do - a callback function should be called after each command.

    I think the resulting code could be fairly small and look somehow like this:

    static int numCommands = 0; // Var which stores the number of commands

    void callback()
    {
    numCommands++; // Increase the command count
    }

    int main()
    {
    enable_single_step_execution(callback); // Enable single step feature and
    set callback
    for (int i = 0; i < 100; i++); // callback() should be now called after
    each asm command
    disable_single_step_execution(); // Disable feature
    cout << "The loop consisted of " << numCommands << " asm commands!" <<
    endl;
    }

    Thanks and regards,
    - Jo
     
    Johannes Röckert, Sep 29, 2006
    #1
    1. Advertising

  2. Re: [OT] Single step execution using a callback between two asm commands

    Johannes Röckert wrote:
    > Hi,
    >
    > can anyone help me on how to develop a program in c++ which does single step
    > execution using a callback between the asm commands? I need something like
    > debuggers do - a callback function should be called after each command.
    >
    > I think the resulting code could be fairly small and look somehow like this:
    >
    > static int numCommands = 0; // Var which stores the number of commands
    >
    > void callback()
    > {
    > numCommands++; // Increase the command count
    > }
    >
    > int main()
    > {
    > enable_single_step_execution(callback); // Enable single step feature and
    > set callback
    > for (int i = 0; i < 100; i++); // callback() should be now called after
    > each asm command
    > disable_single_step_execution(); // Disable feature
    > cout << "The loop consisted of " << numCommands << " asm commands!" <<
    > endl;
    > }


    This cannot be done in a portable manner, so you'll
    need a platform-specific solution. Unfortunately you
    forgot to tell what platform and operating system
    you are interested in.

    I'd say such thing would be rather difficult to accomplish,
    why not use a profiler instead? Some platforms, like most
    of the newel Intels have hardware instruction counters,
    maybe these will provide the functionality you're after?

    HTH,
    - J.
     
    Jacek Dziedzic, Sep 29, 2006
    #2
    1. Advertising

  3. Re: [OT] Single step execution using a callback between two asm commands

    Hi,

    thank you for answering.

    I am looking for a portable solution concerning the OS but for now, windows
    and/or linux (preferred linux) would be sufficient. And I'm using Intel
    CPUs.

    Could you post a link to such a profiler library?

    As far as i found out, telling the cpu to automatically trigger a software
    interrupt is possible. I think it is possible to catch this interrupt but
    the only question is how to tell the cpu to do so. Perhaps, i should check a
    debuggers source code :-/

    The instruction pointer would only help me if the software made a break
    after each command.......

    I would be thankful for further help...

    Regards,
    - Jo


    "Jacek Dziedzic" <jacek@no_spam.tygrys.no_spam.net> schrieb im Newsbeitrag
    news:82b3$451d2d9b$57ceced6$...
    Johannes Röckert wrote:
    > Hi,
    >
    > can anyone help me on how to develop a program in c++ which does single
    > step execution using a callback between the asm commands? I need something
    > like debuggers do - a callback function should be called after each
    > command.
    >
    > I think the resulting code could be fairly small and look somehow like
    > this:
    >
    > static int numCommands = 0; // Var which stores the number of commands
    >
    > void callback()
    > {
    > numCommands++; // Increase the command count
    > }
    >
    > int main()
    > {
    > enable_single_step_execution(callback); // Enable single step feature
    > and set callback
    > for (int i = 0; i < 100; i++); // callback() should be now called after
    > each asm command
    > disable_single_step_execution(); // Disable feature
    > cout << "The loop consisted of " << numCommands << " asm commands!" <<
    > endl;
    > }


    This cannot be done in a portable manner, so you'll
    need a platform-specific solution. Unfortunately you
    forgot to tell what platform and operating system
    you are interested in.

    I'd say such thing would be rather difficult to accomplish,
    why not use a profiler instead? Some platforms, like most
    of the newel Intels have hardware instruction counters,
    maybe these will provide the functionality you're after?

    HTH,
    - J.
     
    Johannes Röckert, Sep 29, 2006
    #3
  4. Johannes Röckert

    Moonlit Guest

    Re: [OT] Single step execution using a callback between two asm commands

    Hi


    "Johannes Röckert" <> wrote in message
    news:efjrvo$8fj$-ilmenau.de...
    > Hi,
    >
    > thank you for answering.
    >
    > I am looking for a portable solution concerning the OS but for now,
    > windows and/or linux (preferred linux) would be sufficient. And I'm using
    > Intel CPUs.
    >
    > Could you post a link to such a profiler library?
    >
    > As far as i found out, telling the cpu to automatically trigger a software
    > interrupt is possible. I think it is possible to catch this interrupt but

    Triggering a software interrupt could be done by

    In visual C++ I often use it to break on a certain condition:
    if( A || B ) __asm int 3;

    AFAIK this is also what a debugger inserts into your code when inserting a
    breakpoint. Then when the breakpoint is hit it will insert the original
    code.

    > the only question is how to tell the cpu to do so. Perhaps, i should check
    > a debuggers source code :-/
    >
    > The instruction pointer would only help me if the software made a break
    > after each command.......
    >
    > I would be thankful for further help...
    >
    > Regards,
    > - Jo
    >
    >
    > "Jacek Dziedzic" <jacek@no_spam.tygrys.no_spam.net> schrieb im Newsbeitrag
    > news:82b3$451d2d9b$57ceced6$...
    > Johannes Röckert wrote:
    >> Hi,
    >>
    >> can anyone help me on how to develop a program in c++ which does single
    >> step execution using a callback between the asm commands? I need
    >> something like debuggers do - a callback function should be called after
    >> each command.
    >>
    >> I think the resulting code could be fairly small and look somehow like
    >> this:
    >>
    >> static int numCommands = 0; // Var which stores the number of commands
    >>
    >> void callback()
    >> {
    >> numCommands++; // Increase the command count
    >> }
    >>
    >> int main()
    >> {
    >> enable_single_step_execution(callback); // Enable single step feature
    >> and set callback
    >> for (int i = 0; i < 100; i++); // callback() should be now called
    >> after each asm command
    >> disable_single_step_execution(); // Disable feature
    >> cout << "The loop consisted of " << numCommands << " asm commands!" <<
    >> endl;
    >> }

    >
    > This cannot be done in a portable manner, so you'll
    > need a platform-specific solution. Unfortunately you
    > forgot to tell what platform and operating system
    > you are interested in.
    >
    > I'd say such thing would be rather difficult to accomplish,
    > why not use a profiler instead? Some platforms, like most
    > of the newel Intels have hardware instruction counters,
    > maybe these will provide the functionality you're after?
    >
    > HTH,
    > - J.
    >


    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl
     
    Moonlit, Sep 29, 2006
    #4
  5. Re: [OT] Single step execution using a callback between two asm commands

    Johannes Röckert wrote:
    > Hi,
    >
    > thank you for answering.
    >
    > I am looking for a portable solution concerning the OS but for now, windows
    > and/or linux (preferred linux) would be sufficient. And I'm using Intel
    > CPUs.
    >
    > Could you post a link to such a profiler library?


    This is a profiler program that, at least on Intel Itanium 2,
    does count single processor ticks:

    http://www.ncsa.uiuc.edu/UserInfo/Resources/Software/Tools/pfmon/

    > As far as i found out, telling the cpu to automatically trigger a software
    > interrupt is possible.


    Sure, the single-step mode. But I suppose you'd need to be
    in kernel mode to do be able to turn it on (I guess).

    > I think it is possible to catch this interrupt but
    > the only question is how to tell the cpu to do so.


    If you are concerned with Intel x86, I vaguely remember that
    there's a flag in the flags register that does just that.
    I even more vaguely recall that it is a little tricky to use,
    because you don't want your "debugger" code to be single-stepped
    and switching between the two modes is tricky.

    > Perhaps, i should check a
    > debuggers source code :-/
    >
    > The instruction pointer would only help me if the software made a break
    > after each command.......


    What happens if an interrupt or a thread switch occurs while
    your loop is being executed? Suddenly there are someone else's
    instructions executed... You want them single-stepped or no?

    > I would be thankful for further help...


    Unfortunately I don't have enough knowledge on this.
    Also, this is not the right newsgroup to ask, because we
    only deal with standard C++ here. You have a system-specific
    problem and your coding it in C++ is only secondary.
    I don't know a proper newsgroup, however.

    HTH,
    - J.
     
    Jacek Dziedzic, Sep 30, 2006
    #5
  6. Re: [OT] Single step execution using a callback between two asm commands

    Hi,

    thanks for help.

    I've found another solution: Using Java and the JVM Tool Interface which
    allows single step execution of java (byte) code. It's been a hard work to
    get it running but now, my Java Code calls a C++-Callback-Function which was
    linked into a shared object. It is nearly exactly what I wanted. The only
    difference is that i wanted to count ASM commands instead of Java Byte Code
    commands.

    I got off a C++ solution because as if you said, there is no platform and
    hardware independend solution.
    int 3 does not help me because I don't know the offset where to post this
    interrupt command...

    Best regards,
    Jo
     
    Johannes Röckert, Sep 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jaap de verwant slachter
    Replies:
    0
    Views:
    1,266
    jaap de verwant slachter
    Jul 1, 2003
  2. Roy in

    need step by step example

    Roy in, Aug 3, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    360
    Roy in
    Aug 3, 2003
  3. Inline asm commands

    , Oct 7, 2005, in forum: C Programming
    Replies:
    7
    Views:
    362
    Mabden
    Oct 15, 2005
  4. Lakota2k
    Replies:
    0
    Views:
    129
    Lakota2k
    Apr 14, 2005
  5. anne001
    Replies:
    7
    Views:
    160
    Logan Capaldo
    Nov 21, 2005
Loading...

Share This Page