Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Eclipse adding single quotes to my C command line parameters?

I'm running Eclipse v4.6 (Neon1) RC1 on a Windows & nbsp;10 machine, and it seems to be adding single quotes around each command line argument/parameter that I pass during debug. I see this show up in memory for each argv.

Strangely I can't produce these quotes to the console with a printf (during RUN); the program successfully loads the file specified by the parameters and outputs to console.

The command line arguments are set in the application run config->Parameters tab, as (single line, no quotes added):

keyFile.txt inputFile.txt outputFile.txt

I do not see this behavior in NetBeans (in fact have switched temporarily).

The issue is this appears to be causing problems with fopen when debugging: it can't find the file. I assume both are using the same working directory, because if I use a static filename of "keyFile.txt", debug works OK.

Unfortunately my console output doesn't work in debug, so I'm a little limited at the moment.

The example code is a much trimmed down version, to demonstrate:

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

int main(int argc, char** argv) {

    // Arguments: keyfile.txt inputfile.txt outputfile.txt

    char * firstArg = argv[1];
    char * secArg   = argv[2];   // Leaving these to show quotes on other inputs as well
    char * thirdArg = argv[3];

    printf("First arg:\t%s\n", firstArg);
    printf("Ptr Address, uint cast:\t0x%x\n", (unsigned int) firstArg);
    printf("Ptr Address, void* cast:\t%p\n", (void *) firstArg);
    printf("Char at Ptr:\t%c\n\n", (char) *(firstArg));

    printf("Second arg: \t%s\n", secArg);
    printf("Third arg: \t%s\n", thirdArg);

    FILE *fptr;
    fptr = fopen(firstArg, "rb");
    if (fptr == NULL)
     {
        perror("Error");
        return -1;
     }

    int kLength=0;
    int inputChar;
    unsigned char keyin[256];

    printf("\nData from file:\n");
    while ((inputChar = fgetc(fptr))!=EOF)        // Read KEYFILE
    {
        if ((kLength%8)==0) { printf("\n"); }
        keyin[kLength++] = (unsigned char) inputChar;
        printf("0x%x\t",inputChar);
    }

    return 0;
}

On Run, output is as expected/follows:

First arg:    keyFile.txt
Ptr Address, uint cast:        0x6b1748
Ptr Address, void* cast:    006B1748
Char at Ptr:    k

Second arg:     inputFile.txt
Third arg:     outputfile.txt
current Path: C:\Users\***\Google Drive\***\eclipse workspace\argTest
Data from file:

0x59    0x45    0xba    0x1e...
...
(data I expect is displayed from file ...)

But during debug, I get trapped by a null fptr, and I see the quotes in memory:

Debug memory: firstArg points to 0x6E1760:
Debug memory: firstArg points to 0x6E1760

Looking at the gdb traces, I see the single quotes here as well. Shown below is everything leading up to this point:

311,234 2-gdb-version
311,239 ~"GNU gdb (GDB) 7.6.1\n"
311,240 ~"Copyright (C) 2013 Free Software Foundation, Inc.\n"
311,240 ~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is fre\
e software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitt\
ed by law.  Type \"show copying\"\nand \"show warranty\" for details.\n"
311,240 ~"This GDB was configured as \"mingw32\".\nFor bug reporting instructions, please see:\n"
311,240 ~"<http://www.gnu.org/software/gdb/bugs/>.\n"
311,240 2^done
311,241 (gdb)
311,243 3-environment-cd "C:/Users/***/Google Drive/***/workspace/argTest"\
311,250 3^done
311,250 (gdb)
311,251 4-gdb-set breakpoint pending on
311,260 4^done
311,260 (gdb)
311,261 5-gdb-set detach-on-fork on
311,270 5^done
311,270 (gdb)
311,271 6-enable-pretty-printing
311,280 6^done
311,280 (gdb)
311,281 7-gdb-set python print-stack none
311,290 7^done
311,290 (gdb)
311,291 8-gdb-set print object on
311,300 8^done
311,300 (gdb)
311,301 9-gdb-set print sevenbit-strings on
311,310 9^done
311,310 (gdb)
311,311 10-gdb-set host-charset UTF-8
311,320 10^done
311,320 (gdb)
311,321 11-gdb-set target-charset WINDOWS-1252
311,330 11^done
311,330 (gdb)
311,331 12-gdb-set target-wide-charset UTF-16
311,340 12^done
311,340 (gdb)
311,342 13source .gdbinit
311,350 &"source .gdbinit\n"
311,350 &".gdbinit: No such file or directory.\n"
311,350 13^error,msg=".gdbinit: No such file or directory."
311,350 (gdb)
311,351 14-gdb-set target-async off
311,360 14^done
311,360 (gdb)
311,361 15-gdb-set auto-solib-add on
311,370 15^done
311,370 (gdb)
311,379 16-file-exec-and-symbols --thread-group i1 Debug/argTest.exe
311,384 16^done
311,384 (gdb)
311,385 17-gdb-set --thread-group i1 args 'keyFile.txt' 'inputFile.txt' 'outputFile.txt'
311,394 17^done
311,394 (gdb)
...
like image 716
NEJames Avatar asked Sep 10 '16 01:09

NEJames


2 Answers

This is a known bug in Eclipse, tracked here:

  • https://bugs.eclipse.org/bugs/show_bug.cgi?id=494246
like image 58
Ben Avatar answered Nov 14 '22 23:11

Ben


My workaround is to set the arguments in .gdbinit file instead of setting in Arguments tab (in Debug Configurations), just put the line like:

set args arg1 arg2

And then, it works fine!

like image 37
puttza Avatar answered Nov 15 '22 00:11

puttza