I'm writing a package that accepts arbitrary character
strings from the command line, and I'm trying to debug it with
gdb.
When run without gdb the command line string is passed
directly to my program, but when I tell gdb to pass it, gdb
insists on interpreting it as a filename, and won't pass it
through.
Example without gdb:
=================================================
./testtoken ' (name testtoken endchar \) < testtoken.adb |
(trace) count lines|cons '
Input string is:
....+....1....+....2....+....3....+....4....+....5....+....6
(name testtoken endchar \) < testtoken.adb | (trace) count lines
cons
Before: TokenFrom = 1
TextLeft = 71
After: TokenFrom = 1
TextLeft = 71
Token 1 is: ' (name testtoken endchar \) < testtoken.adb |
(trace) count lines|cons '
End of tokens reached.
=================================================
Example with gdb:
=================================================
gdb testtoken --args ' (name testtoken endchar \) <
testtoken.adb | (trace) count lines|cons '
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html> This is free software: you are
free to change and redistribute it. There is NO WARRANTY, to the
extent permitted by law. Type "show copying" and "show
warranty" for details.
This GDB was configured as "i586-suse-linux"...
(name testtoken endchar \) < testtoken.adb | (trace) count lines
cons : No such file or directory.
(gdb)
=================================================
I have tried using the 'run PROGNAME ARGUMENTS...' method, but
it gives
me the same results. I have attached the source code and a
terminal session showing exactly what I have tried, so that you
can (presumably) duplicate my results. Perhaps I am
misunderstanding somehow the proper gdb syntax to be used, or
have wrong versions of tools?
Leslie
According to "man gdb", the syntax for --args is:
gdb [options] --args prog [arguments]
So the first thing after "--args" is the program name (prog), and *then*
come the programs' arguments. Try this:
gdb --args testtoken ' (name testtoken endchar \) <
> testtoken.adb | (trace) count lines|cons '
--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
> gdb --args testtoken ' (name testtoken endchar \) <
> > testtoken.adb | (trace) count lines|cons '
I get exactly the same result as before:
==================================
gdb --args testtoken ' (name testtoken endchar \) <
testtoken.adb | (trace) count lines|cons '
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute
it.
There is NO WARRANTY, to the extent permitted by law. Type "show
copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
testtoken: No such file or directory.
(gdb)
========================
> Niklas Holsti wrote:
>
> > gdb --args testtoken ' (name testtoken endchar \) <
> > > testtoken.adb | (trace) count lines|cons '
>
> I get exactly the same result as before:
This older version of gdb lacks the --args feature, but `set args` and
`show args` seem to work. Some of the tokens in your arg string appear
to be shell operators and commands. You may have to evaluate the string
in a shell and feed the result to args.
<transcript>
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40
UTC 2008) Copyright 2004 Free Software Foundation, Inc. GDB is free
software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions. Type "show copying" to see the conditions. There is
absolutely no warranty for GDB. Type "show warranty" for details. This
GDB was configured as "i386-apple-darwin"...Reading symbols for shared
libraries .. done
(gdb) set args ' (name testtoken endchar \) < testtoken.adb | (trace) count lines|cons '
(gdb) show args
Argument list to give program being debugged when it is started is
"' (name testtoken endchar \) < testtoken.adb | (trace) count lines|cons '".
(gdb)
</transcript>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Well, perhaps GDB is right and there is no such file?
Looking more carefully at your original question, at the attached
terminal session, it seems that your program is really named
"testcmdline", not "testtoken":
> 15:49:35 turriff@pinto
> ~/Documents/SourceCode$
> ./testcmdline 'with an arbitrary command string'
> with an arbitrary command string
Perhaps you should use
gdb --args testcmdline ' (name testtoken endchar \) <
testtoken.adb | (trace) count lines|cons '
The GDB command "set args" as suggested by John Matthews is also a good
method, and the one I have used until now.
> Leslie wrote:
>> Niklas Holsti wrote:
>>
>>> gdb --args testtoken ' (name testtoken endchar \) <
>>>> testtoken.adb | (trace) count lines|cons '
>>
>> I get exactly the same result as before:
>> ==================================
>> gdb --args testtoken ' (name testtoken endchar \) <
>> testtoken.adb | (trace) count lines|cons '
> ...
>> This GDB was configured as "i586-suse-linux"...
>> testtoken: No such file or directory.
>
> Well, perhaps GDB is right and there is no such file?
>
> Looking more carefully at your original question, at the
> attached terminal session, it seems that your program is really
> named "testcmdline", not "testtoken":
>
Ack! You're right; I changed the name of the program between
times. But it does the same thing with the correct program
name:
================================
gdb --args testcmdline ' (name testtoken endchar \) <
testtoken.adb | (trace) count lines|cons '
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute
it.
There is NO WARRANTY, to the extent permitted by law. Type "show
copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
(gdb)
===============================
Leslie
> In article <hhvlc7$nc8$1...@news.albasani.net>,
> Leslie <jltu...@centurytel.net> wrote:
>
>> Niklas Holsti wrote:
>>
>> > gdb --args testtoken ' (name testtoken endchar \) <
>> > > testtoken.adb | (trace) count lines|cons '
>>
>> I get exactly the same result as before:
>
> This older version of gdb lacks the --args feature, but `set
> args` and `show args` seem to work. Some of the tokens in your
> arg string appear to be shell operators and commands. You may
> have to evaluate the string in a shell and feed the result to
> args.
>
The single quotes should deactivate the "specialness" of those
operators. In any case, the entire string is being rejected,
not just parts of it, and no substitutions are made by shell.
However, I must say 'thank you very much,' because 'set args'
does the trick.
Thank you very much.
Leslie
You must also tell GDB to "run" the program at this point. So far, it
has only read the program file (testcmdline) but has not started to
execute the program. So type "run" to the "(gdb)" prompt.
> John B. Matthews wrote:
>
> > In article <hhvlc7$nc8$1...@news.albasani.net>,
> > Leslie <jltu...@centurytel.net> wrote:
> >
> >> Niklas Holsti wrote:
> >>
> >> > gdb --args testtoken ' (name testtoken endchar \) <
> >> > > testtoken.adb | (trace) count lines|cons '
> >>
> >> I get exactly the same result as before:
> >
> > This older version of gdb lacks the --args feature,
Looking again, my gdb has an --args option; it just doesn't work. :-)
> > but `set args` and `show args` seem to work. Some of the tokens in
> > your arg string appear to be shell operators and commands. You may
> > have to evaluate the string in a shell and feed the result to args.
>
> The single quotes should deactivate the "specialness" of those
> operators. In any case, the entire string is being rejected,
> not just parts of it, and no substitutions are made by shell.
You're right about the quotes, but I was mistaken about shell expansion.
It seems gdb uses the environments $SHELL (or /bin/sh) to expand args:
<http://sourceware.org/gdb/current/onlinedocs/gdb/Arguments.html>
(gdb) set args $((7 * 6))
(gdb) r
Starting program: argtest $((7 * 6))
42
> However, I must say 'thank you very much,' because 'set args'
> does the trick.
>
> Thank you very much.
Excellent!