This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Modify gcc to make it debug-able


The gcc program is actually a wrapper of those programs actually do the 
compiling job, so we need to modify gcc to debug the wrapped program. 
Below is an introduction of modifying gcc into a debugable program.

The point that gcc actually call these wrapped program is in the gcc-
3.0.2/gcc/gcc.c:2666, it look like this:
      commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
                                  programname, temp_filename,
                                  &errmsg_fmt, &errmsg_arg,
                                  ((i == 0 ? PEXECUTE_FIRST : 0)
                                   | (i + 1 == n_commands ? 
PEXECUTE_LAST : 0)
                                   | (string == commands[i].prog
                                      ? PEXECUTE_SEARCH : 0)
                                   | (verbose_flag ? PEXECUTE_VERBOSE : 
0)));

We need to change it in to this:
      commands[i].pid = pexecute_dbg (string, (char *const *) commands
[i].argv,
                                  programname, temp_filename,
                                  &errmsg_fmt, &errmsg_arg,
                                  ((i == 0 ? PEXECUTE_FIRST : 0)
                                   | (i + 1 == n_commands ? 
PEXECUTE_LAST : 0)
                                   | (string == commands[i].prog
                                      ? PEXECUTE_SEARCH : 0)
                                   | (verbose_flag ? PEXECUTE_VERBOSE : 
0)));
which would call a different process executor

the process executor is located in libiberty/pexecute.c, all we need to do 
is add a new function pexecute_dbg into it(after the last line):
pexecute_dbg (program, argv, this_pname, temp_base, errmsg_fmt, 
errmsg_arg, flags)
     const char *program;
     char * const *argv;
     const char *this_pname;
     const char *temp_base ATTRIBUTE_UNUSED;
     char **errmsg_fmt, **errmsg_arg;
     int flags;
{
        char **argvp;
        char buffer[20];
        char *env;

        env=getenv("DEBUG_SUB_PROCESS");
        if(!env || *env=='\0')
                return pexecute (program, argv, this_pname, temp_base, 
errmsg_fmt, errmsg_arg, flags);
        printf("The following command is going to be executed.\n\n");
        argvp=argv;
        while(*argvp)
        {
                printf("%s ", *argvp);
                argvp++;
        }
        printf("\n\nDo you want to debug it? [n]");
        assert(fgets(buffer, 20, stdin));
        if(buffer[0]=='y')
        {
                char *temp_name,*volitile_name;
                char *new_program;
                char *new_argv[5];
                FILE *temp_file;

                volitile_name=tmpnam(NULL);
                assert(volitile_name);
                temp_name=(char*)malloc(strlen(volitile_name)+1);
                temp_name=(char*)malloc(strlen(volitile_name)+1);
                assert(temp_name);
                strcpy(temp_name, volitile_name);
                temp_file=fopen(temp_name, "w");
                assert(temp_file);
                fputs("set args", temp_file);
                argvp=argv;
                while(*argvp)
                {
                        fputc(' ', temp_file);
                        fputs(*argvp, temp_file);
                        argvp++;
                }
                fprintf(temp_file, "\nshell rm %s\n", temp_name);
                fclose(temp_file);
                new_program=env;
                new_argv[0]=env;
                new_argv[1]=argv[0];
                new_argv[2]="-x";
                new_argv[3]=temp_name;
                new_argv[4]=NULL;
                return pexecute (new_program, new_argv, this_pname, 
temp_base, errmsg_fmt, errmsg_arg, flags);
        }
        else
                return pexecute (program, argv, this_pname, temp_base, 
errmsg_fmt, errmsg_arg, flags);
}

Note there are many pexecute functions in this file, and only on 
pexecute_dbg function, obviously our pexecute_dbg function is specially 
designed for linux platform, does it work under any other platform? I 
don't know.

How to use it? set the environment variable DEBUG_SUB_PROCESS to you 
desired debugger and run gcc to compile your program, it will stop and ask 
you if you want to debug each wrapped program, when you answer yes, it 
will start a debugger to debug it.


________________
Fudan University Alumni Email System, http://mail.fudan.edu,  FREE Signup.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]