6

I don't know how to debug after the process after calling execle. I've looked at other websites and some suggested using set fork-follow-mode child, which helped me get into the fork. However, after the fork, I exit into the main function and never get into the program I am exec'ing.

Here is the code:

            } else if (!(pid_T2 = fork())) {
                char **env = NULL;
                char *units_env = NULL;
                char *sleep_env = NULL;
                size_t sleep_sz = 16;

                env = (char **) malloc(3 * sizeof(char *));
                sleep_env = (char *) malloc(sleep_sz * sizeof(char));
                snprintf(sleep_env, sleep_sz, "TSTALL=%d", cmd_args->sleep_num);
                if (cmd_args->kb) {
                        units_env = "UNITS=1";
                } else {
                        units_env = "UNITS=0";
                }
                *(env) = units_env; *(env + 1) = sleep_env; *(env + 2) = "TMOM=0";

                /*printf("%s %s\n", *(env), *(env + 1));*/

                close(pipe_A2toT2[1]);
                dup2(pipe_A2toT2[0], 0);
                close(pipe_A2toT2[0]);

                execle("totalsize", "totalsize", NULL, env); //Exits to main after this line, never goes into program.
          }

I know that the process image gets replaced by exec call, however why am I still exiting to this program's main instead of going into totalsize program?

mrQWERTY
  • 4,039
  • 13
  • 43
  • 91
  • Possible duplicate of [How to make gdb follow execv? Not working despite "follow-exec-mode"](http://stackoverflow.com/questions/10671229/how-to-make-gdb-follow-execv-not-working-despite-follow-exec-mode) – Ciro Santilli OurBigBook.com Mar 29 '17 at 14:25

1 Answers1

11

Here is the code:

That's not the code. That's an un-compilable and meaningless snippet of the code. You also didn't tell what OS you are using, or which GDB commands you used.

Here is an example showing how this is supposed to work, on Linux:

// echo.c

#include <stdio.h>

int main(int argc, char *argv[0]) {
  for (int i = 1; i < argc; ++i) {
    if (i != 1) printf(" ");
    printf("%s", argv[i]);
  }
  printf("\n");
  return 0;
}

// exec.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
  pid_t pid = fork();
  int status;

  if (pid == 0) {
    execlp("./echo", "echo", "aa", "bb", (char*)0);
    abort();
  } else {
    printf("parent %d waiting for %d\n", getpid(), pid);
    waitpid(pid, &status, 0);
    printf("child %d exited %d\n", pid, status);
  }
  return status;
}

Now let's compile this all and run under GDB:

gcc -g -std=c99 -o echo echo.c
gcc -g -o exec exec.c

gdb -q ./exec
Reading symbols from ./exec...done.
(gdb) set follow-fork-mode child
(gdb) break main
Breakpoint 1 at 0x4006a5: file exec.c, line 9.
(gdb) run
Starting program: /tmp/exec 

Breakpoint 1, main () at exec.c:9
9         pid_t pid = fork();
(gdb) c
Continuing.
[New process 9851]

Note how GDB attached new program above, because follow-fork-mode told it to.

parent 9832 waiting for 9851
process 9851 is executing new program: /tmp/echo

Note how GDB noticed that the process is executing a new binary.

[Switching to process 9851]

Breakpoint 1, main (argc=3, argv=0x7fffffffe8d8) at echo.c:4
4         for (int i = 1; i < argc; ++i) {

Note that we are now stopped in a different main.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362