Search the symbol table of a child process


 
Thread Tools Search this Thread
Top Forums Programming Search the symbol table of a child process
# 1  
Old 04-23-2019
Search the symbol table of a child process

Hi,

I am a newbie in Linux land, and I have a question about programming parent/child process interaction:

How do I search the value of a symbol in the child process? Is it possible?

I am doing a fork() and execve() to spawn any child possible, and I need something on the parent side to give me the value of any given symbol in the child, ie. "main".

In AmigaOS (my home country) this is very simple. Maybe there is a reason for it not to be so simple in UNIX land?

Best /Alpha
# 2  
Old 04-23-2019
You have to use an IPC (interprocess communication) mechanism, some listed here:
1. use a file
2. use pipes
3. use shared memory
4. semaphores / mutexes

tldp.org link for IPC
6 Linux Interprocess Communications Documentation

Unless you are using shared memory, there is normally no direct access to memory-only objects by the parent into a child process memory. Debuggers may use kernel mode memory access to get around this restriction -- a security issue otherwise.

Last edited by jim mcnamara; 04-23-2019 at 12:39 PM..
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-23-2019
Thanks.

I still don't understand, though: If my child executes something like this:

Code:
int pid = fork();
if(pid == 0) {
    //where exactly do I set up the IPC? Before this point, I do not know any symbol values of 'processName', and after this point, I can no longer do any child specific symbol manipulation.
    execve(processName, 0, 0); //processName can be any process. Like 'ls'.
} else {
    //what to do here?
}

... where is the space, from where the child should conduct its part of the IPC? Can I somehow set up a handler function on the child side, or what exactly am I supposed to do?

Sorry, this is so alien to me, and probably I am trying something, that is not very 'Unix', because of my background in another operating system. Any help is very much appreciated.
# 4  
Old 04-23-2019
Where you set up IPC depends on which one you select. If you were to read the link, you would see a pipe example like this (from forgeeks.org):

Code:
// C program to demonstrate use of fork() and pipe() 
#include<stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 
#include<sys/types.h> 
#include<string.h> 
#include<sys/wait.h> 
  
int main() 
{ 
    // We use two pipes 
    // First pipe to send input string from parent 
    // Second pipe to send concatenated string from child 
  
    int fd1[2];  // Used to store two ends of first pipe 
    int fd2[2];  // Used to store two ends of second pipe 
  
    char fixed_str[] = "forgeeks.org"; 
    char input_str[100]; 
    pid_t p; 
  
    if (pipe(fd1)==-1) 
   { 
        fprintf(stderr, "Pipe Failed" ); 
        return 1; 
    } 
    if (pipe(fd2)==-1) 
    { 
        fprintf(stderr, "Pipe Failed" ); 
        return 1; 
    } 
  
    scanf("%s", input_str); 
    p = fork(); 
  
    if (p < 0) 
    { 
        fprintf(stderr, "fork Failed" ); 
        return 1; 
    } 
  
    // Parent process 
    else if (p > 0) 
    { 
        char concat_str[100]; 
  
        close(fd1[0]);  // Close reading end of first pipe 
  
        // Write input string and close writing end of first 
        // pipe. 
        write(fd1[1], input_str, strlen(input_str)+1); 
        close(fd1[1]); 
        // Wait for child to send a string 
        wait(NULL); 
  
        close(fd2[1]); // Close writing end of second pipe 
  
        // Read string from child, print it and close 
        // reading end. 
        read(fd2[0], concat_str, 100); 
        printf("Concatenated string %s\n", concat_str); 
        close(fd2[0]); 
    } 
  
    // child process 
    else
    { 
        close(fd1[1]);  // Close writing end of first pipe 
  
        // Read a string using first pipe 
        char concat_str[100]; 
        read(fd1[0], concat_str, 100); 
  
        // Concatenate a fixed string with it 
        int k = strlen(concat_str); 
        int i; 
        for (i=0; i<strlen(fixed_str); i++) 
            concat_str[k++] = fixed_str[i]; 
  
        concat_str[k] = '\0';   // string ends with '\0' 
  
        // Close both reading ends 
        close(fd1[0]); 
        close(fd2[0]); 
  
        // Write concatenated string and close writing end 
        write(fd2[1], concat_str, strlen(concat_str)+1); 
        close(fd2[1]); 
  
        exit(0); 
    } 
}

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 04-23-2019
Piping enables me to read the console output of a process. I did a fair bit of piping, back when I ported CMake to the Amiga platform.

Now, I simply do not get: How does that give me access to the symbols of the execve process? AFAICS there is no way, that a random process like 'ls' is going to come up with a generalized form of submitting a symbol value (ie. "main = 0xbeefdeaf") to either the stdin, stdout or stderr. I guess, if I *could* interrerupt the child process and execute something like

Code:
// ** child code
//int parentPipeFd = something sensible
void interrupt_handler() {
    void *main__symbol = dlsym(RTLD_DEFAULT, "main");
    fprintf(parentPipeFd, "0x%x", main__symbol);
}

But then how do I actually run a piece of code on the child side without having to write it in the actual code of the execve process?

Sorry, I am being really thick headed on this point, I can see. Please bear with me, I am so new to all of this Smilie.

EDIT: Could this be what I am looking for? EDIT EDIT: I am too new to post links. I think, I figured it out, thanks for the help Smilie.

Last edited by alphakili; 04-23-2019 at 02:54 PM..
# 6  
Old 04-23-2019
Well, in fact it seems, that I was wrong - I haven't solved the problem. What I was trying to do was to setup a signal handler, that would drive the interchange of information between the parent and the child. According to an answer to a similar question on another forum (I still cannot post links due to my status as initiate), this is indeed not possible at all. So I am about to give the answer to my own question, that what I am trying to do is actually impossible. I would love nothing more than to be proven wrong Smilie.
# 7  
Old 04-24-2019
Quote:
Originally Posted by alphakili
Now, I simply do not get: How does that give me access to the symbols of the execve process?
It doesn't. The child is supposed to print its own symbols.

Either that, or you go whole-hog and attach a debugger. Probably much easier to use gdb than build it yourself.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

2. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

3. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

4. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

5. Programming

Symbol table of a C program

Hi, is there any command to see symbol table info. will it show where its allocating memory for varibales golbals & locals and code.(i mean the segments). i read there is a section called read only data segment and this is where initialized data such as strings stores. i have wriiten the... (7 Replies)
Discussion started by: MrUser
7 Replies

6. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

7. Programming

Reading ELF file Symbol table of C++ program

Folks, I have some program(Test.cpp) as follows, #include<iostream> class Abc { private: int _theVar; public : int printVar(); }; int Abc :: printVar() { _theVar=10; } main() { Abc _t; (2 Replies)
Discussion started by: vinod_chitrali
2 Replies

8. Linux

Reading ELF file Symbol table of C++ program

Folks, I have some program(Test.cpp) as follows, #include<iostream> class Abc { private: int _theVar; public : int printVar(); }; int Abc :: printVar() { _theVar=10; } main() { Abc _t; (0 Replies)
Discussion started by: vinod_chitrali
0 Replies

9. Programming

how to view symbol table in unix

hi , How to view the contents of a "c" program symbol table information in unix. (1 Reply)
Discussion started by: saravanan_nitt
1 Replies

10. Programming

no symbol table

Hi@all, I try to compile c code on hpux 11.11 pa-risc 2 with gcc (32bit). I compile with the option -g, so that I get the symbol table, but it is not available. Does someone knows something on this? thx (2 Replies)
Discussion started by: Dom_Cyrus
2 Replies
Login or Register to Ask a Question