Re: Accessing program counter registers from within C or Aseembler.

From: Valdis . Kletnieks
Date: Wed Nov 17 2004 - 21:21:59 EST


On Tue, 16 Nov 2004 13:20:15 PST, A M said:

> Does anybody know how to access the address of the
> current executing instruction in C while the program
> is executing?

For what processor? x86, itanium, sparc, s390 all do it differently.

Also, the answer to "this *very* instruction" is different from
"where this instruction was when we trapped/kdbg/interrupt/whatever
it so we could look at the current process/thread/worker state".

In other words, are you trying to answer "Where in memory am *I*?"
or "Where in memory is <that very recent code I want to look at>?"

(Hint - for the former, you can probably get very good approximations
by just looking at the entry point address for the function:

(void *) where = &__FUNCTION__;

> Also, is there a method to load a program image from
> memory not a file (an exec that works with a memory
> address)? Mainly I am looking for a method that brings
> a program image into memory modify parts of it and
> start the in-memory modified version.

In user space, you probably want either mmap() or dlopen(), depending what it
is you're trying to do, most likely...

In kernel space, you'll have to be more specific as to what you're
trying to do, but you're always welcome to write a replacement for
fs/binfmt_elf.c :)

> Can anybody think of a method to replace a thread
> image without replacing the whole process image?

What are you trying to achieve here? It's unclear what you're
hoping will happen....

Attachment: pgp00000.pgp
Description: PGP signature