From: Glynn Clements <glynn@gclements.plus.com>
To: A M <alim1993@yahoo.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Access to Program Counter in C
Date: Fri, 19 Nov 2004 16:03:24 +0000 [thread overview]
Message-ID: <16798.6476.77458.178618@cerise.gclements.plus.com> (raw)
In-Reply-To: <20041116163821.61564.qmail@web51902.mail.yahoo.com>
A M wrote:
> Does anybody know how to access the address of the
> current executing instruction in C while the program
> is executing?
You can write a function which will return the saved EIP, i.e. the
address of the instruction immediately following the "call"
instruction:
#include <stdio.h>
static void *get_eip(int dummy)
{
return *(void **)((char *)&dummy - 4);
}
int main(void)
{
void *eip = get_eip(0);
printf("%p\n", eip);
return 0;
}
This relies upon the fact that the saved EIP is immediately below the
first argument on the stack.
Test run:
Value returned is $1 = (void *) 0x80483a8
> disassemble main
Dump of assembler code for function main:
0x0804838c <main+0>: push %ebp
0x0804838d <main+1>: mov %esp,%ebp
0x0804838f <main+3>: sub $0x18,%esp
0x08048392 <main+6>: and $0xfffffff0,%esp
0x08048395 <main+9>: mov $0x0,%eax
0x0804839a <main+14>: sub %eax,%esp
0x0804839c <main+16>: movl $0x0,(%esp,1)
0x080483a3 <main+23>: call 0x8048384 <get_eip>
==> 0x080483a8 <main+28>: mov %eax,0xfffffffc(%ebp)
0x080483ab <main+31>: mov 0xfffffffc(%ebp),%eax
0x080483ae <main+34>: mov %eax,0x4(%esp,1)
0x080483b2 <main+38>: movl $0x80484e4,(%esp,1)
0x080483b9 <main+45>: call 0x80482a8
0x080483be <main+50>: mov $0x0,%eax
0x080483c3 <main+55>: leave
0x080483c4 <main+56>: ret
End of assembler dump.
--
Glynn Clements <glynn@gclements.plus.com>
next prev parent reply other threads:[~2004-11-19 16:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-11-16 16:38 Access to Program Counter in C A M
2004-11-19 6:32 ` sandeep
2004-11-19 11:30 ` Brian Raiter
2004-11-19 16:03 ` Glynn Clements [this message]
-- strict thread matches above, loose matches on Subject: below --
2004-11-19 7:31 siddharth vora
2004-11-19 8:03 ` Justinas
2004-11-19 8:04 ` sandeep
2004-11-19 7:58 siddharth vora
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=16798.6476.77458.178618@cerise.gclements.plus.com \
--to=glynn@gclements.plus.com \
--cc=alim1993@yahoo.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.