How to do this debugging?

People,

I usually pipe my data file through my ruby script with:

  cat parsefile | t1.rb

but some obscure problems have been tricky to find - if I use:

  ruby -r debug t1.rb

- how do I pipe the parsefile in ?

Thanks,

Phil.

···

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

quoth the Philip Rhoades:

People,

I usually pipe my data file through my ruby script with:

  cat parsefile | t1.rb

but some obscure problems have been tricky to find - if I use:

  ruby -r debug t1.rb

- how do I pipe the parsefile in ?

Doesn't this work?:

          ruby -r debug t1.rb < parsefile

Thanks,

Phil.

-d

···

--
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

Philip Rhoades wrote:

People,

I usually pipe my data file through my ruby script with:

  cat parsefile | t1.rb

but some obscure problems have been tricky to find - if I use:

  ruby -r debug t1.rb

- how do I pipe the parsefile in ?

If you redefine $stdin, you can get this to work. Enter the debugger
with:

        ruby -r debug t1.rb

Now redefine $stdin as:

        $stdin = IO.popen("cat parsefile")

You can now start debugging. All input will be read from the result of
the IO.popen call.

--Dale Martenson

···

--
Posted via http://www.ruby-forum.com/\.

Dale, Darren,

Philip Rhoades wrote:
> People,
>
> I usually pipe my data file through my ruby script with:
>
> cat parsefile | t1.rb
>
> but some obscure problems have been tricky to find - if I use:
>
> ruby -r debug t1.rb
>
> - how do I pipe the parsefile in ?
>

If you redefine $stdin, you can get this to work. Enter the debugger
with:

        ruby -r debug t1.rb

Now redefine $stdin as:

        $stdin = IO.popen("cat parsefile")

You can now start debugging. All input will be read from the result of
the IO.popen call.

--Dale Martenson

Using both this method and:

  ruby -r debug t1.rb < parsefile (which I had already tried)

do strange things - eg:

[root@prix bin]# ruby -r debug t1.rb
Debug.rb
Emacs support available.

