![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tracing self process using ptrace() | vpraveen84 | High Level Programming | 1 | 06-02-2008 01:14 PM |
| Cannot umount - device busy | keelba | HP-UX | 2 | 04-11-2008 12:16 PM |
| rmdev - device is busy | Livio | AIX | 2 | 05-17-2006 12:58 AM |
| umount, device busy, but.. | sTorm | UNIX for Dummies Questions & Answers | 10 | 08-20-2002 03:49 AM |
| freeBSD device driver (use struct uio) | Alex_T | High Level Programming | 0 | 07-31-2002 06:12 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
| Forum Sponsor | ||
|
|
|
||||
|
I can't actually try this, but that kill() immediately followed by a ptrace() really bothers me.
You seem to be thinking that the kill() system call will not return to the caller until after the signaled process has completed whatever action will result from the signal. Or something like that. In any event, my guess is that you need to signal the child and then wait until it stops. Look at paragraph 1 of the ptrace man page: "the tracing process is expected to notice this via wait(2) or the delivery of a SIGCHLD signal..." Where are you doing that? Some time is required to actually stop a process, even one that is sleeping. And look at the "ERRORS" section of the man page...only 3 possibilities. And only: "A request (other than PT_ATTACH) specified a process that wasn't stopped" really could apply here. Hmmmm. Good thing you didn't unbuffer stdout. If you had, that printf might have slowed down the tracer enough that it might sometimes work and sometimes fail. |
|
||||
|
Thanks for your reply!
I actually tried it using sleep( ) before, but that didn't have any effect. Using wait( ), as you suggested, did the trick. Now I have a new problem: If I pass low addresses as third parameter, I always get ``Bad address'' errors. While trying to find the root of this problem, I happened to pass the uninitialised variable ``i'' as address (I used it as loop counter before and when I commented that out, the value became, of course, undefined :-) )... It worked! Strangely, the value of ``i'' was 671482112. Quotation from the man page: Quote:
Code: Code:
...
int i;
...
printf( "Attempting to read...\n" );
wait( NULL );
rc = ptrace( PT_READ_D, child, ( void* )i, 0 );
if( rc == -1 && errno ) {
ERR( "ptrace", child );
}
printf( "%p: %d\n", ( void* )i, rc );
Code:
Attaching to 6487 Stopping.. Attempting to read... 0x28060100: -716130182 |
|
||||
|
It's awfully hard to help much on this since I don't have access to your system and this stuff varies a lot among kernels. But I'll try...
First, let's say that your pagesize is 4096 and your program needs 50 pages. So it is 50 * 4096 = 204800 bytes long. That does not mean that the first address is zero and the last is 204800. You can't make something like that work with any cpu. Typically the pages are arranged into segments and the segments are contiguous. But the segments will be scattered throughout the virtual address space. Think about malloc() and the underlying brk()/sbrk() calls. They need to be able to make the data segment grow. The stack must be able to change size dynamically. Maybe your program will map in a shared library...now everything has to grow. Also remember that it is illegal to deference a null pointer. Put something at zero and rerefencing a null pointer becomes legal. And taking the address of whatever you put there would give you NULL. The c standard prohibits that taking the address of an object would result in a NULL. And memory is organized into pages. If you can't use the first byte, you can't you the first page. So you need to know what address you need. It can be anywhere. You might try putting "int xyzzy=777" before the main in your little to-be-traced program. Then run the nm program on it. Since xyzzy is external in scope it should probably be in the symbol table. Get the address for it and plug that into your tracer program. Then see if you can get the value 777. I tend to think that will work. By the way, I have never used ptrace() on any system, so I am not exactly a ptrace() expert. I am not going to be able to help write a debugger or anything. |
|
||||
|
Ok, using addresses from nm works.
Thanks again for your help! |
|
||||
|
After sleeping on this, I think that I know how to to take this to the next level. The tracing program should be able to use nlist() on the executable of the child. This would enable it to programmaticly obtain the address of externals.
|
||||
| Google The UNIX and Linux Forums |