Find the answer to your Linux question:
Results 1 to 3 of 3
Is there a program out there like ltrace that will allow the user to determine every line of code executed by another program? Once upon a time I created such ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2004
    Location
    Minneapolis, MN
    Posts
    10

    tracing program execution


    Is there a program out there like ltrace that will allow the user to determine every line of code executed by another program?

    Once upon a time I created such a program on DomainOS (Apollo Computer) called 'tca' (Test Coverage Analyzer). You could execute a program then post-process the trace log to find out what lines of code where (or weren't) executed. It was an internal-only program. It would be nice to, at least, find some way of generating the PC counter history of the program execution.

    Thanks!
    Joel

  2. #2
    Just Joined!
    Join Date
    Aug 2004
    Posts
    17
    Try strace.

  3. #3
    Just Joined!
    Join Date
    Mar 2004
    Location
    Minneapolis, MN
    Posts
    10
    I think I have what I want as an example of starting point. My version of Sandeep's "small example" outputs the instruction count and instruction pointer.

    Here the URL containing Sandeep's example: http://linuxgazette.net/issue81/sandeep.html

    Here's my variant showing the instruction count and instruction pointer:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <syscall.h>
    #include <sys/ptrace.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <errno.h>
    #include <linux/user.h>
    
    int main&#40;void&#41;
    &#123;
            long long counter = 0;  /*  machine instruction counter */
            int wait_val;           /*  child's return value        */
            int pid;                /*  child's process id          */
            struct user_regs_struct regs;
    
            puts&#40;"Please wait"&#41;;
    
            switch &#40;pid = fork&#40;&#41;&#41; &#123;
            case -1&#58;
                    perror&#40;"fork"&#41;;
                    break;
            case 0&#58; /*  child process starts        */
                    ptrace&#40;PTRACE_TRACEME, 0, 0, 0&#41;;
                    /*
                     *  must be called in order to allow the
                     *  control over the child process
                     */
                    execl&#40;"/bin/ls", "ls", NULL&#41;;
                    /*
                     *  executes the program and causes
                     *  the child to stop and send a signal
                     *  to the parent, the parent can now
                     *  switch to PTRACE_SINGLESTEP
                     */
                    break;
                    /*  child process ends  */
            default&#58;/*  parent process starts       */
                    wait&#40;&wait_val&#41;;
                    /*
                     *   parent waits for child to stop at next
                     *   instruction &#40;execl&#40;&#41;&#41;
                     */
                    while &#40;wait_val == 1407 &#41; &#123;
                            if &#40;ptrace&#40;PTRACE_GETREGS, pid, 0, &regs&#41; != 0&#41;
                                    perror&#40;"ptrace gr"&#41;;
    
                            printf&#40;"%lld %08x\n", counter, regs.eip &#41;;
                            counter++;
    
                            if &#40;ptrace&#40;PTRACE_SINGLESTEP, pid, 0, 0&#41; != 0&#41;
                                    perror&#40;"ptrace ss"&#41;;
                            /*
                             *   switch to singlestep tracing and
                             *   release child
                             *   if unable call error.
                             */
                            wait&#40;&wait_val&#41;;
                            /*   wait for next instruction to complete  */
    
                    &#125;
                    /*
                     * continue to stop, wait and release until
                     * the child is finished; wait_val != 1407
                     * Low=0177L and High=05 &#40;SIGTRAP&#41;
                     */
            &#125;
            printf&#40;"Number of machine instructions &#58; %lld\n", counter&#41;;
            return 0;
    &#125;
    **MOD_EDIT: added 'code' tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •