Lots of questions about trace(1)

Lots of questions about trace(1)

Post by Saripalli Ramakrish » Thu, 25 Mar 1993 22:56:00



Hi there,

        I have some doubts regarding the usage of trace(1) or its
        equivalent strace(1) (from sunsite.unc.edu and other sites)
        for the SunOS

        I got my program here (in C) as follows ->

        It uses the dlsym() dlopen() functions (these functions are
        available using the -ldl switch) to read the addresses of
        symbol table entries and use these addresses to access these
        values.A convenient way to map strings to virtual addresses

        /*****************My program**********************************/

        #include        <dlfcn.h>
        typedef         int             (*INT_FUNC_PTR)();

        main()
        {
                void    *p;
                void    *h;
                int     func();

                printf("before the dlopen()\n");
                h = dlopen(0,1);
                printf("after the dlopen()\n");

                if (!h) {
                        printf("I have failed here");
                        exit(1);
                        }

                printf("before the dlsym()\n");
                p = dlsym(h,"func");
                printf("after the dlsym()\n");

                if (!p ) {
                        printf("I have failed on second");
                        exit(1);
                        }

                printf("p = %x\n",p);
                printf("address = %x\n",func);

                printf("calling the function\n");

                ((INT_FUNC_PTR) p) ();
        }

        func()
        {
                int     i;

                printf("read in a integer");

                scanf("%d",&i);

                i += 5 ;

                printf("in func value of i = %d\n",i);
        }

        /************output of strace(1)*****************************/

        open("/usr/lib/ld.so", RDONLY, 40300) = 4
        read(4, "\81\3\1\b\0\0\80\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0", 32) = 32
        mmap(0, 40960, READ|EXEC, PRIVATE, 4, 0) = 0xf77e0000
        mmap(0xf77e8000, 8192, READ|WRITE|EXEC, PRIVATE|FIXED, 4, 0x8000) = 0xf77e8000
        open("/dev/zero", RDONLY, 7) = 5
        getrlimit(STACK, {cur: 8388608, max: 402653184}) = 0
        mmap(0xf7800000, 8192, READ|WRITE, PRIVATE|FIXED, 5, 0) = 0xf7800000
        close(4) = 0
        getuid() = 237 ([euid 237])
        getgid() = 102 ([egid 102])
        open("/etc/ld.so.cache", RDONLY, 5000100021) = 4
        fstat(4, [REG ino 2022 nlnks 1 ...]) = 0
        mmap(0, 4096, READ, SHARED, 4, 0) = 0xf77c0000
        close(4) = 0
        open("/usr/local/lib/X11", RDONLY, 1010525) = 4
        fstat(4, [DIR ino 185120 nlnks 15 ...]) = 0
        mmap(0xf7802000, 8192, READ|WRITE, PRIVATE|FIXED, 5, 0) = 0xf7802000
        getdents(4, {Total: 52 dents}, 8192) = 1180
        getdents(4, {Total: 0 dents}, 8192) = 0
        close(4) = 0
        open("/usr/local/ow/lib", RDONLY, 22) = 4
        getdents(4, {Total: 77 dents}, 8192) = 1952
        getdents(4, {Total: 0 dents}, 8192) = 0
        mmap(0xf7804000, 8192, READ|WRITE, PRIVATE|FIXED, 5, 0) = 0xf7804000
        close(4) = 0
        open("/usr/local/interviews/lib/SUN4", RDONLY, 21) = -1 (No such file or directory)
        open("/usr/lib/libdl.so.1.0", RDONLY, 22770) = 4
        read(4, "\81\3\1\b\0\0 \0\0\0 \0\0\0\0\0\0\0\0\84\0\0\0 \0\0\0\0\0\0\0\0", 32) = 32
        mmap(0, 16396, READ|EXEC, PRIVATE, 4, 0) = 0xf77a0000
        mmap(0xf77a2000, 8192, READ|WRITE|EXEC, PRIVATE|FIXED, 4, 0x2000) = 0xf77a2000
        close(4) = 0
        open("/usr/lib/libc.so.1.6", RDONLY, 23010) = 4
        read(4, "\81\3\1\b\0\6\c0\0\0\0@\0\0\0\0\0\0\0s \0\0\0 \0\0\0\0\0\0\0\0", 32) = 32
        mmap(0, 458764, READ|EXEC, PRIVATE, 4, 0) = 0xf7710000
        mmap(0xf777c000, 16384, READ|WRITE|EXEC, PRIVATE|FIXED, 4, 0x6c000) = 0xf777c000
        close(4) = 0
        close(5) = 0
        ioctl(1, TCGETA, 0xf7ffec54) = 0
        getpagesize() = 0x1000
        brk(0x61b0) = 0
        brk(0x71b0) = 0
        write(1, "before the dlopen()\n", 20) = 20
        write(1, "after the dlopen()\n", 19) = 19
        write(1, "before the dlsym()\n", 19) = 19
        write(1, "after the dlsym()\n", 18) = 18
        write(1, "p = 23b0\n", 9) = 9
        write(1, "address = 23b0\n", 15) = 15
        write(1, "calling the function\n", 21) = 21
        ioctl(0, TCGETA, 0xf7ffed6c) = 0
        write(1, "read in a integer", 17) = 17
        read(0, "10\n", 128) = 3
        write(1, "in func value of i = 15\n", 24) = 24
        close(0) = 0
        close(1) = 0
        close(2) = 0
        rexit(1) = ?
        /***********end of output from strace(1)*****************/

        Now my questions to the gurus are ->

        what is all the output that is generated before my actual
        output? In which phase of the compilation is this done
        (I guess it is the link phase but i am not sure)

        I do have the source for strace(1) as it is PD,but I am
        not able to debug it.

        Why is all this output needed?.It appears that this output
        is needed before the actual program even though I have
        just the standard C library linked in and it is apparently
        being done for all programs.

        From the first line of the strace(1) output,it looks like
        the ld.so program(the dynamic link editor) is being mapped
        into the address space of my process).Why is this needed?

        Also from the various lines in the output,it is clear that
        the shared object versions of the standard C library
        (libc.so.x.x) are being mapped into the process.Why?

        Also why does the strace(1) program need /usr/local/ow/lib
        and the directory entries for the X11 lib?

->   Also more interestingly the calls to dlopen(),dlsym()
        exist no more in the output from strace(1),whereas the
        calls to scanf() and standard I/O library calls are
        mapped to read() and write() system calls as expected.

        What is happening here?.

        In short I am asking questions about the strace(1) or
        the trace(1) program.

        I would really appreciate it if some kind soul could
        answer these questions.

        If someone has the source for strace(1),could someone
        lead me along the source?.

        thanks.
                                        Ramakrishna Saripalli

e-mail : rsar...@cs.clemson.edu
Man is a beast and usually has no control over his passions.