Single step execution using a callback between two asm commands

J

Johannes Röckert

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
 
J

Jacek Dziedzic

Johannes said:
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.
 
J

Johannes Röckert

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


Johannes said:
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.
 
M

Moonlit

Hi


Johannes Röckert said:
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




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
 
J

Jacek Dziedzic

Johannes said:
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.
 
J

Johannes Röckert

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top