a few remarks about debugging in r

Upload: elz0rr0

Post on 03-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 A Few Remarks About Debugging in R

    1/44

    A Few Remarks About Debugging in R

    Norm Matloff

    Dept. of Computer ScienceUniversity of California, Davis

    October 13, 2009

    http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    2/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD.

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    3/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    4/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    (Will release my Rdsm shared-memory parallel R packageto CRAN in the next couple of weeks, much improved over

    the version.)

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    5/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    (Will release my Rdsm shared-memory parallel R packageto CRAN in the next couple of weeks, much improved over

    the version.) Im a longtime Linux fan/promoter. I use it for everything,

    both professional and personal.

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    6/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    (Will release my Rdsm shared-memory parallel R packageto CRAN in the next couple of weeks, much improved over

    the version.) Im a longtime Linux fan/promoter. I use it for everything,

    both professional and personal.

    Have been programming since I was 17.

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    7/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    (Will release my Rdsm shared-memory parallel R packageto CRAN in the next couple of weeks, much improved over

    the version.) Im a longtime Linux fan/promoter. I use it for everything,

    both professional and personal.

    Have been programming since I was 17. (FORTRAN,punched cards.)

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    8/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    (Will release my Rdsm shared-memory parallel R packageto CRAN in the next couple of weeks, much improved over

    the version.) Im a longtime Linux fan/promoter. I use it for everything,

    both professional and personal.

    Have been programming since I was 17. (FORTRAN,punched cards.)

    GUIs and IDEs are nice, but: I didnt grow up with it. Not a necessity to me like it is for

    most new grads today.

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    9/44

    Where Im Coming From

    My sources of bias:

    Early career was as a statistics professor at UCD. UCD CS Dept. since 1983. Main research areas:

    probabilistic/statistical modeling of computer systems;

    parallel processing; computer architecture and networks;

    Chinese-language computing.

    (Will release my Rdsm shared-memory parallel R packageto CRAN in the next couple of weeks, much improved over

    the version.) Im a longtime Linux fan/promoter. I use it for everything,

    both professional and personal.

    Have been programming since I was 17. (FORTRAN,punched cards.)

    GUIs and IDEs are nice, but: I didnt grow up with it. Not a necessity to me like it is for

    most new grads today. I like to use the same text editor for everything I do.

    http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    10/44

    My Biases (contd.)

    Debugging is one of the most underrated software skills.

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    11/44

    My Biases (contd.)

    Debugging is one of the most underrated software skills.

    I may not think IDEs are important, but debugging tools are

    crucial.

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    12/44

    My Biases (contd.)

    Debugging is one of the most underrated software skills.

    I may not think IDEs are important, but debugging tools are

    crucial.Thats why Pete Salzman and I wrote our book on

    debugging.

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    13/44

    My Biases (contd.)

    Debugging is one of the most underrated software skills.

    I may not think IDEs are important, but debugging tools are

    crucial.Thats why Pete Salzman and I wrote our book on

    debugging.

    Rs debugging tools have been its weakest link.

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    14/44

    My Biases (contd.)

    Debugging is one of the most underrated software skills.

    I may not think IDEs are important, but debugging tools are

    crucial.Thats why Pete Salzman and I wrote our book on

    debugging.

    Rs debugging tools have been its weakest link.

    I thus am delighted that REvolution Computing is stepping

    into the void. I hope they or others go to open source/cross

    platform.

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    15/44

    What Has Been Available?

    Rs built-in debug() function: non-GUI, limited capabilities

    but serviceable. Somewhat improved in R 2.10.0.

    Wh H B A il bl ?

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    16/44

    What Has Been Available?

    Rs built-in debug() function: non-GUI, limited capabilities

    but serviceable. Somewhat improved in R 2.10.0.

    Rs built-in trace(), browser(), recovery() etc. functions:

    These add finer control, e.g. conditional breakpoints, crash

    post mortems, etc.

    Wh H B A il bl ?

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    17/44

    What Has Been Available?

    Rs built-in debug() function: non-GUI, limited capabilities

    but serviceable. Somewhat improved in R 2.10.0.

    Rs built-in trace(), browser(), recovery() etc. functions:

    These add finer control, e.g. conditional breakpoints, crash

    post mortems, etc. Mark Bravingtons debug package: GUI view of ones

    source code as one traverses it. But cant use mouse to

    click-and-set breakpoints, query values of variables, etc.

    Wh t H B A il bl ?

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    18/44

    What Has Been Available?

    Rs built-in debug() function: non-GUI, limited capabilities

    but serviceable. Somewhat improved in R 2.10.0.

    Rs built-in trace(), browser(), recovery() etc. functions:

    These add finer control, e.g. conditional breakpoints, crash

    post mortems, etc. Mark Bravingtons debug package: GUI view of ones

    source code as one traverses it. But cant use mouse to

    click-and-set breakpoints, query values of variables, etc.

    (Mark just informed me today hes preparing an update to

    the package.)

    D id t f D b i T l

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    19/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Desiderata for Deb gging Tools

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    20/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Desiderata for Debugging Tools

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    21/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Desiderata for Debugging Tools

    http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    22/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Not in the above tools.

    Desiderata for Debugging Tools

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    23/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Not in the above tools.

    User-customizable: Ability to program macros, etc.

    Desiderata for Debugging Tools

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    24/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Not in the above tools.

    User-customizable: Ability to program macros, etc.

    Doable in above tools via trace() and browser().

    Desiderata for Debugging Tools

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    25/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Not in the above tools.

    User-customizable: Ability to program macros, etc.

    Doable in above tools via trace() and browser().

    GUI. Most people other than me really want it (me too, to a

    large degree).

    Desiderata for Debugging Tools

    http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    26/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Not in the above tools.

    User-customizable: Ability to program macros, etc.

    Doable in above tools via trace() and browser().

    GUI. Most people other than me really want it (me too, to a

    large degree).

    Watchpoints. Run until specified variable changes value.

    Fairly easy to implement in an interpreted language like R.

    Desiderata for Debugging Tools

    http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    27/44

    Desiderata for Debugging Tools

    Viewable breakpoints.

    Not in the above tools. (An example in my forthcoming Rbook has a kludge fix for this.)

    Saveable debugging sessions. So can come back the next

    day for more torture without setup time. :-)

    Not in the above tools.

    User-customizable: Ability to program macros, etc.

    Doable in above tools via trace() and browser().

    GUI. Most people other than me really want it (me too, to a

    large degree).

    Watchpoints. Run until specified variable changes value.

    Fairly easy to implement in an interpreted language like R.

    Ability to debug parallel code (next slide).

    Debugging Parallel Code

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    28/44

    Debugging Parallel Code

    Debugging Parallel Code

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    29/44

    Debugging Parallel Code

    Debugging of parallel processes much harder than for

    single process.

    Debugging Parallel Code

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    30/44

    Debugging Parallel Code

    Debugging of parallel processes much harder than for

    single process.

    Not many tools out there even for C/C++. TotalView

    (commercial), XMPI (open source, for MPI).

    Debugging Parallel Code

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    31/44

    Debugging Parallel Code

    Debugging of parallel processes much harder than for

    single process.

    Not many tools out there even for C/C++. TotalView

    (commercial), XMPI (open source, for MPI). Screen footprint problem: If have n processes, that means

    n windows. Problem is compounded if use GUI.

    Debugging Parallel Code

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    32/44

    gg g

    Debugging of parallel processes much harder than for

    single process.

    Not many tools out there even for C/C++. TotalView

    (commercial), XMPI (open source, for MPI). Screen footprint problem: If have n processes, that means

    n windows. Problem is compounded if use GUI.

    Many existing parallel R platforms make parallel debugging

    very difficult, due to lack of terminals for the processes.

    (One of my Rdsm modes allows it.)

    Tips on Debugging R

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    33/44

    p gg g

    Tips on Debugging R

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    34/44

    p gg g

    Sorry, no magic bullets.

    Tips on Debugging R

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    35/44

    p gg g

    Sorry, no magic bullets.

    Fundamental principles of debugging:

    Tips on Debugging R

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    36/44

    Sorry, no magic bullets.

    Fundamental principles of debugging:

    Principle of Confirmation:

    Tips on Debugging R

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    37/44

    Sorry, no magic bullets.

    Fundamental principles of debugging:

    Principle of Confirmation: Even though you are sure acertain variable has a certain value, or that a certainif-condition holds, confirm it.

    Tips on Debugging R

    http://goforward/http://find/http://goback/
  • 7/28/2019 A Few Remarks About Debugging in R

    38/44

    Sorry, no magic bullets.

    Fundamental principles of debugging:

    Principle of Confirmation: Even though you are sure acertain variable has a certain value, or that a certainif-condition holds, confirm it.

    Start small: Try the program on a small vector, maybe inwith a scaled-down version of the program itself.

    Tips on Debugging R

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    39/44

    Sorry, no magic bullets.

    Fundamental principles of debugging:

    Principle of Confirmation: Even though you are sure acertain variable has a certain value, or that a certainif-condition holds, confirm it.

    Start small: Try the program on a small vector, maybe inwith a scaled-down version of the program itself.

    Top-down approach: When debugging f() which calls g(),dont follow calls to g() at first. Check first whether thereturn value of g() is correct.

    Tips on Debugging R

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    40/44

    Sorry, no magic bullets.

    Fundamental principles of debugging:

    Principle of Confirmation: Even though you are sure acertain variable has a certain value, or that a certainif-condition holds, confirm it.

    Start small: Try the program on a small vector, maybe inwith a scaled-down version of the program itself.

    Top-down approach: When debugging f() which calls g(),dont follow calls to g() at first. Check first whether thereturn value of g() is correct.

    Use binary search: Say you have a syntax error thats

    baffling you. Comment-out half the code of the function, tosee if the error disappears. Then comment-out half of thehalf that triggers the error, etc.

    Tips (contd.)

    http://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    41/44

    Have a boolean global variable, say dbg, that turns

    debugging on and off, and then insert breakpoints with

    something like

    if (dbg) browser()

    Tips (contd.)

    http://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    42/44

    Have a boolean global variable, say dbg, that turns

    debugging on and off, and then insert breakpoints with

    something like

    if (dbg) browser()

    My aforementioned kludge may help you organize better,

    e.g. keep track of your breakpoints. Download from http://heather.cs.ucdavis.edu/DebugKludge.r .

    Tips (contd.)

    http://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    43/44

    Have a boolean global variable, say dbg, that turns

    debugging on and off, and then insert breakpoints with

    something like

    if (dbg) browser()

    My aforementioned kludge may help you organize better,

    e.g. keep track of your breakpoints. Download from http://heather.cs.ucdavis.edu/DebugKludge.r .

    If you are using a terminal-less parallel R package and are

    forced to use print statements in lieu of a debugging tool,

    use message() instead of print(). (The latter may not

    actually print.)

    Tips (contd.)

    http://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://find/
  • 7/28/2019 A Few Remarks About Debugging in R

    44/44

    Have a boolean global variable, say dbg, that turns

    debugging on and off, and then insert breakpoints with

    something like

    if (dbg) browser()

    My aforementioned kludge may help you organize better,

    e.g. keep track of your breakpoints. Download from http://heather.cs.ucdavis.edu/DebugKludge.r .

    If you are using a terminal-less parallel R package and are

    forced to use print statements in lieu of a debugging tool,

    use message() instead of print(). (The latter may not

    actually print.)

    Use a good text editor, with syntax highlighting, matching

    for braces etc., infinite undo, comment/uncomment, etc.

    This saves typing, so you are distracted less from the task

    at hand.

    http://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://heather.cs.ucdavis.edu/DebugKludge.rhttp://goforward/http://find/http://goback/