t1.rb:6:require 'ftools'
(rdb:1) $stdin = IO.popen("cat t")
#<IO:0xb7f16708>
(rdb:1) Received: from localhost (HELO localhost.localdomain)
(127.0.0.1) by prix.pricom.com.au with SMTP; 20 Jun 2006 08:11:43 -0000
t1.rb:6:compile error
t1.rb:6: syntax error, unexpected ':', expecting $end
.
.
similar repeated errors for the rest of the parsefile ('t")

The top of t1.rb looks like:

#!/usr/bin/ruby
# qmail-pfilter.rb
# v1.1
# 2006-03-14

require 'ftools' (line 6)

The first line of the mail item - text file "t" (parsefile) is read in
and then I get the compile/syntax errors - these continue for the whole
of the rest of the text file but I need to be able to step through the
program one line at a time from the parsefile . .

What is going on?

Thanks,

Phil.

···

On Tue, 2006-06-20 at 05:04 +0900, Dale Martenson wrote:
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

Philip Rhoades wrote:

Dale, Darren,

>
        $stdin = IO.popen("cat parsefile")

You can now start debugging. All input will be read from the result of
the IO.popen call.

--Dale Martenson

Using both this method and:

  ruby -r debug t1.rb < parsefile (which I had already tried)

do strange things - eg:

[root@prix bin]# ruby -r debug t1.rb
Debug.rb
Emacs support available.

t1.rb:6:require 'ftools'
(rdb:1) $stdin = IO.popen("cat t")
#<IO:0xb7f16708>
(rdb:1) Received: from localhost (HELO localhost.localdomain)
(127.0.0.1) by prix.pricom.com.au with SMTP; 20 Jun 2006 08:11:43 -0000
t1.rb:6:compile error
t1.rb:6: syntax error, unexpected ':', expecting $end
.
.
similar repeated errors for the rest of the parsefile ('t")

The top of t1.rb looks like:

#!/usr/bin/ruby
# qmail-pfilter.rb
# v1.1
# 2006-03-14

require 'ftools' (line 6)

The first line of the mail item - text file "t" (parsefile) is read in
and then I get the compile/syntax errors - these continue for the whole
of the rest of the text file but I need to be able to step through the
program one line at a time from the parsefile . .

What is going on?

I don't know. I am not sure what your program is doing. Is there anyway
you could share the source. Or could you reduce the program down to a
simple case that still causes the problem.

Here is a very simple program that attempts to show a couple of things.

1. I required 'ftools' since that seems to be where you are getting into
trouble. It is not needed by the program. Then stepped in the debugger
to see what happens when the "require 'ftools'" line is processed. You
should do the same.

2. The program takes the piped input and burps it back out again with a
prefix. The input is just the program source itself.

------ d.rb ------

#!/usr/bin/env ruby

require 'ftools'

$stdin.each_line do |line|
        puts "BUBBA: "+line
end
puts "BUBBA END."

------ EOF ------

Here is my output when I stepped through the program in the debugger.

------ output ------

# ruby -r debug d.rb
Debug.rb
Emacs support available.

d.rb:3:require 'ftools'
(rdb:1) $stdin = IO.popen( "cat d.rb" )
#<IO:0x401f5f40>
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:1:class << File
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:3: BUFSIZE = 8 * 1024
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:5: def catname from, to
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:15: def syscopy from, to
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:41: def copy from, to, verbose = false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:46: alias cp copy
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:50: def move from, to, verbose = false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:75: alias mv move
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:81: def compare from, to, verbose = false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:112: alias cmp compare
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:116: def safe_unlink(*files)
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:127: alias rm_f safe_unlink
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:129: def makedirs(*dirs)
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:148: alias mkpath makedirs
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:150: alias o_chmod chmod
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:152: vsave, $VERBOSE = $VERBOSE, false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:153: def chmod(mode, *files)
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:158: $VERBOSE = vsave
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:160: def install(from, to, mode = nil,
verbose = false)
(rdb:1) s
d.rb:5:$stdin.each_line do |line|
(rdb:1) s
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: #!/usr/bin/env ruby
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA:
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: require 'ftools'
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA:
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: $stdin.each_line do |line|
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: puts "BUBBA: "+line
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: end
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: puts "BUBBA END."
d.rb:8:puts "BUBBA END."
(rdb:1) s
BUBBA END.

···

On Tue, 2006-06-20 at 05:04 +0900, Dale Martenson wrote:

#

------ EOF ------

You should attempt to step through your program to see what happens. You
may want to turn on trace.

--
Posted via http://www.ruby-forum.com/\.

Dale,

Something odd here - see below . .

Philip Rhoades wrote:
> Dale, Darren,
>
>
>> >
>> $stdin = IO.popen("cat parsefile")
>>
>> You can now start debugging. All input will be read from the result of
>> the IO.popen call.
>>
>> --Dale Martenson
>
>
> Using both this method and:
>
> ruby -r debug t1.rb < parsefile (which I had already tried)
>
> do strange things - eg:
>
> [root@prix bin]# ruby -r debug t1.rb
> Debug.rb
> Emacs support available.
>
> t1.rb:6:require 'ftools'
> (rdb:1) $stdin = IO.popen("cat t")
> #<IO:0xb7f16708>
> (rdb:1) Received: from localhost (HELO localhost.localdomain)
> (127.0.0.1) by prix.pricom.com.au with SMTP; 20 Jun 2006 08:11:43 -0000
> t1.rb:6:compile error
> t1.rb:6: syntax error, unexpected ':', expecting $end
> .
> .
> similar repeated errors for the rest of the parsefile ('t")
>
>
> The top of t1.rb looks like:
>
> #!/usr/bin/ruby
> # qmail-pfilter.rb
> # v1.1
> # 2006-03-14
>
> require 'ftools' (line 6)
>
> The first line of the mail item - text file "t" (parsefile) is read in
> and then I get the compile/syntax errors - these continue for the whole
> of the rest of the text file but I need to be able to step through the
> program one line at a time from the parsefile . .
>
> What is going on?
>

I don't know. I am not sure what your program is doing. Is there anyway
you could share the source. Or could you reduce the program down to a
simple case that still causes the problem.

Here is a very simple program that attempts to show a couple of things.

1. I required 'ftools' since that seems to be where you are getting into
trouble. It is not needed by the program. Then stepped in the debugger
to see what happens when the "require 'ftools'" line is processed. You
should do the same.

2. The program takes the piped input and burps it back out again with a
prefix. The input is just the program source itself.

------ d.rb ------

#!/usr/bin/env ruby

require 'ftools'

$stdin.each_line do |line|
        puts "BUBBA: "+line
end
puts "BUBBA END."

------ EOF ------

Here is my output when I stepped through the program in the debugger.

------ output ------

# ruby -r debug d.rb
Debug.rb
Emacs support available.

d.rb:3:require 'ftools'
(rdb:1) $stdin = IO.popen( "cat d.rb" )
#<IO:0x401f5f40>
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:1:class << File
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:3: BUFSIZE = 8 * 1024
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:5: def catname from, to
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:15: def syscopy from, to
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:41: def copy from, to, verbose = false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:46: alias cp copy
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:50: def move from, to, verbose = false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:75: alias mv move
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:81: def compare from, to, verbose = false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:112: alias cmp compare
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:116: def safe_unlink(*files)
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:127: alias rm_f safe_unlink
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:129: def makedirs(*dirs)
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:148: alias mkpath makedirs
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:150: alias o_chmod chmod
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:152: vsave, $VERBOSE = $VERBOSE, false
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:153: def chmod(mode, *files)
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:158: $VERBOSE = vsave
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:160: def install(from, to, mode = nil,
verbose = false)
(rdb:1) s
d.rb:5:$stdin.each_line do |line|
(rdb:1) s
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: #!/usr/bin/env ruby
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA:
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: require 'ftools'
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA:
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: $stdin.each_line do |line|
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: puts "BUBBA: "+line
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: end
d.rb:6: puts "BUBBA: "+line
(rdb:1) s
BUBBA: puts "BUBBA END."
d.rb:8:puts "BUBBA END."
(rdb:1) s
BUBBA END.
#

------ EOF ------

You should attempt to step through your program to see what happens. You
may want to turn on trace.

[root@prix bin]# ruby -r debug debugtst.rb
Debug.rb
Emacs support available.

debugtst.rb:3:require 'ftools'
(rdb:1) $stdin = IO.popen( "cat debugtst.rb" )
#<IO:0xb7f676c8>
(rdb:1) #!/usr/bin/env ruby
nil
(rdb:1)
#!/usr/bin/env ruby
nil
(rdb:1) require 'ftools'
true
(rdb:1)
require 'ftools'
false
(rdb:1) $stdin.each_line do |line|
debugtst.rb:3:compile error
debugtst.rb:3: syntax error, unexpected $end
$stdin.each_line do |line|
                          ^
(rdb:1) puts "BUBBA: "+line
debugtst.rb:3:undefined local variable or method `line' for main:Object
(rdb:1) end
debugtst.rb:3:compile error
debugtst.rb:3: syntax error, unexpected kEND
(rdb:1) puts "BUBBA END."
BUBBA END.
nil
(rdb:1)
puts "BUBBA END."
BUBBA END.
nil
(rdb:1)
puts "BUBBA END."
BUBBA END.
nil
(rdb:1)
puts "BUBBA END."
BUBBA END.
nil
(rdb:1)

I don't get the chance to step . . it looks like something peculiar to
my system (FC5 + latest Ruby) . . any idea what?

Thanks,

Phil.

···

On Tue, 2006-06-20 at 23:54 +0900, Dale Martenson wrote:

> On Tue, 2006-06-20 at 05:04 +0900, Dale Martenson wrote:

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

You know, if you use ARGF instead of $stdin your program will work for

ruby a.rb some_file

and cat some_file | ./a.rb

···

On Jun 20, 2006, at 2:53 PM, Philip Rhoades wrote:

$stdin.each_line do |line|
        puts "BUBBA: "+line
end

Philip Rhoades wrote:

Dale,

Something odd here - see below . .

>>
> Debug.rb
> .
> require 'ftools' (line 6)
you could share the source. Or could you reduce the program down to a
prefix. The input is just the program source itself.
puts "BUBBA END."

/usr/lib/ruby/1.8/ftools.rb:15: def syscopy from, to
(rdb:1) s
/usr/lib/ruby/1.8/ftools.rb:150: alias o_chmod chmod
d.rb:5:$stdin.each_line do |line|
d.rb:6: puts "BUBBA: "+line
BUBBA: end
You should attempt to step through your program to see what happens. You
may want to turn on trace.

[root@prix bin]# ruby -r debug debugtst.rb
Debug.rb
Emacs support available.

debugtst.rb:3:require 'ftools'
(rdb:1) $stdin = IO.popen( "cat debugtst.rb" )
#<IO:0xb7f676c8>
(rdb:1) #!/usr/bin/env ruby
nil
(rdb:1)
#!/usr/bin/env ruby
nil
(rdb:1) require 'ftools'
true
(rdb:1)
require 'ftools'
false
(rdb:1) $stdin.each_line do |line|
debugtst.rb:3:compile error
debugtst.rb:3: syntax error, unexpected $end
$stdin.each_line do |line|
                          ^
(rdb:1) puts "BUBBA: "+line
debugtst.rb:3:undefined local variable or method `line' for main:Object
(rdb:1) end
debugtst.rb:3:compile error
debugtst.rb:3: syntax error, unexpected kEND
(rdb:1) puts "BUBBA END."
BUBBA END.
nil
(rdb:1)
puts "BUBBA END."
BUBBA END.
nil
(rdb:1)
puts "BUBBA END."
BUBBA END.
nil
(rdb:1)
puts "BUBBA END."
BUBBA END.
nil
(rdb:1)

I don't get the chance to step . . it looks like something peculiar to
my system (FC5 + latest Ruby) . . any idea what?

What version of ruby are you using? I happened to be using 1.8.2 (not
the latest and greatest, but darn good none the less). It is like it is
not letting you redefine $stdin. Once you do, it takes the input to the
debugger from the piped input which is not what you want. Is this a
difference between using readline or not for IRB. I don't think so and I
would assume you are using readline for IRB. I also tried it on an
older machine of mine that is running a 1.8.1 version of Ruby that
doesn't have readline and it worked as I would expect.

Weird?

--Dale

···

On Tue, 2006-06-20 at 23:54 +0900, Dale Martenson wrote:

--
Posted via http://www.ruby-forum.com/\.

Logan,

···

On Thu, 2006-06-22 at 04:18 +0900, Logan Capaldo wrote:

On Jun 20, 2006, at 2:53 PM, Philip Rhoades wrote:

>> $stdin.each_line do |line|
>> puts "BUBBA: "+line
>> end

You know, if you use ARGF instead of $stdin your program will work for

ruby a.rb some_file

and cat some_file | ./a.rb

OK . . but how does that help when trying to use "ruby -r debug? - I
still get the previous problems . .

Thanks,

Phil.

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

Dale,

Philip Rhoades wrote:
> Dale,
>
> Something odd here - see below . .
>
>
>> >>
>> > Debug.rb
>> > .
>> > require 'ftools' (line 6)
>> you could share the source. Or could you reduce the program down to a
>> prefix. The input is just the program source itself.
>> puts "BUBBA END."
>>
>> /usr/lib/ruby/1.8/ftools.rb:15: def syscopy from, to
>> (rdb:1) s
>> /usr/lib/ruby/1.8/ftools.rb:150: alias o_chmod chmod
>> d.rb:5:$stdin.each_line do |line|
>> d.rb:6: puts "BUBBA: "+line
>> BUBBA: end
>> You should attempt to step through your program to see what happens. You
>> may want to turn on trace.
>
>
> [root@prix bin]# ruby -r debug debugtst.rb
> Debug.rb
> Emacs support available.
>
> debugtst.rb:3:require 'ftools'
> (rdb:1) $stdin = IO.popen( "cat debugtst.rb" )
> #<IO:0xb7f676c8>
> (rdb:1) #!/usr/bin/env ruby
> nil
> (rdb:1)
> #!/usr/bin/env ruby
> nil
> (rdb:1) require 'ftools'
> true
> (rdb:1)
> require 'ftools'
> false
> (rdb:1) $stdin.each_line do |line|
> debugtst.rb:3:compile error
> debugtst.rb:3: syntax error, unexpected $end
> $stdin.each_line do |line|
> ^
> (rdb:1) puts "BUBBA: "+line
> debugtst.rb:3:undefined local variable or method `line' for main:Object
> (rdb:1) end
> debugtst.rb:3:compile error
> debugtst.rb:3: syntax error, unexpected kEND
> (rdb:1) puts "BUBBA END."
> BUBBA END.
> nil
> (rdb:1)
> puts "BUBBA END."
> BUBBA END.
> nil
> (rdb:1)
> puts "BUBBA END."
> BUBBA END.
> nil
> (rdb:1)
> puts "BUBBA END."
> BUBBA END.
> nil
> (rdb:1)
>
>
> I don't get the chance to step . . it looks like something peculiar to
> my system (FC5 + latest Ruby) . . any idea what?
>

What version of ruby are you using?

ruby-1.8.4-6.fc5

I happened to be using 1.8.2 (not
the latest and greatest, but darn good none the less). It is like it is
not letting you redefine $stdin. Once you do, it takes the input to the
debugger from the piped input which is not what you want. Is this a
difference between using readline or not for IRB.

readline-5.0-3.2.1

I don't think so and I
would assume you are using readline for IRB. I also tried it on an
older machine of mine that is running a 1.8.1 version of Ruby that
doesn't have readline and it worked as I would expect.

Weird?

Yep, it's got me stumped . . but I have never used the Ruby debugger
before and I thought I might be doing something dumb . .

Thanks,

Phil.

···

On Thu, 2006-06-22 at 06:18 +0900, Dale Martenson wrote:

> On Tue, 2006-06-20 at 23:54 +0900, Dale Martenson wrote:

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au

Logan,

$stdin.each_line do |line|
        puts "BUBBA: "+line
end

You know, if you use ARGF instead of $stdin your program will work for

ruby a.rb some_file

and cat some_file | ./a.rb

OK . . but how does that help when trying to use "ruby -r debug? - I
still get the previous problems . .

Thanks,

Phil.

It doesn't apparently, I only mention it as a useful idiom for when you want a program to like that :wink:

···

On Jun 22, 2006, at 4:57 AM, Philip Rhoades wrote:

On Thu, 2006-06-22 at 04:18 +0900, Logan Capaldo wrote:

On Jun 20, 2006, at 2:53 PM, Philip Rhoades wrote:

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: phil@pricom.com.au