1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
|
# GUIDE
## Introduction
### First Steps
A handful of commands are enough to get started using `byebug`. The following
session illustrates these commands. Take the following sample file:
```ruby
#
# The n'th triangle number: triangle(n) = n*(n+1)/2 = 1 + 2 + ... + n
#
def triangle(n)
tri = 0
0.upto(n) { |i| tri += i }
tri
end
t = triangle(3)
puts t
```
Let's debug it.
```console
$ byebug /path/to/triangle.rb
[1, 10] in /path/to/triangle.rb
1: #
2: # The n'th triangle number: triangle(n) = n*(n+1)/2 = 1 + 2 + ... + n
3: #
=> 4: def triangle(n)
5: tri = 0
6:
7: 0.upto(n) { |i| tri += i }
8:
9: tri
10: end
(byebug)
```
We are currently stopped before the first executable line of the program: line 4
of `triangle.rb`. If you are used to less dynamic languages and have used
debuggers for more statically compiled languages like C, C++, or Java, it may
seem odd to be stopped before a function definition but in Ruby line 4 is
executed.
Byebug's prompt is `(byebug)`. If the program has died and you are in
post-mortem debugging, `(byebug:post-mortem)` is used instead. If the program
has terminated normally and the `--no-quit` option has been specified in the
command line, the prompt will be `(byebug:ctrl)` instead. The commands available
change depending on the program's state.
Byebug automatically lists 10 lines of code centered around the current line
every time it is stopped. The current line is marked with `=>`. If the range
would overflow the beggining or the end of the file, byebug will move it
accordingly so that only actual real lines of code are displayed.
Now let us step through the program.
```console
(byebug) step
[5, 14] in /path/to/triangle.rb
5: tri = 0
6:
7: 0.upto(n) { |i| tri += i }
8:
9: tri
10: end
11:
=> 12: t = triangle(3)
13: puts t
(byebug) <RET> # hit enter
[1, 10] in /path/to/triangle.rb
1: #
2: # The n'th triangle number: triangle(n) = n*(n+1)/2 = 1 + 2 + ... + n
3: #
4: def triangle(n)
=> 5: tri = 0
6:
7: 0.upto(n) { |i| tri += i }
8:
9: tri
10: end
(byebug) eval tri
nil
(byebug) step
[2, 11] in /path/to/triangle.rb
2: # The n'th triangle number: triangle(n) = n*(n+1)/2 = 1 + 2 + ... + n
3: #
4: def triangle(n)
5: tri = 0
6:
=> 7: 0.upto(n) { |i| tri += i }
8:
9: tri
10: end
11:
(byebug) eval tri
0
```
The first `step` command runs the script one executable unit. The second command
we entered was just hitting the return key: `byebug` remembers the last command
you entered was `step` and runs it again.
One way to print the values of variables is `eval` (there are other ways). When we
look at the value of `tri` the first time, we see it is `nil`. Again we are
stopped _before_ the assignment on line 5, and this variable hadn't been set
previously. However after issuing another `step` command we see that the value
is 0 as expected. If every time we stop we want to see the value of `tri` to see
how things are going, there is a better way by setting a display expression:
```console
(byebug) display tri
1: tri = 0
```
Now let us run the program until right before we return from the function. We'll
want to see which lines get run, so we turn on _line tracing_. If we don't want
whole paths to be displayed when tracing, we can turn on _basename_.
```console
(byebug) set linetrace
linetrace is on
(byebug) set basename
basename is on
(byebug) finish 0
Tracing: triangle.rb:7 0.upto(n) { |i| tri += i }
1: tri = 0
Tracing: triangle.rb:7 0.upto(n) { |i| tri += i }
1: tri = 0
Tracing: triangle.rb:7 0.upto(n) { |i| tri += i }
1: tri = 1
Tracing: triangle.rb:7 0.upto(n) { |i| tri += i }
1: tri = 3
Tracing: triangle.rb:9 tri
1: tri = 6
1: tri = 6
[4, 13] in /home/davidr/Proyectos/byebug/triangle.rb
4: def triangle(n)
5: tri = 0
6:
7: 0.upto(n) { |i| tri += i }
8:
9: tri
=> 10: end
11:
12: t = triangle(3)
13: puts t
(byebug) quit
Really quit? (y/n)
y
```
So far, so good. As you can see from the above, to get out of `byebug`, one
can issue a `quit` command (or the abbreviation `q`). If you want to quit
without being prompted, suffix the command with an exclamation mark, e.g., `q!`.
### Second Sample Session: Delving Deeper
In this section we'll introduce breakpoints, the call stack and restarting.
Below we will debug a simple Ruby program to solve the classic Towers of Hanoi
puzzle. It is augmented by the bane of programming: some command-parameter
processing with error checking.
```ruby
#
# Solves the classic Towers of Hanoi puzzle.
#
def hanoi(n, a, b, c)
hanoi(n - 1, a, c, b) if n - 1 > 0
puts "Move disk #{a} to #{b}"
hanoi(n - 1, c, b, a) if n - 1 > 0
end
n_args = $ARGV.length
raise("*** Need number of disks or no parameter") if n_args > 1
n = 3
if n_args > 0
begin
n = $ARGV[0].to_i
rescue ValueError
raise("*** Expecting an integer, got: #{$ARGV[0]}")
end
end
raise("*** Number of disks should be between 1 and 100") if n < 1 || n > 100
hanoi(n, :a, :b, :c)
```
Recall in the first section it was stated that before the `def` is run, the
method it names is undefined. Let's check that out. First let's see what
private methods we can call before running `def hanoi`.
```console
$ byebug path/to/hanoi.rb
1: #
2: # Solves the classic Towers of Hanoi puzzle.
3: #
4: def hanoi(n, a, b, c)
5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
(byebug) private_methods
public
private
include
using
define_method
default_src_encoding
DelegateClass
Digest
timeout
initialize_copy
initialize_dup
initialize_clone
sprintf
format
Integer
Float
String
Array
Hash
warn
raise
fail
global_variables
__method__
__callee__
__dir__
eval
local_variables
iterator?
block_given?
catch
throw
loop
respond_to_missing?
trace_var
untrace_var
at_exit
syscall
open
printf
print
putc
puts
gets
readline
select
readlines
`
p
test
srand
rand
trap
load
require
require_relative
autoload
autoload?
proc
lambda
binding
caller
caller_locations
exec
fork
exit!
system
spawn
sleep
exit
abort
Rational
Complex
set_trace_func
gem_original_require
Pathname
pp
y
URI
rubygems_require
initialize
singleton_method_added
singleton_method_removed
singleton_method_undefined
method_missing
(byebug) private_methods.member?(:hanoi)
false
```
`private_methods` is not a byebug command but a Ruby feature. By default, when
`byebug` doesn't understand a command, it will evaluate it as if it was a Ruby
command. You can use any Ruby to inspect your program's state at the place it
is stopped.
Now let's see what happens after stepping:
```console
(byebug) step
[5, 14] in /path/to/hanoi.rb
5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
11:
=> 12: n_args = $ARGV.length
13:
14: raise("*** Need number of disks or no parameter") if n_args > 1
(byebug) private_methods.member?(:hanoi)
true
(byebug)
```
Okay, lets go on and talk about program arguments.
```console
(byebug) $ARGV
[]
```
Oops. We forgot to specify any parameters to this program. Let's try again. We
can use the `restart` command here.
```console
(byebug) restart 3
Re exec'ing:
/path/to/exe/byebug /path/to/hanoi.rb 3
[1, 10] in /path/to/hanoi.rb
1: #
2: # Solves the classic Towers of Hanoi puzzle.
3: #
=> 4: def hanoi(n, a, b, c)
5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
(byebug) break 5
Created breakpoint 1 at /path/to/hanoi.rb:5
(byebug) continue
Stopped by breakpoint 1 at /path/to/hanoi.rb:5
[1, 10] in /path/to/hanoi.rb
1: #
2: # Solves the classic Towers of Hanoi puzzle.
3: #
4: def hanoi(n, a, b, c)
=> 5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
(byebug) display n
1: n = 3
(byebug) display a
2: a = :a
(byebug) display b
3: b = :b
(byebug) undisplay 3
(byebug) continue
Stopped by breakpoint 1 at /path/to/hanoi.rb:5
1: n = 2
2: a = :a
[1, 10] in /path/to/hanoi.rb
1: #
2: # Solves the classic Towers of Hanoi puzzle.
3: #
4: def hanoi(n, a, b, c)
=> 5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
(byebug) c
Stopped by breakpoint 1 at /path/to/hanoi.rb:5
1: n = 1
2: a = :a
[1, 10] in /path/to/hanoi.rb
1: #
2: # Solves the classic Towers of Hanoi puzzle.
3: #
4: def hanoi(n, a, b, c)
=> 5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
(byebug) set nofullpath
fullpath is off
(byebug) where
--> #0 Object.hanoi(n#Fixnum, a#Symbol, b#Symbol, c#Symbol) at .../shortpath/to/hanoi.rb:5
#1 Object.hanoi(n#Fixnum, a#Symbol, b#Symbol, c#Symbol) at .../shortpath/to/hanoi.rb:5
#2 <top (required)> at .../Proyectos/byebug/hanoi.rb:28
(byebug)
```
In the above we added new commands: `break` (see [breakpoints]()), which
indicates to stop just before that line of code is run, and `continue`, which
resumes execution. To remove a display expression `undisplay` is used. If we
give a display number, just that display expression is removed.
We also used a new command `where`(see [backtrace]()) to show the callstack. In
the above situation, starting from the bottom line we see we called the `hanoi`
method from line 28 of the file `hanoi.rb` and the `hanoi` method called itself
two more times at line 5.
In the callstack we show a _current frame_ mark, the frame number, the method
being called, the names of the parameters, the types those parameters
_currently_ have and the file-line position. Remember it's possible that when
the program was called the parameters had different types, since the types of
variables can change dynamically. You can alter the style of what to show in the
trace (see [callstyle]()).
Now let's move around the callstack.
```console
(byebug) undisplay
Clear all expressions? (y/n) y
(byebug) n_args
NameError Exception: undefined local variable or method `n_args' for main:Object
(byebug) frame 2
[19, 28] in /path/to/hanoi.rb
19: begin
20: n = $ARGV[0].to_i
21: rescue ValueError
22: raise("*** Expecting an integer, got: #{$ARGV[0]}")
23: end
24: end
25:
26: raise("*** Number of disks should be between 1 and 100") if n < 1 || n > 100
27:
=> 28: hanoi(n, :a, :b, :c)
(byebug) n_args
1
(byebug) eval n
3
(byebug) down 2
[1, 10] in /path/to/hanoi.rb
1: #
2: # Solves the classic Towers of Hanoi puzzle.
3: #
4: def hanoi(n, a, b, c)
=> 5: hanoi(n - 1, a, c, b) if n - 1 > 0
6:
7: puts "Move disk #{a} to #{b}"
8:
9: hanoi(n - 1, c, b, a) if n - 1 > 0
10: end
(byebug) eval n
2
```
Notice in the above to get the value of variable `n` we had to use a print
command like `eval n`. If we entered just `n`, that would be taken to mean byebug
command `next`. In the current scope, variable `n_args` is not defined. However
I can change to the top-most frame by using the `frame 2` command. Notice that
inside frame #2, the value of `n_args` can be shown. Also note that the value of
variable `n` is different.
### Attaching to a running program with `byebug`
In the previous sessions we've been calling byebug right at the outset, but
there is another mode of operation you might use. If there's a lot of code that
needs to be run before the part you want to inspect, it might not be efficient
or convenient to run byebug from the outset.
In this section we'll show how to enter the code in the middle of your program,
while delving more into byebug's operation. We will also use unit testing. Using
unit tests will greatly reduce the amount of debugging needed, while at the same
time, will increase the quality of your program.
What we'll do is take the `triangle` code from the first session and write a
unit test for that. In a sense we did write a tiny test for the program which
was basically the last line where we printed the value of `triangle(3)`. This
test however wasn't automated: the expectation is that someone would look at the
output and verify that what was printed is what was expected.
Before we can turn that into something that can be `required`, we probably want
to remove that output. However I like to keep in that line so that when I
look at the file, I have an example of how to run it. Therefore we will
conditionally run this line if that file is invoked directly, but skip it if it
is not. _NOTE: `byebug` resets `$0` to try to make things like this work._
```ruby
if __FILE__ == $PROGRAM_NAME
t = triangle(3)
puts t
end
```
Okay, we're now ready to write our unit test and we'll use the `minitest`
framework for that. Here's the test code, it should be placed in the same
directory as `triangle.rb`.
```ruby
require "minitest/autorun"
require_relative "triangle.rb"
class TestTriangle < Minitest::Test
def test_basic
solutions = []
0.upto(5) { |i| solutions << triangle(i) }
assert_equal([0, 1, 3, 6, 10, 15], solutions, "First 5 triangle numbers")
end
end
```
Let's say we want to stop before the first statement in our test method, we'll
add the following:
```ruby
...
def test_basic
byebug
solutions = []
...
```
Now we run the program, requiring `byebug`
```console
$ ruby -rbyebug test_triangle.rb
Run options: --seed 31679
# Running:
[2, 11] in test_triangle.rb
2: require_relative "triangle.rb"
3:
4: class TestTriangle < Minitest::Test
5: def test_basic
6: byebug
=> 7: solutions = []
8:
9: 0.upto(5) { |i| solutions << triangle(i) }
10:
11: assert_equal([0, 1, 3, 6, 10, 15], solutions, "First 5 triangle numbers")
(byebug)
```
and we see that we are stopped at line 7 just before the initialization of the
list `solutions`.
Now let's see where we are...
```console
(byebug) set nofullpath
Displaying frame's full file names is off.
(byebug) bt
--> #0 TestTriangle.test_basic at .../Proyectos/byebug/test_triangle.rb:7
#1 block (3 levels) in Minitest::Test.run at .../lib/minitest/test.rb:108
#2 Minitest::Test.capture_exceptions at .../lib/minitest/test.rb:206
#3 block (2 levels) in Minitest::Test.run at .../lib/minitest/test.rb:105
#4 Minitest::Test.time_it at .../lib/minitest/test.rb:258
#5 block in Minitest::Test.run at .../lib/minitest/test.rb:104
#6 #<Class:Minitest::Runnable>.on_signal(name#String, action#Proc) at .../minitest-5.5.0/lib/minitest.rb:321
#7 Minitest::Test.with_info_handler(&block#Proc) at .../lib/minitest/test.rb:278
#8 Minitest::Test.run at .../lib/minitest/test.rb:103
#9 #<Class:Minitest>.run_one_method(klass#Class, method_name#String) at .../minitest-5.5.0/lib/minitest.rb:768
#10 #<Class:Minitest::Runnable>.run_one_method(klass#Class, method_name#String, reporter#Minitest::CompositeReporter) at .../minitest-5.5.0/lib/minitest.rb:295
#11 block (2 levels) in #<Class:Minitest::Runnable>.run(reporter#Minitest::CompositeReporter, options#Hash) at .../minitest-5.5.0/lib/minitest.rb:289
ͱ-- #12 Array.each at .../minitest-5.5.0/lib/minitest.rb:288
#13 block in #<Class:Minitest::Runnable>.run(reporter#Minitest::CompositeReporter, options#Hash) at .../minitest-5.5.0/lib/minitest.rb:288
#14 #<Class:Minitest::Runnable>.on_signal(name#String, action#Proc) at .../minitest-5.5.0/lib/minitest.rb:321
#15 #<Class:Minitest::Runnable>.with_info_handler(reporter#Minitest::CompositeReporter, &block#Proc) at .../minitest-5.5.0/lib/minitest.rb:308
#16 #<Class:Minitest::Runnable>.run(reporter#Minitest::CompositeReporter, options#Hash) at .../minitest-5.5.0/lib/minitest.rb:287
#17 block in #<Class:Minitest>.__run(reporter#Minitest::CompositeReporter, options#Hash) at .../minitest-5.5.0/lib/minitest.rb:150
ͱ-- #18 Array.map at .../minitest-5.5.0/lib/minitest.rb:150
#19 #<Class:Minitest>.__run(reporter#Minitest::CompositeReporter, options#Hash) at .../minitest-5.5.0/lib/minitest.rb:150
#20 #<Class:Minitest>.run(args#Array) at .../minitest-5.5.0/lib/minitest.rb:127
#21 block in #<Class:Minitest>.autorun at .../minitest-5.5.0/lib/minitest.rb:56
(byebug)
```
We get the same result as if we had run byebug from the outset.
### Debugging Oddities: How debugging Ruby may be different from other languages
If you are used to debugging in other languages like C, C++, Perl, Java or even
Bash (see [bashdb](http://bashdb.sourceforge.net)), there may be a number of things that
seem or feel a little bit different and may confuse you. A number of these
things aren't oddities of the debugger per se but differences in how Ruby works
compared to those other languages. Because Ruby works a little differently from
those other languages, writing a debugger has to also be a little different as
well if it is to be useful. In this respect, using Byebug may help you
understand Ruby better.
We've already seen one such difference: the fact that we stop on method
definitions or `def`'s and that is because these are in fact executable
statements. In other compiled languages this would not happen because that's
already been done when you compile the program (or in Perl when it scans in the
program). In this section we'll consider some other things that might throw off
new users to Ruby who are familiar with other languages and debugging in them.
#### Bouncing Around in Blocks (iterators)
When debugging languages with coroutines like Python and Ruby, a method call may
not necessarily go to the first statement after the method header. It's possible
that the call will continue after a `yield` statement from a prior call.
```ruby
#
# Enumerator for primes
#
class SievePrime
def initialize
@odd_primes = []
end
def next_prime
candidate = 2
yield candidate
not_prime = false
candidate += 1
loop do
@odd_primes.each do |p|
not_prime = (0 == (candidate % p))
break if not_prime
end
unless not_prime
@odd_primes << candidate
yield candidate
end
candidate += 2
end
end
end
SievePrime.new.next_prime do |prime|
puts prime
break if prime > 10
end
```
```console
$ byebug primes.rb
[1, 10] in /path/to/primes.rb
1: #
2: # Enumerator for primes
3: #
=> 4: class SievePrime
5: def initialize
6: @odd_primes = []
7: end
8:
9: def self.next_prime(&block)
10: candidate = 2
(byebug) set linetrace
line tracing is on.
(byebug) set basename
basename in on.
(byebug) step 9
Tracing: primes.rb:5 def initialize
Tracing: primes.rb:9 def next_prime
Tracing: primes.rb:31 SievePrime.new.next_prime do |prime|
Tracing: primes.rb:6 @odd_primes = []
Tracing: primes.rb:10 candidate = 2
Tracing: primes.rb:11 yield candidate
Tracing: primes.rb:32 puts prime
2
Tracing: primes.rb:33 break if prime > 10
Tracing: primes.rb:12 not_prime = false
[7, 16] in /path/to/primes.rb
7: end
8:
9: def next_prime
10: candidate = 2
11: yield candidate
=> 12: not_prime = false
13: candidate += 1
14:
15: loop do
16: @odd_primes.each do |p|
17: not_prime = (0 == (candidate % p))
(byebug)
```
The loop between lines 31-34 gets interleaved between those of
`SievePrime#next_prime`, lines 9-28 above.
#### No Parameter Values in a Call Stack
In traditional debuggers, in a call stack you can generally see the names of the
parameters and the values that were passed in.
Ruby is a very dynamic language and it tries to be efficient within the confines
of the language definition. Values generally aren't taken out of a variable or
expression and pushed onto a stack. Instead a new scope is created and the
parameters are given initial values. Parameter passing is by _reference_ not by
_value_ as it is say Algol, C, or Perl. During the execution of a method,
parameter values can change (and often do). In fact even the _class_ of the
object can change.
So at present, the name of the parameter is shown. The call-style setting
([callstyle]()) can be used to set whether the name is shown or the name and the
_current_ class of the object.
#### Lines You Can Stop At
Consider the following little Ruby program.
```ruby
"Yes it does" =~ /
(Yes) \s+
it \s+
does
/ix
puts $1
```
The stopping points that Ruby records are the last two lines, lines 5 and 6.
Inside `byebug` you can get a list of stoppable lines for a file using the `info
file` command.
### Threading support
Byebug supports debugging Ruby programs making use of multiple threads.
Let's consider the following sample program:
```ruby
class Company
def initialize(task)
@tasks, @results = Queue.new, Queue.new
@tasks.push(task)
end
def run
manager = Thread.new { manager_routine }
employee = Thread.new { employee_routine }
sleep 6
go_home(manager)
go_home(employee)
end
#
# An employee doing his thing
#
def employee_routine
loop do
if @tasks.empty?
have_a_break(0.1)
else
work_hard(@tasks.pop)
end
end
end
#
# A manager doing his thing
#
def manager_routine
loop do
if @results.empty?
have_a_break(1)
else
show_off(@results.pop)
end
end
end
private
def show_off(result)
puts result
end
def work_hard(task)
task ** task
end
def have_a_break(amount)
sleep amount
end
def go_home(person)
person.kill
end
end
Company.new(10).run
```
The `Company` class simulates a real company. The company has a manager and an
employee represented by 2 threads: they work concurrently to achieve the
company's targets.
* The employee looks for tasks to complete. If there are tasks, it works hard to
complete them. Otherwise he has a quick break.
```ruby
#
# An employee doing his thing
#
def employee_routine
loop do
if @tasks.empty?
have_a_break(0.1)
else
work_hard(@tasks.pop)
end
end
end
```
* The manager, on the other hand, sits there all day and sporadically checks
whether there are any results to show off.
```ruby
#
# A manager doing his thing
#
def manager_routine
loop do
if @results.empty?
have_a_break(1)
else
show_off(@results.pop)
end
end
end
```
We do some abstractions easily readable in the code. Our tasks are just a
`Queue` of numbers, so are our results. What our employer does when he works is
some calculation with those numbers and what the manager does with the results
is printing them to the screen.
We instantiate a new company with an initial task and after running that
company we expect the result to be printed in the screen, but it is not. Lets
debug our sample program:
```console
[1, 10] in /path/to/company.rb
=> 1: class Company
2: def initialize(task)
3: @tasks, @results = Queue.new, Queue.new
4:
5: @tasks.push(task)
6: end
7:
8: def run
9: manager = Thread.new { manager_routine }
10: employee = Thread.new { employee_routine }
(byebug) l
[11, 20] in /path/to/company.rb
11:
12: sleep 6
13:
14: go_home(manager)
15: go_home(employee)
16: end
17:
18: #
19: # An employee doing his thing
20: #
(byebug) c 12
Stopped by breakpoint 1 at /path/to/company.rb:12
[7, 16] in /path/to/company.rb
7:
8: def run
9: manager = Thread.new { manager_routine }
10: employee = Thread.new { employee_routine }
11:
=> 12: sleep 6
13:
14: go_home(manager)
15: go_home(employee)
16: end
(byebug) th l
+ 1 #<Thread:0x0000000192f328 run> /path/to/company.rb:12
2 #<Thread:0x00000001ff9870@/path/to/company.rb:9 sleep>
3 #<Thread:0x00000001ff80d8@/path/to/company.rb:10 sleep>
```
What we have done here is just start our program and advance to the point
inmediately after our `employee` and `manager` threads have been created. We
can then check that the threads are there using the `thread list` command. Now
we want to debug both of this threads to check what's happening and look for the
bug.
```console
(byebug) th switch 3
[5, 14] in /path/to/company.rb
5: @tasks.push(task)
6: end
7:
8: def run
9: manager = Thread.new { manager_routine }
=> 10: employee = Thread.new { employee_routine }
11:
12: sleep 6
13:
14: go_home(manager)
(byebug) th stop 1; th stop 2
$ 1 #<Thread:0x00000001307310 sleep> /path/to/company.rb:12
$ 2 #<Thread:0x000000018bf438 sleep> /path/to/company.rb:9
(byebug) th l
$ 1 #<Thread:0x00000001307310 sleep> /path/to/company.rb:12
$ 2 #<Thread:0x000000018bf438@/path/to/company.rb:9 sleep> /path/to/company.rb:55
+ 3 #<Thread:0x00000001ff80d8@/path/to/company.rb:10 sleep> /path/to/company.rb:10
```
We have started by debugging the `employee` thread. To do that, we switch to
that thread using the `thread switch 3` command. The thread number is the one
specified by `thread list`, we know this is our worker thread because `thread
list` specifies where the thread is defined in the file (and its current
position if the thread is currently running).
After that we stopped the main thread and the worker thread, using the command
`thread stop`. We do this because we want to focus on the employee thread first
and don't want the program to finish while we are debugging. Notice that stopped
threads are marked with the "$" symbol whereas the current thread is marked with
the "+" symbol.
```console
(byebug) s
[17, 26] in /path/to/company.rb
17:
18: #
19: # An employee doing his thing
20: #
21: def employee_routine
=> 22: loop do
23: if @tasks.empty?
24: have_a_break(0.1)
25: else
26: work_hard(@tasks.pop)
(byebug) s
[18, 27] in /path/to/company.rb
18: #
19: # An employee doing his thing
20: #
21: def employee_routine
22: loop do
=> 23: if @tasks.empty?
24: have_a_break(0.1)
25: else
26: work_hard(@tasks.pop)
27: end
(byebug) n
[21, 30] in /path/to/company.rb
21: def employee_routine
22: loop do
23: if @tasks.empty?
24: have_a_break(0.1)
25: else
=> 26: work_hard(@tasks.pop)
27: end
28: end
29: end
30:
(byebug) s
[49, 58] in /path/to/company.rb
49: def show_off(result)
50: puts result
51: end
52:
53: def work_hard(task)
=> 54: task ** task
55: end
56:
57: def have_a_break(amount)
58: sleep amount
(byebug) s
[21, 30] in /path/to/company.rb
21: #
22: # An employee doing his thing
23: #
24: def employee_routine
25: loop do
=> 26: if @tasks.empty?
27: have_a_break(0.1)
28: else
29: work_hard(@tasks.pop)
30: end
(byebug) n
[22, 31] in /path/to/company.rb
22: # An employee doing his thing
23: #
24: def employee_routine
25: loop do
26: if @tasks.empty?
=> 27: have_a_break(0.1)
28: else
29: work_hard(@tasks.pop)
30: end
31: end
(byebug) n
[21, 30] in /path/to/company.rb
21: #
22: # An employee doing his thing
23: #
24: def employee_routine
25: loop do
=> 26: if @tasks.empty?
27: have_a_break(0.1)
28: else
29: work_hard(@tasks.pop)
30: end
31: end
(byebug)
```
Everything seems fine in this thread. The first iteration the employee will do
his job, and after that it will just check for new tasks and sleep. Let's debug
the manager task now:
```console
(byebug) th resume 2
2 #<Thread:0x000000019892d8@/path/to/company.rb:12 run> /path/to/company.rb:12
(byebug) th switch 2
2 #<Thread:0x000000019892d8@/path/to/company.rb:12 sleep> /path/to/company.rb:12
[7, 16] in /path/to/company.rb
7:
8: #
9: # A CEO running his company
10: #
11: def run
=> 12: manager = Thread.new { manager_routine }
13: employee = Thread.new { employee_routine }
14:
15: sleep 6
16:
(byebug)
```
We used the command `thread resume` to restart the manager's thread and then
switch to it using `thread switch`. It's important to resume the thread's
execution before switching to it, otherwise we'll get a hang because we cannot
run a sleeping thread.
Now we can investigate the problem in the employer's side:
```console
(byebug) s
[30, 39] in /path/to/company.rb
30:
31: #
32: # A manager doing his thing
33: #
34: def manager_routine
=> 35: loop do
36: if @results.empty?
37: have_a_break(1)
38: else
39: show_off(@results.pop)
(byebug) s
[31, 40] in /path/to/company.rb
31: #
32: # A manager doing his thing
33: #
34: def manager_routine
35: loop do
=> 36: if @results.empty?
37: have_a_break(1)
38: else
39: show_off(@results.pop)
40: end
(byebug) n
[32, 41] in /path/to/company.rb
32: # A manager doing his thing
33: #
34: def manager_routine
35: loop do
36: if @results.empty?
=> 37: have_a_break(1)
38: else
39: show_off(@results.pop)
40: end
41: end
(byebug) n
[31, 40] in /path/to/company.rb
31: #
32: # A manager doing his thing
33: #
34: def manager_routine
35: loop do
=> 36: if @results.empty?
37: have_a_break(1)
38: else
39: show_off(@results.pop)
40: end
(byebug)
```
Now we can see the problem, the `@results` variable is always empty! The
employee forgot to leave the results in his manager's deck. We fix it by
changing the line
```ruby
work_hard(@tasks.pop)
```
in the `employee_routine` method with the line
```ruby
@results << work_hard(@tasks.pop)
```
To be continued...
* More complex examples with objects, pretty printing and irb.
* Line tracing and non-interactive tracing.
* Post-mortem debugging.
## Getting in & out
### Starting byebug
There is a wrapper script called `byebug` which basically `require`'s the gem
then loads `byebug` before its argument (the program to be debugged) is started.
If you don't need to pass dash options to your program, which might be confused
with byebug options, then you don't need to add the `--`. To get a brief list of
options and descriptions, use the `--help` option.
```console
$ byebug --help
byebug 3.5.1
Usage: byebug [options] <script.rb> -- <script.rb parameters>
-d, --debug Set $DEBUG=true
-I, --include list Add paths to $LOAD_PATH
-m, --[no-]post-mortem Use post-mortem mode
-q, --[no-]quit Quit when script finishes
-x, --[no-]rc Run byebug initialization file
-s, --[no-]stop Stop when script is loaded
-r, --require file Require library before script
-R, --remote [host:]port Remote debug [host:]port
-t, --[no-]trace Turn on line tracing
-v, --version Print program version
-h, --help Display this message
```
Many options appear as a long option name, such as `--help` and a short one
letter option name, such as `-h`. The list of options is detailed below:
#### -h | --help
It causes `byebug` to print some basic help and exit.
#### -v | --version
It causes `byebug` to print its version number and exit.
#### -d | --debug
Sets `$DEBUG` to `true`. Compatible with Ruby's flag.
#### -I | --include path
Adds `path` to load path. `path` can be a single path or a colon separated path
list.
#### -m | --post-mortem
If your program raises an exception that isn't caught you can enter byebug for
inspection of what went wrong. You may also want to use this option in
conjunction with `--no-stop`. See also [Post-Mortem Debugging]().
#### --no-quit
Keep inside `byebug` after your program terminates normally.
#### --no-stop
Normally `byebug` stops before executing the first statement. If instead you
want it to start running initially and perhaps break it later in the execution,
use this option.
#### -r | --require lib
Requires the library before executing the script. This option is compatible
with Ruby's.
#### -t | --trace
Turns on line tracing. Running `byebug --trace <rubyscript>.rb` is pretty much
like running `ruby -rtracer <rubyscript>.rb`. If all you want to do however is
get a line trace, `tracer` is most likely faster than `byebug`.
```console
$ time byebug --trace --no-stop hanoi.rb > /dev/null
real 0m0.743s
user 0m0.668s
sys 0m0.068s
$ time ruby -rtracer hanoi.rb > /dev/null
real 0m0.077s
user 0m0.072s
sys 0m0.004s
```
### Byebug default options
Byebug has many command-line options,; it seems that some people want to set
them differently from the defaults. For example, some people may want
`--no-quit` to be the default behavior. One could write a wrapper script or set
a shell alias to handle this.
### Command Files
A command file is a file of lines that are `byebug` commands. Comments (lines
starting with `#`) may also be included. An empty line in a command file does
nothing; it does not mean to repeat the last command, as it would from the
terminal.
When you start `byebug`, it automatically executes commands from its
_init file_, called `.byebugrc`. During startup, `byebug` does the following:
* __Processes command line options and operands.__ Reads the init file in your
current directory, if any, and then checks your home directory. The home
directory is the directory named in the `$HOME` or `$HOMEPATH` environment
variable. Thus, you can have more than one init file, one generic in your home
directory, and another, specific to the program you are debugging, in the
directory where you invoke `byebug`.
You can also request the execution of a command file with the `source` command
(see [Source]()).
### Quitting byebug
To exit `byebug`, use the `quit` command (abbreviated to `q`). Normally, if you
are in an interactive session, this command will prompt to ask if you really
want to quit. If you want to quit without being prompted, enter `quit
unconditionally` (abbreviated to `q!`).
Another way to terminate byebug is to use the `kill` command. This does the
more forceful `kill -9`. It can be used in cases where `quit` doesn't work (I
haven't seen those yet).
### Calling byebug from inside your program
Running a program from byebug adds a bit of overhead and slows it down a little.
Furthermore, by necessity, debuggers change the operation of the program they
are debugging. And this can lead to unexpected and unwanted differences. It has
happened so often that the term
[Heisenbugs](https://en.wikipedia.org/wiki/Heisenbug) was coined to describe the
situation where using a debugger (among other possibilities) changes the
behavior of the program so that the bug doesn't manifest itself anymore.
There is another way to get into byebug which adds no overhead or slowdown until
you reach the point at which you want to start debugging. However here you must
change the script and make an explicit call to byebug. Because byebug isn't
involved before the first call, there is no overhead and the script will run
at the same speed as if there were no byebug.
To enter byebug this way, just drop `byebug` in whichever line you want to start
debugging at. You also have to require byebug somehow. If using bundler, it will
take care of that for you, otherwise you can use the ruby `-r` flag or add
`require "byebug"` in the line previous to the `byebug` call.
If speed is crucial, you may want to start and stop this around certain sections
of code, using `Byebug.start` and `Byebug.stop`. Alternatively, instead of
issuing an explicit `Byebug.stop` you can add a block to the `Byebug.start` and
debugging is turned on for that block. If the block of code raises an uncaught
exception that would cause the block to terminate, the `stop` will occur. See
[Byebug.start with a block]().
When `byebug`is run, `.byebugrc` is read.
You may want to enter byebug at several points in the program where there is a
problem you want to investigate. And since `byebug` is just a method call it's
possible to enclose it in a conditional expression, for example
```ruby
byebug if "bar" == foo and 20 == iter_count
```
### Restarting Byebug
You can restart the program using `restart [program args]`. This is a re-exec -
all byebug state is lost. If command arguments are passed, those are used.
Otherwise program arguments from the last invocation are used.
You won't be able to restart your program in all cases. First, the program
should have been invoked at the outset rather than having been called from
inside your program or invoked as a result of post-mortem handling.
Also, since this relies on the OS `exec` call, this command is available only if
your OS supports `exec`.
## Debugging remote programs
It is possible to set up debugging so that you can issue byebug commands from
outside the process running the Ruby code. In fact, you might even be on a
different computer than the one running the Ruby program.
To setup remote debugging, drop the following somewhere before the point in the
program that you want to debug (In Rails, the
`config/environments/development.rb` could be a good candidate).
```ruby
require "byebug/core"
Byebug.wait_connection = true
Byebug.start_server("localhost", <port>)
```
Once this piece gets executed, you can connect to the remote debugger from your
local machine, by running: `byebug -R localhost:<port>`.
Next, at a place of program execution which gets run just before the code you
want to debug, add a call to `byebug` as was done without remote execution:
```ruby
# work, work, work...
byebug
some ruby code # byebug will stop before this line is run
```
## Byebug Command Reference
### Command Syntax
Usually a command is put on a single line. There is no limit on how long it can
be. It starts with a command name, which is followed by arguments whose meaning
depends on the command name. For example, the command `step` accepts an
argument which is the number of times to step, as in `step 5`. You can also use
the `step` command with no arguments. Some commands do not allow any arguments.
Multiple commands can be put on a line by separating each with a semicolon `;`.
You can disable the meaning of a semicolon to separate commands by escaping it
with a backslash.
For example, you might want to enter the following code to compute the 5th
Fibonacci number.
```console
(byebug) fib1=0; fib2=1; 5.times {|temp| temp=fib1; fib1=fib2; fib2 += temp }
0
1
SyntaxError Exception: /home/davidr/Proyectos/sample_app/trace.rb:1: syntax
error, unexpected end-of-input, expecting '}'
5.times { |temp| temp=fib1
^
nil
1
SyntaxError Exception: /home/davidr/Proyectos/sample_app/trace.rb:1: syntax
error, unexpected tSTRING_DEND, expecting end-of-input
fib2 += temp }
^
nil
(byebug) fib1=0\; fib2=1\; 5.times {|temp| temp=fib1\; fib1=fib2\; fib2 += temp }
5
(byebug) fib2
8
```
You might also consider using the [irb]() or [pry]() commands and then you
won't have to escape semicolons.
A blank line as input (typing just `<RET>`) means to repeat the previous
command.
Byebug uses readline, which handles line editing and retrieval of previous
commands. Up arrow, for example, moves to the previous byebug command; down
arrow moves to the next more recent command (provided you are not already at
the last command). Command history is saved in file `.byebug_history`. A limit
is put on the history size. You can see this with the `show history size`
command. See [history]() for history parameters.
### Command Output
In the command-line interface, when `byebug` is waiting for input it presents a
prompt of the form `(byebug)`. If the program has terminated normally the prompt
will be `(byebug:ctrl)` and in post-mortem debugging it will be
`(byebug:post-mortem)`.
Whenever `byebug` gives an error message such as for an invalid command or an
invalid location position, it will generally preface the message with `***`.
### Command Help
Once inside `byebug` you can always ask it for information on its commands using
the `help` command. You can use `help` (abbreviated `h`) with no arguments to
display a short list of named classes of commands
```console
(byebug) help
break -- Sets breakpoints in the source code
catch -- Handles exception catchpoints
condition -- Sets conditions on breakpoints
continue -- Runs until program ends, hits a breakpoint or reaches a line
delete -- Deletes breakpoints
disable -- Disables breakpoints or displays
display -- Evaluates expressions every time the debugger stops
down -- Moves to a lower frame in the stack trace
edit -- Edits source files
enable -- Enables breakpoints or displays
finish -- Runs the program until frame returns
frame -- Moves to a frame in the call stack
help -- Helps you using byebug
history -- Shows byebug's history of commands
info -- Shows several informations about the program being debugged
interrupt -- Interrupts the program
irb -- Starts an IRB session
kill -- Sends a signal to the current process
list -- Lists lines of source code
method -- Shows methods of an object, class or module
next -- Runs one or more lines of code
pry -- Starts a Pry session
quit -- Exits byebug
restart -- Restarts the debugged program
save -- Saves current byebug session to a file
set -- Modifies byebug settings
show -- Shows byebug settings
skip -- Runs until the next breakpoint as long as it is different from the current one
source -- Restores a previously saved byebug session
step -- Steps into blocks or methods one or more times
thread -- Commands to manipulate threads
tracevar -- Enables tracing of a global variable
undisplay -- Stops displaying all or some expressions when program stops
untracevar -- Stops tracing a global variable
up -- Moves to a higher frame in the stack trace
var -- Shows variables and its values
where -- Displays the backtrace
```
With a command name, `help` displays information on how to use the command.
```console
(byebug) help list
l[ist][[-=]][ nn-mm]
Lists lines of source code
Lists lines forward from current line or from the place where code was
last listed. If "list-" is specified, lists backwards instead. If
"list=" is specified, lists from current line regardless of where code
was last listed. A line range can also be specified to list specific
sections of code.
(byebug)
```
A number of commands, namely `info`, `set`, `show`, `enable` and `disable`, have
many sub-parameters or _subcommands_. When you ask for help for one of these
commands, you will get help for all of the subcommands that command offers.
Sometimes you may want help only on a subcommand and to do this just follow the
command with its subcommand name. For example, `help info breakpoints`will just
give help about the `info breakpoints` command. Furthermore it will give longer
help than the summary information that appears when you ask for help. You don't
need to list the full subcommand name, just enough of the letters to make that
subcommand distinct from others will do. For example, `help info b` is the same
as `help info breakpoints`.
Some examples follow.
```console
(byebug) help info
info[ subcommand]
Generic command for showing things about the program being debugged.
--
List of "info" subcommands:
--
info args -- Argument variables of current stack frame
info breakpoints -- Status of user-settable breakpoints
info catch -- Exceptions that can be caught in the current stack frame
info display -- Expressions to display when program stops
info file -- Info about a particular file read in
info files -- File names and timestamps of files read in
info line -- Line number and filename of current position in source file
info program -- Execution status of the program
```
```console
(byebug) help info breakpoints
Status of user-settable breakpoints.
Without argument, list info about all breakpoints.
With an integer argument, list info on that breakpoint.
```
```console
(byebug) help info b
Status of user-settable breakpoints.
Without argument, list info about all breakpoints.
With an integer argument, list info on that breakpoint.
```
### Control Commands: quit, restart, source
#### Quit
To exit `byebug`, type `quit` (abbreviated to `q`). Normally, if you are in an
interactive session, this command will prompt you to confirm you really want to
quit. If you want to quit without being prompted, enter `quit unconditionally`
(abbreviated to `q!`).
#### Restart
To restart the program, use the `restart|r` command. This is a re-exec - all
`byebug` state is lost. If command arguments are passed, those are used.
Otherwise program arguments from the last invocation are used.
You won't be able to restart your program in all cases. First, the program
should have been invoked at the outset rather than having been called from
inside your program or invoked as a result of post-mortem handling.
#### Source
You can run `byebug` commands inside a file, using the command `source <file>`.
The lines in a command file are executed sequentially. They are not printed as
they are executed. If there is an error, execution proceeds to the next command
in the file. For information about command files that get run automatically on
startup see [Command Files]().
### Display Commands: display, undisplay
#### Display
If you find that you want to print the value of an expression frequently (to see
how it changes), you might want to add it to the *automatic display list** so
that `byebug` evaluates it each time your program stops or after a line is
printed if line tracing is enabled. Each expression added to the list is given a
number to identify it; to remove an expression from the list, you specify that
number. The automatic display looks like this:
```console
(byebug) display n
1: n = 3
```
This display shows item numbers, expressions and their current values. If the
expression is undefined or illegal the expression will be printed but no value
will appear.
```console
(byebug) display undefined_variable
2: undefined_variable =
(byebug) display 1/0
3: 1/0 =
```
If you use `display` with no argument, `byebug` will display the current values
of the expressions in the list, just as it is done when your program stops.
Using `info display` has the same effect.
#### Undisplay
To remove an item from the list, use `undisplay` followed by the number
identifying the expression you want to remove. `undisplay` does not repeat if
you press `<RET>`after using it (otherwise you would just get the error _No
display number n_)
You can also temporarily disable or enable display expressions, so that the will
not be printed but they won't be forgotten either, so you can toggle them again
later. To do that, use `disable display` or `enable display` followed by the
expression number.
### Evaluation of expressions: irb, pry
To examine and change data in your script you can just evaluate any Ruby code
from `byebug`'s prompt. Any input that is not recognized as a command will be
evaluated, so `byebug` essentially works as a REPL. If you want to evaluate
something that conflicts with a `byebug` command, just use Ruby's `eval`. For
example, if you want to print a variable called `n`, type `eval n` because
typing just `n` will execute `byebug`'s command `next`.
Finally, if you need more advanced functionality from REPL's, you can enter
`irb` or `pry` using `irb` or `pry` commands. The binding's environment will be
set to the current state in the program. When you leave the repl and go back to
`byebug`'s command prompt we show the file, line and text position of the
program. If you issue a `list` without location information, the default
location used is the current line rather than the current position that may have
got updated via a prior `list` command.
```console
$ byebug triangle.rb
[1, 10] in /path/to/triangle.rb
1: # Compute the n'th triangle number, the hard way: triangle(n) == (n*(n+1))/2
=> 2: def triangle(n)
3: tri = 0
4: 0.upto(n) do |i|
5: tri += i
6: end
7: tri
8: end
9:
10: if __FILE__ == $0
(byebug) irb
irb(main):001:0> (0..6).inject { |sum, i| sum += i }
=> 21
irb(main):002:0> exit
(byebug)
```
### Printing variables: var
Byebug can print many different information about variables. Such as
* `var const <object>`. Show the constants of `<object>`. This is basically
listing variables and their values in `<object>.constant`.
* `var instance <object>`. Show the instance variables of `<object>`. This is
basically listing `<object>.instance_variables`.
* `var instance`. Show instance_variables of `self`.
* `var local`. Show local variables.
* `var global`. Show global variables.
* `var all`. Show local, global and instance and class variables of `self`.
* `method instance <object>`. Show methods of `<object>`. Basically this is the
same as running `<object>.instance_methods(false)`.
* `method <class-or-module>`. Show methods of the class or module
`<class-or-module>`. Basically this is the same as running
`<class-or-module>.methods`.
### Examining Program Source Files: list
`byebug` can print parts of your script's source. When your script stops,
`byebug` spontaneously lists the source code around the line where it stopped
that line. It does that when you change the current stack frame as well.
Implicitly there is a default line location. Each time a list command is run
that implicit location is updated, so that running several list commands in
succession shows a contiguous block of program text.
If you don't need code context displayed every time, you can issue the `set
noautolist` command. Now whenever you want code listed, you can explicitly issue
the `list` command or its abbreviation `l`. Notice that when a second listing is
displayed, we continue listing from the place we last left off. When the
beginning or end of the file is reached, the line range to be shown is adjusted
so "it doesn't overflow". You can set the `noautolist` option by default by
dropping `set noautolist` in byebug's startup file `.byebugrc`.
If you want to set how many lines to be printed by default rather than use the
initial number of lines, 10, use the `set listsize` command ([listsize()). To
see the entire program in one shot, give an explicit starting and ending line
number. You can print other portions of source files by giving explicit position
as a parameter to the list command.
There are several ways to specify what part of the file you want to print. `list
nnn` prints lines centered around line number `nnn` in the current source file.
`l` prints more lines, following the last lines printed. `list -` prints lines
just before the lines last printed. `list nnn-mmm` prints lines between `nnn`
and `mmm` inclusive. `list =` prints lines centered around where the script is
stopped. Repeating a `list` command with `RET` discards the argument, so it is
equivalent to typing just `list`. This is more useful than listing the same
lines again. An exception is made for an argument of `-`: that argument is
preserved in repetition so that each repetition moves up in the source file.
### Editing Source files: edit
To edit a source file, use the `edit` command. The editor of your choice is invoked
with the current line set to the active line in the program. Alternatively, you can
give a line specification to specify what part of the file you want to edit.
You can customize `byebug` to use any editor you want by using the `EDITOR`
environment variable. The only restriction is that your editor (say `ex`) recognizes
the following command-line syntax:
```bash
ex +nnn file
```
The optional numeric value `+nnn` specifies the line number in the file where
you want to start editing. For example, to configure `byebug` to use the `vi` editor,
you could use these commands with the `sh` shell:
```bash
EDITOR=/usr/bin/vi
export EDITOR
byebug ...
```
or in the `csh` shell,
```bash
setenv EDITOR /usr/bin/vi
byebug ...
```
### The stack trace
When your script has stopped, one thing you'll probably want to know is where
it stopped and some idea of how it got there.
Each time your script calls a method or enters a block, information about this
action is saved. This information is what we call a _stack frame_ or just a
_frame_. The set of all frames at a certain point in the program's execution is
called the _stack trace_ or just the _stack_. Each frame contains a line number
and the source-file name that the line refers to. If the frame is the beginning
of a method it also contains the method name.
When your script is started, the stack has only one frame, that of the `main`
method. This is called the _initial frame_ or the _outermost frame_. Each time
a method is called, a new frame is added to the stack trace. Each time a method
returns, the frame for that method invocation is removed. If a method is
recursive, there can be many frames for the same method. The frame for the
method in which execution is actually occurring is called the _innermost
frame_. This is the most recently created of all the stack frames that still
exist.
Every time the debugger stops, one entry in the stack is selected as the
current frame. Many byebug commands refer implicitly to the selected block. In
particular, whenever you ask Byebug to list lines without giving a line number
or location the value is found in the selected frame. There are special
commands to select whichever frame you're interested in, such as `up`, `down`
and `frame`.
After switching frames, when you issue a `list` command without any position
information, the position used is the location in the frame that you just
switched between, rather than a location that got updated via a prior `list`
command.
Byebug assigns numbers to all existing stack frames, starting with zero for the
_innermost frame_, one for the frame that called it, and so on upward. These
numbers do not really exist in your script, they are assigned by Byebug to give
you a way of designating stack frames in commands.
### Printing the Stack: `where` command
The command `where`, aliased to `bt` or `backtrace` prints the call stack., It
shows one line per frame, for many frames, starting with the place that you are
stopped at (frame zero), followed by its caller (frame one), and on up the
stack. Each frame is numbered and can be referred to in the `frame` command.
The position of the current frame is marked with `-->`.
The are some special frames generated for methods that are implemented in C.
One such method is `each`. They are marked differently in the call stack to
indicate that we cannot switch to those frames. This is because they have no
source code in Ruby, so we can not debug them using Byebug.
```console
(byebug) where
--> #0 Object.gcd(a#Fixnum, b#Fixnum) at line gcd.rb:6
#1 at line gcd.rb:19
```
### Selecting a frame: `up`, `down` and `frame` commands
* `up <n>`: Move `n` frames up the stack, towards the outermost frame (higher
frame numbers, frames that have existed longer). `n` defaults to one.
* `down <n>`: Move `n` frames down the stack, towards the _innermost frame_
(lower frame numbers, frames that were created more recently). `n` defaults to
one.
* `frame <n>`: Allows you to move to an arbitrary frame. `n` is the stack frame
number or 0 if no frame number is given. `frame 0` will show the current and
most recent stack frame. If a negative number is given, counting is from the
other end of the stack frame, so `frame -1` shows the least-recent, outermost
stack frame. Without an argument, `frame` prints the current stack frame.
|