1

Instead of calling strace directly from the command line,

I want to write a program that runs strace for me to a given program. I tried to solve this for hours but I just don't know how to get it working.

I previously wrote a program that uses the same piece of code used below. Previous program simply duplicates the action of shell, and it records user input to a log file. Link: https://pastebin.com/Wf9TTZSM

All I did was take out the relevant codes, and change the user input to run "strace". But it doesn't work.

How can I get this working?

I'm getting the Seg fault (core dumped) error for below

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>


int main ()
{

    char *s = "strace ls";

    char* separator = " ";
    char* argv[64];
    int argc = 0;
    char* tmp;

    printf("%s", s);

    argv[argc] = strtok_r(s, separator, &tmp);
    while( argv[argc] != NULL){
        argc+=1;
        argv[argc] = strtok_r(NULL, separator, &tmp);
    }

    execvp(argv[0],argv);

    return 0;
}
Leonard
  • 2,978
  • 6
  • 21
  • 42
  • 2
    Change `char *s = "strace ls";` to `char s[] = "strace ls";` – Barmar May 24 '18 at 04:11
  • @Barmar Thanks so much I figure it out right after! But my question is, do you have any idea why my code worked fine with char *user_input in my previous program that I linked? Is it because I'm passing it as an argument to the function "execute"? – Leonard May 24 '18 at 05:02
  • Your previous code doesn't show how `execute()` is called. If the argument is a literal string it should fail in the same way, because you're not allowed to modify literals, and that's how `strtok()` works. If you call it with an array containing user input then it can be modified by `strtok()`. – Barmar May 24 '18 at 16:11
  • @Barmar Got it. My execute() is at line 72 btw. If you answer this I will accept you as the answer. – Leonard Jun 01 '18 at 07:24
  • Duplicate questions can't be answered. – Barmar Jun 01 '18 at 07:27
  • The previous code has `char user_input[1024];` That's a modifiable array, so no error. – Barmar Jun 01 '18 at 07:29

0 Answers0