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
|
<!DOCTYPE html>
<meta charset="utf-8">
<title>rr</title>
<section>
<h1>How <a href="http://rr-project.org">rr</a> works</h1>
<footer><a href="www.mozilla.org/research/">mozilla
research</a></footer>
</section>
<section>
<p>rr records nondeterministic executions and debugs them
deterministically.
<p>Practical tool; version 1.2 is latest release. Used to debug
Firefox.
</section>
<!-----------------------------------------------------------------------------
-- Why?
-->
<section>
<h2>Why?</h2>
</section>
<section>
<p>Deterministic debugging: record nondeterministic failure once,
debug deterministically forever.
</section>
<section>
<p>Record intermittent test failures "at scale" online, debug the
recordings offline at leisure.
</section>
<section>
<p>Omniscient debugging: issue queries over program state changes;
go backwards in time.
</section>
<!-----------------------------------------------------------------------------
-- Overview
-->
<section>
<h2>Overview</h2>
</section>
<section>
<p><code>rr record prog --args</code><br />
→<em>saves recording</em>
<p><code>rr replay</code><br /> →<em>debugger socket drives
replay of most recent recording</em>
</section>
<section>
<p>Most of an application's execution is deterministic.
<p>rr records the <em>nondeterministic</em> parts.
</section>
<section>
<h3>Examples of nondeterministic inputs</h3>
<ul>
<li><code>clock_gettime(...<span class="highlight">&now</span>);</code>
<li><code>read(fd, <span class="highlight">buf</span>,
4096);</code>
<li><code>__asm__("<span class="highlight">rdtsc</span>")</code>
<li><code>ioctl(<span class="highlight">...</span>)</code>
<li>UNIX signals...
</ul>
</section>
<section>
<p>Then during replay, emulate system calls and <code>rdtsc</code>
by writing the saved nondeterministic data back to the tracee.
</section>
<section>
<p>Shared-memory multitasking is a nondeterministic "input".
<p>... but modern hardware can't record it efficiently. So rr
doesn't record truly parallel executions.
</section>
<section>
<h3>Scheduling tasks</h3>
<p>Can switch tasks at syscalls. Must preempt straight-line code
too; <em>and replay the preemptions deterministically</em>.
</section>
<section>
<h3>Hardware performance counters (HPCs)</h3>
<p>Recent chips count instructions-retired, branches-retired,
..., and can be programmed to interrupt after a count
of <i>x</i>.
</section>
<section>
<p>Simulate task preemption with HPC interrupts.
<p>Idea: program insns-retired counter to interrupt
after <i>k</i> <sup>✱</sup>. That <i>k</i> approximates
a <em>time slice</em>.
</section>
<section>
<h3>Replaying preemption</h3>
<p>Record the insn-retired counter value <i>v</i> to the trace file.
During replay, program the interrupt for <i>v</i>. Voilà.
</section>
<section>
<p>UNIX signals are recorded and replayed like task preemptions.
<p>Record counter value <i>v</i> and signum. Replay by interrupting
after <i>v</i> and "delivering" signum.
</section>
<!-----------------------------------------------------------------------------
-- System requirements
-->
<section>
<h2>System requirements</h2>
</section>
<section>
<h3>Basic requirements</h3>
<ul>
<li>Intel chip with Nehalem (2010) or later µarch<sup>1</sup>. VM
with perf counter virtualization is OK.
<li>x86 userspace. x86-64 kernel is OK.
<li>linux with <code>PTRACE_INTERRUPT</code> support: ≥
3.4
<li>(<i>strongly encouraged</i>) linux with seccomp-bpf support:
≥ 3.5
</ul>
<footer><sup>1</sup> Some Haswell chips don't work with rr
(yet).</footer>
</section>
<section>
<p>rr touches low-level details of machine architecture, by
necessity; f.e. kernel syscall ABI.
<p>Supporting more ISAs is "just work". x86-64 coming.
</section>
<section>
<p>ARM chips don't have the performance counters that rr requires.
<p>So no ARM support is possible at the moment.
</section>
<section>
<p>Precise HPC events identify points in execution.
<p>Precise replay of signals and preemption requires interrupting
tracees at these events.
</section>
<section>
<h3><sup>✱</sup>Performance counters are messier in reality</h3>
<ul>
<li>Insns-retired counter is imprecise. <em>Use precise
retired-branch counter instead</em>.
<li>Counter interrupts can overshoot. <em>Subtract a "skid
region"</em>.
<li>(<em>So replay point is technically indeterminate. But
doesn't seem to be a problem in practice, yet</em>.)
</ul>
</section>
<section>
<p>seccomp-bpf enables rr to <em>selectively trace</em>
syscalls.
<p>Only trap to rr for syscalls that can't be handled in the tracee.
Over 100x faster in µbenchmarks.
</section>
<section>
<h3>Buffer syscalls; flush buffer as "super event"</h3>
<p><img style="width: 100%;" src="res/syscall-buffer.svg">
</section>
<!-----------------------------------------------------------------------------
-- Recorder implementation
-->
<section>
<h2>Recorder implementation</h2>
</section>
<section>
<p>Tasks are controlled through the ptrace API.
<p>HPCs are controlled through the perf event API.
</section>
<section>
<p>The first traced task is forked from rr. After
that, <code>clone()</code> and <code>fork()</code>from tracees
add new tasks.
<p>And tasks die at <code>exit()</code>.
</section>
<section>
<h3>Simplified recorder loop</h3>
<!-- TODO use incremental list? ol seems to be broken. -->
<pre>
while live_task():
task t = <span class="highlight">schedule()</span>
if not <span class="highlight">status_changed(t)</span>:
<span class="highlight">resume_execution(t)</span>
<span class="highlight">state_change(t)</span>
</pre>
<footer>src/recorder.cc</footer>
</section>
<section>
<h3>Scheduling a task</h3>
<pre>
task <span class="highlight">schedule()</span>:
for each task t, round-robin:
if is_runnable(t)
or <span class="highlight">status_changed(t)</span>:
return t
tid = waitpid(ANY_CHILD_TASK)
return task_map[tid]
</pre>
<footer>src/recorder_sched.cc</footer>
</section>
<section>
<h3>Tasks changing status</h3>
<pre>
bool <span class="highlight">status_changed(task t)</span>:
# Non-blocking
return waitpid(t.tid, WNOHANG)
<em># Deceptively simple: includes
# syscalls, signals, ptrace
# events ...</em>
</pre>
<footer>src/task.cc</footer>
</section>
<section>
<h3>Resuming task execution</h3>
<p><em>Invariant</em>: At most one task is running userspace code.
All other tasks are either idle or awaiting completion of a
syscall.<sup>†</sup>
</section>
<section>
<p>Multiple running tasks suffer from <em>shared-memory
hazards</em>.
<p>rr doesn't attempt to record these hazards, so can't replay them
deterministically.
</section>
<section>
<h3>Resuming a task, simplified</h3>
<pre>
void <span class="highlight">resume_execution(task t)</span>:
ptrace(PTRACE_SYSCALL, t.tid)
waitpid(t.tid) # Blocking
<em># Again, deceptively simple: traps
# for syscalls, signals, ptrace
# events ...</em>
</pre>
<footer>src/{recorder, task}.cc</footer>
</section>
<section>
<p>Most recorder work is done
for <code><span class="highlight">state_change(task
t)</span></code>.
<p>But before looking at it, a few digressions ...
</section>
<section>
<h3>Generating time-slice interrupts</h3>
<ul>
<li><code>perf_event_open()</code> fd for
retired-conditional-branches; details are µarch
specific
<li>Set event "sample period" to <i>k</i>
<li>Make event fd O_ASYNC and set tracee task as owner
<li>→ <em>tracee sent SIGSTKFLT at rbc ≈ <i>k</i></em>
</ul>
<footer>src/hpc.cc</footer>
</section>
<section>
<h3>Trapping tracees at rdtsc</h3>
<ul>
<li><code>prctl(PR_SET_TSC, PR_TSC_SIGSEGV)</code> → tracees
executing rdtsc trap to SIGSEGV
<li>rr examines which instruction triggered SIGSEGV
<li>if rdtsc, value is recorded by rr tracer and tracee insn is
emulated
</ul>
</section>
<section>
<p>Tracees generate ptrace events by executing fork, clone, exit,
and some other syscalls.
<p>ptrace events exist for linux reasons that aren't
interesting.
</section>
<section>
<p>(rr tracees can share memory mappings with other processes.
<p>Not possible to record efficiently in SW; needs kernel and/or HW
support. Unsupported until then.)
</section>
<section>
<h3>Tracee events recorded
by <code><span class="highlight">state_change()</span></code></h3>
<ul>
<li>"Pseudo"-signals delivered by implementation of rdtsc or
time-slice interrupts
<li>Other, "real", signals
<li>ptrace events
<li>Syscall entry and exit
</ul>
</section>
<section>
<p>Some syscalls must be executed atomically; can't switch task
until syscall finishes.
<p>Ex: <code>mmap</code> modifies address space, can race other
syscalls.
</section>
<section>
<p>On the other hand, some syscalls <em>require</em> switching;
syscall can't finish until the task switches.
<p>Ex: <code>waitpid()</code> only returns after child runs, changes
state.
</section>
<section>
<p><em>Problem</em>: kernel writes non-atomic syscall outparams in
an order that rr can't record.
<p>Kernel outparam writes race rr tracees in userspace, syscalls.
</section>
<section>
<p>Solution: allocate <em>scratch space</em> for the outparams of
non-atomic syscalls. At syscall exit, write scratch data back to
outparams.
<p><em>→ rr orders outparam writes</em>
</section>
<section>
<p>POSIX signals can arrive at practically any point in execution
and invoke signal handler code.
<p><em>→ tracee code can be (almost) arbitrarily
re-entered</em>
</section>
<section>
<p>Linux exits tracees out of syscalls with
an <code>ERESTART*</code> error code before delivering signals.
Syscall not always restarted after signal.
<p>Sighandler
nesting <a href="https://github.com/mozilla/rr/wiki/Linux-signals">gets
complex</a>.
</section>
<section>
<h3>When a signal becomes pending</h3>
<ul>
<li>consult sighandler table to see if there's a registered
sighandler function
<li>if so, <code>SINGLESTEP</code> into the sighandler frame and
record the <code>struct sigframe</code> set up by kernel. Also
record sighandler registers.
<li>otherwise, deliver the signal using the ptrace API
</section>
<section>
<p>Sighandlers exit using the <code>SYS_sigreturn</code> syscall.
rr uses these calls to help determine whether interrupted syscalls
are restarted.
</section>
<section>
<p>Tracees exit in unpredictable order at fatal signals like
SIGABRT. Naïve <code>waitpid()</code> calls deadlock.
<p><code>exit_group</code>: same problem.
</section>
<section>
<h3>"Unstable" tracee exit</h3>
<p>rr solves this by detaching and not waiting on affected tracees.
<p><sup>†</sup>Breaks rr scheduling invariant.
</section>
<!-----------------------------------------------------------------------------
-- Syscall buffer
-->
<section>
<h2>Syscall buffer</h2>
</section>
<section>
<p>ptrace traps are expensive. Better to do as much work in tracee
process as possible.
<p>Use seccomp-bpf to selectively trap syscalls.
</section>
<section>
<p>Syscall hooks are <code>LD_PRELOAD</code>'d into tracees.
<p>Hook functions record kernel return value and outparam data to
the <em>syscall buffer</em>.
</section>
<section>
<p>rr monkeypatches <code>__kernel_vsyscall()</code> in vdso to jump
to rr trampoline.
<p>Trampoline calls dispatcher, which calls rr hook if
available.
</section>
<section>
<p><em>Untraced</em> syscalls are recorded to syscallbuf by tracee.
<em>Traced</em> events recorded by the rr process "flush" the
tracee's syscallbuf.
<p>Lib falls back on traced syscalls.
</section>
<section>
<h3>Simplified example of syscallbuf hook function</h3>
<pre style="margin: 50px 50px; font-size: 24px">
static int sys_close(int fd)
{
long ret;
if (!start_buffer_syscall(SYS_close))
/* Fall back on traced syscall.
* This generates a ptrace trap. */
return syscall(SYS_close, fd);
/* Untraced syscall. Does not generate
* ptrace trap.*/
ret = untraced_syscall1(SYS_close, fd);
/* Save the result to syscall buffer. */
return commit_syscall(SYS_close, ret);
}
</pre>
</section>
<section>
<h3>How untraced syscalls are made</h3>
<ul>
<li>Create single "untraced" kernel entry point
<pre>
asm("_untraced_syscall:\n\t"
"int $0x80");
</pre>
<li>Install seccomp-bpf filter that passes calls
from <code>_untraced_syscall</code>, traps to rr otherwise.
</ul>
</section>
<section>
<p>seccomp-bpf traps generate <code>PTRACE_EVENT_SECCOMP</code> in
tracer process.
<p>rr can then <code>PTRACE_SYSCALL</code> the tracee into traced
syscall.
</section>
<section>
<p><em>Problem</em>: buffered syscalls don't trap to rr, by design.
But may-block syscalls (f.e. <code>waitpid()</code>) require rr to
schedule another task.
</section>
<section>
<h3>perf events to the rescue: "descheduled" event</h3>
<p>Set event to fire on tracee context switch. Event traps to rr.
<p><em>Buffered syscall blocks → context switch → rr
trap</em>
</section>
<section>
<h3>Generating desched events</h3>
<ul>
<li>In <em>tracee</em>, <code>perf_event_open()</code> fd for
context-switch counter
<li>Set event "sample period" to <code>1</code> (i.e. next context
switch) just before buffered syscall
<li>Disarm event just after buffered syscall.
<li>→ <em>tracee sent SIGSYS if context switched during
buffered syscall</em>
</ul>
<footer>src/preload/preload.c</footer>
</section>
<!-----------------------------------------------------------------------------
-- Saved traces
-->
<section>
<h2>Saved traces</h2>
</section>
<section>
<p>Traces are saved to <code>~/.rr</code> by default.
<p>Stored on disk uncompressed. Trace compression is planned.
</section>
<section>
<h3>Trace directory contents</h3>
<ul>
<li><code>args_env</code>: command-line args and environment
pairs used to <code>execvpe()</code> initial tracee
<li><code>events</code>: sequence of syscalls, signals, and
various other execution events
<li><code>mmaps</code>: metadata about mmap'd files
<li><code>data</code>/<code>data_header</code>: all recorded data,
along with metadata about when it was recorded
<li><code>version</code>: trace format version number
</ul>
</section>
<!-----------------------------------------------------------------------------
-- Replayer implementation
-->
<section>
<h2>Replayer implementation</h2>
</section>
<section>
<p>Emulate most syscalls using trace data.
<p>Actually execute a small number.
</section>
<section>
<h3>Built around PTRACE_SYSEMU</h3>
<p><code>SYSCALL</code> runs tracee to syscall, executes it.
<p><code>SYSEMU</code> runs to syscall, <em>doesn't</em> execute it.
rr replays side effects.
</section>
<section>
<h3>Replaying time-slice interrupts, in theory</h3>
<p>Program instructions counter to interrupt after the
recorded <i>t</i> number of instructions.
<p>Tracee stops at <i>t</i>.
</section>
<section>
<h3>Replaying time-slice interrupts, in practice</h3>
<ul>
<li>have to use retired-conditional-branch counter (RBC)
<li>RBC interrupts aren't precise. Can overshoot by up to 70
branches.
<li>RBC counter value doesn't uniquely identify a point in
execution (unlike retired-insn counter value)
</ul>
</section>
<section>
<h3>Finding execution target, in practice</h3>
<ul>
<li>program RBC interrupt for <i>target-rbc - SKID_SIZE</i>
<li>after RBC interrupt, set breakpoint on target <code>$ip</code>
to avoid single-stepping when possible
<li>when breakpoint hit, compare RBC value and register files to
guess if at execution target. If RBC and regs match what was
recorded, done.
</ul>
<p>To reiterate, this is not sound.
</section>
<section>
<p>Deterministic signals were raised by program execution. For
example, <code>*NULL = 42;</code>
<p>Replayed "naturally" in the course of execution.
</section>
<section>
<p>Async signals were raised externally at some execution point
during recording.
<p>Replay to that execution point <em>just as for time-slice
interrupts</em>.
</section>
<section>
<h3>Replay signal delivery by emulating</h3>
<p>If there was a sighandler, restore recorded <code>sigframe</code>
and registers at sighandler entry.
<p>Otherwise, nothing else to do.
</section>
<section>
<h3>Replaying buffered syscalls</h3>
<p>Read saved buffer from trace.
<p>Replay each syscall as normal, but restore outparam data from
records in the read buffer.
</section>
<!-----------------------------------------------------------------------------
-- Debugger interface
-->
<section>
<h2>Debugger interface</h2>
</section>
<section>
<p>Common commands supported.
<p><code>c, s, si, b, bt, watch, info regs, thr, info thr ...</code>
</section>
<section>
<p><code>(gdb) call foo()</code> can cause replay divergence.
<p>So you're not allowed to do it … for now. Support coming.
</section>
<section>
<p>Small stub translates from and to gdb remote protocol.
<p>Then passes debugger requests up to rr replayer.
<footer>src/debugger_gdb.cc</footer>
</section>
<section>
<p>Replayer fulfills requests using <code>ptrace()</code> or cached
data.
<p>And resumes tracee execution when asked.
<footer>src/replayer.cc</footer>
</section>
<section>
<p>Breakpoints, <code>int $3</code>, <code>stepi</code>, watchpoints
all raise <code>SIGTRAP</code>.
<p><code>$ip</code>, breakpoint table, gdb request,
and <code>$DR6</code> decode trap.
<footer>src/replayer.cc</footer>
</section>
<!-----------------------------------------------------------------------------
-- Future work
-->
<section>
<h2>Future work</h2>
</section>
<section>
<h3><a href="https://github.com/mozilla/rr/wiki/Checkpointing-(deepfork)-during-replay">Checkpointing</a></h3>
<p>Make a "deep fork" of tracee tree during replay.
<p>Run code (or whatever) in copied tree, return to original.
</section>
<section>
<h3><a href="https://github.com/mozilla/rr/wiki/Chroniclerr">Omniscient
debugging (aka chroniclerr)</a></h3>
<p>Use <a href="https://code.google.com/p/chronicle-recorder/">chronicle</a>-style
instrumentation to generate execution DB.
<p>Query state changes in DB.
</section>
<section>
<h3><a href="http://research.microsoft.com/en-us/projects/chess/">CHESS</a>-style
execution search; targeted recording</h3>
<p>At each scheduling decision, make a checkpoint.
<p>If execution reaches bad state, done. Else, resume checkpoint.
</section>
<section>
<h3>Other projects</h3>
<ul>
<li>Copy traces across machines
<li>Integrate hardware shared-memory multithreading recorder
like <a href="http://iacoma.cs.uiuc.edu/iacoma-papers/isca13_1.pdf">QuickRec</a>
<li>Record ptrace API; <code>rr record rr record</code>…
<li>Handle GPU drivers (NVIDIA, ATI, ...)
<li>Port to Darwin kernel
<li>Port to Windows NT kernel
<li><s>ARM port not possible with current generation of chips</s>
</ul>
</section>
<section>
<h3>Thanks from the rr team!</h3>
<ul>
<li><a href="http://rr-project.org/">rr-project.org</a>
<li><a href="https://github.com/mozilla/rr">github.com/mozilla/rr</a>
<li><a href="https://mail.mozilla.org/listinfo/rr-dev">mail.mozilla.org/listinfo/rr-dev</a>
<li>#research on irc.mozilla.org
</ul>
</section>
<section>
<h2>Appendix: rr for RnR people</h2>
</section>
<section>
<p>Release 1.2 available today at
<p><a href="http://rr-project.org/">rr-project.org</a>
</section>
<section>
<h3>Use cases</h3>
<ul>
<li>Run on modern, commodity hardware and software: <code>yum
install rr; rr record</code>…
<li>Aspire to general tool, focus on Firefox initially
<li>Record nondeterministic test failures at scale (e.g., Firefox
build/test infra), debug offline
<li>"Super-debugger" for local development
<li>Search execution space to actively find bugs
</ul>
</section>
<section>
<h3>Design concerns</h3>
<ul>
<li>Commodity HW → only record single HW thread (for
now!)
<li>Commodity SW → stick to higher-level userspace APIs
(e.g., ptrace, PEBS)
<li>Record tests at scale → record perf must be "economical",
but not mission-critical
<li>"Super-debugger" → the usual, plus queries over execution
history; pretty fast replay
<li>Search exe space → flexible scheduling and
checkpointing
</section>
<section>
<h3>rr recorder overview</h3>
<ul>
<li>Record "applications" consisting of linux tasks
<li>Schedule CPU slices by programming precise counter interrupt
(retired branches, RBC) for <i>k</i>
<li>Time slice is special case of signal: time-slice recorded as
<i>(0, rec-RBC)</i>, signals <i>(signum, rec-RBC)</i>
<li>Record kernel-created signal stack
<li>Syscall "outparams" and <code>rdtsc</code> generate trace
traps; results saved to log
<li><i>Plus a "faster" mode we'll cover later</i>
</section>
<section>
<h3>Trade-off: scheduling from userspace</h3>
<ul>
<li>While it's great to fully control scheduling ...
<li>... we have to approximate timeslices; can be unfair
<li>... interactive programs don't "feel" native; rr has its own
heuristics
<li>... can be slower
<li>Future work is to have the option of both
</ul>
</section>
<section>
<h3>Headache: kernel writes racing with userspace</h3>
<ul>
<li>rr doesn't (can't efficiently) record reads/writes of memory
shared outside of tracee application.
<li>But there are still hazards with the kernel:
<li>... kernel-write/task-read hazard on syscall outparam buffers
→ rr replaces user buffers with scratch and serializes
writes
<li>... kernel-write/task-read on random futexes,
e.g. CLONE_CHILD_CLEARTID → no good solution yet;
usleep…
</ul>
</section>
<section>
<h3>rr replayer overview</h3>
<ul>
<li>Replay signal/time-slice <i>(signum, rec-RBC)</i> by
programming interrupt for <i>rec-RBC</i>
<li>Emulate signals by restoring signal stack and regs
<li>Emulate syscalls by restoring outparams where possible
<li>Execute non-emulatable clone/mmap/et al. as required
<li>Serve debugger requests (maybe covered later)
</section>
<section>
<h3>Replayer headache: slack in counter interrupts</h3>
<ul>
<li>Interrupt programmed for <i>RBC = k</i> may actually fire at
up to <i>RBC = k + slack</i>
<li>(Slack empirically seen to be >= 70 branches)
<li>So we program interrupt for <i>RBC = k - slack</i> and then
advance by breakpoint+stepi
<li>"At target" when <i>rec-RBC == rep-RBC</i> and <i>[rec-regs]
== [rep-regs]</i>
<li>Replay target therefore technically indeterminate
</ul>
</section>
<section>
<h3>Recorder "fast mode": syscall buffering</h3>
<ul>
<li>ptrace traps are slow
<li><em>Idea</em>: avoid them when possible by buffering log data
in tracee task
<li>Implementation: LD_PRELOAD a helper library with hooks for
common syscalls (read, write, gettimeofday, etc.)
<li>Hook makes fast untraced syscall, saves outparams in
task-local buffer
<li>Flush buffer at traced event (including buffer overflow)
</section>
<section>
<h3>Headache: many syscalls made internally in glibc</h3>
<ul>
<li>Those syscalls can't be wrapped by usual approach of
interposing exported symbol using LD_PRELOAD
<li>Solution: monkeypatch <code>__kernel_vsyscall()</code> in
vdso.
<li>Syscalls directly made through <code>int $0x80</code> still
can't be buffered.
<li>We hope this terrible hack evolves into kernel support.
</section>
<section>
<h3>Headache: buffering syscalls that may block</h3>
<ul>
<li>read/write/… may block if buffer empty/full/…
<li>But, <em>untraced</em> syscall from wrapper means no trap to
rr for scheduling arbitration
<li>If another tracee is blocked too, then may deadlock
<li>Solution: libc wrapper programs perf_event interrupt triggered
by next context-switch of task
<li>If the syscall blocks, task is switched out, and rr tracer
gets interrupt (SIGIO from perf_event)
</ul>
</section>
<section>
<h3>Fun debugging tricks</h3>
<ul>
<li>Save register / RBC info at all events, verify in replay
<li>Generate memory checksums at selected events, verify in
replay
<li>LLVM pass to add "execution path logging" (Bell-Larus): poor
man's log of retired branches. Save to "magic fd" in recording,
verify in replay.
<li>Hack replayer itself to efficiently log arbitrary info at
arbitrary points
</ul>
</section>
<style>
.highlight {
color: red;
background-color: yellow;
font-weight: bold;
}
.todo {
color: aqua;
background-color: purple;
font-weight: bold;
}
</style>
<!-- Maybe a font from http://www.google.com/webfonts ? -->
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet'>
<style>
html, .view body { background-color: black; counter-reset: slideidx; }
body, .view section { background-color: white; border-radius: 12px }
/* A section is a slide. It's size is 800x600, and this will never change */
section, .view head > title {
/* The font from Google */
font-family: 'Oswald', arial, serif;
font-size: 30px;
}
.view section:after {
counter-increment: slideidx;
content: counter(slideidx, decimal-leading-zero);
position: absolute; bottom: -80px; right: 100px;
color: white;
}
.view head > title {
color: white;
text-align: center;
margin: 1em 0 1em 0;
}
h1, h2 {
margin-top: 200px;
text-align: center;
font-size: 80px;
}
h3 {
margin: 50px 0 25px 50px;
}
ul {
margin: 25px 100px;
}
li > ul {
margin: 15px 50px;
}
p {
margin: 75px;
font-size: 50px;
}
blockquote {
height: 100%;
background-color: black;
color: white;
font-size: 60px;
padding: 50px;
}
blockquote:before {
content: open-quote;
}
blockquote:after {
content: close-quote;
}
/* Figures are displayed full-page, with the caption
on top of the image/video */
figure {
background-color: black;
width: 100%;
height: 100%;
}
figure > * {
position: absolute;
}
figure > img, figure > video {
width: 100%; height: 100%;
}
figcaption {
margin: 70px;
font-size: 50px;
}
footer {
position: absolute;
bottom: 0;
width: 100%;
padding: 40px;
text-align: right;
background-color: #F3F4F8;
border-top: 1px solid #CCC;
}
/* Transition effect */
/* Feel free to change the transition effect for original
animations. See here:
https://developer.mozilla.org/en/CSS/CSS_transitions
How to use CSS3 Transitions: */
section {
-moz-transition: left 400ms linear 0s;
-webkit-transition: left 400ms linear 0s;
-ms-transition: left 400ms linear 0s;
transition: left 400ms linear 0s;
}
.view section {
-moz-transition: none;
-webkit-transition: none;
-ms-transition: none;
transition: none;
}
.view section[aria-selected] {
border: 5px red solid;
}
/* Before */
section { left: -150%; }
/* Now */
section[aria-selected] { left: 0; }
/* After */
section[aria-selected] ~ section { left: +150%; }
/* Incremental elements */
/* By default, visible */
.incremental > * { opacity: 1; }
/* The current item */
.incremental > *[aria-selected] { opacity: 1; }
/* The items to-be-selected */
.incremental > *[aria-selected] ~ * { opacity: 0; }
/* The progressbar, at the bottom of the slides, show the global
progress of the presentation. */
#progress-bar {
height: 2px;
background: #AAA;
}
</style>
<!-- {{{{ dzslides core
#
#
# __ __ __ . __ ___ __
# | \ / /__` | | | \ |__ /__`
# |__/ /_ .__/ |___ | |__/ |___ .__/ core :€
#
#
# The following block of code is not supposed to be edited.
# But if you want to change the behavior of these slides,
# feel free to hack it!
#
-->
<div id="progress-bar"></div>
<!-- Default Style -->
<style>
* { margin: 0; padding: 0; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
[role="note"] { display: none; }
body {
width: 800px; height: 600px;
margin-left: -400px; margin-top: -300px;
position: absolute; top: 50%; left: 50%;
overflow: hidden;
display: none;
}
.view body {
position: static;
margin: 0; padding: 0;
width: 100%; height: 100%;
display: inline-block;
overflow: visible; overflow-x: hidden;
/* undo Dz.onresize */
transform: none !important;
-moz-transform: none !important;
-webkit-transform: none !important;
-o-transform: none !important;
-ms-transform: none !important;
}
.view head, .view head > title { display: block }
section {
position: absolute;
pointer-events: none;
width: 100%; height: 100%;
}
.view section {
pointer-events: auto;
position: static;
width: 800px; height: 600px;
margin: -150px -200px;
float: left;
transform: scale(.4);
-moz-transform: scale(.4);
-webkit-transform: scale(.4);
-o-transform: scale(.4);
-ms-transform: scale(.4);
}
.view section > * { pointer-events: none; }
section[aria-selected] { pointer-events: auto; }
html { overflow: hidden; }
html.view { overflow: visible; }
body.loaded { display: block; }
.incremental {visibility: hidden; }
.incremental[active] {visibility: visible; }
#progress-bar{
bottom: 0;
position: absolute;
-moz-transition: width 400ms linear 0s;
-webkit-transition: width 400ms linear 0s;
-ms-transition: width 400ms linear 0s;
transition: width 400ms linear 0s;
}
.view #progress-bar {
display: none;
}
</style>
<script>
var Dz = {
remoteWindows: [],
idx: -1,
step: 0,
html: null,
slides: null,
progressBar : null,
params: {
autoplay: "1"
}
};
Dz.init = function() {
document.body.className = "loaded";
this.slides = Array.prototype.slice.call($$("body > section"));
this.progressBar = $("#progress-bar");
this.html = document.body.parentNode;
this.setupParams();
this.onhashchange();
this.setupTouchEvents();
this.onresize();
this.setupView();
}
Dz.setupParams = function() {
var p = window.location.search.substr(1).split('&');
p.forEach(function(e, i, a) {
var keyVal = e.split('=');
Dz.params[keyVal[0]] = decodeURIComponent(keyVal[1]);
});
// Specific params handling
if (!+this.params.autoplay)
$$.forEach($$("video"), function(v){ v.controls = true });
}
Dz.onkeydown = function(aEvent) {
// Don't intercept keyboard shortcuts
if (aEvent.altKey
|| aEvent.ctrlKey
|| aEvent.metaKey
|| aEvent.shiftKey) {
return;
}
if ( aEvent.keyCode == 37 // left arrow
|| aEvent.keyCode == 38 // up arrow
|| aEvent.keyCode == 33 // page up
) {
aEvent.preventDefault();
this.back();
}
if ( aEvent.keyCode == 39 // right arrow
|| aEvent.keyCode == 40 // down arrow
|| aEvent.keyCode == 34 // page down
) {
aEvent.preventDefault();
this.forward();
}
if (aEvent.keyCode == 35) { // end
aEvent.preventDefault();
this.goEnd();
}
if (aEvent.keyCode == 36) { // home
aEvent.preventDefault();
this.goStart();
}
if (aEvent.keyCode == 32) { // space
aEvent.preventDefault();
this.toggleContent();
}
if (aEvent.keyCode == 70) { // f
aEvent.preventDefault();
this.goFullscreen();
}
if (aEvent.keyCode == 79) { // o
aEvent.preventDefault();
this.toggleView();
}
}
/* Touch Events */
Dz.setupTouchEvents = function() {
var orgX, newX;
var tracking = false;
var db = document.body;
db.addEventListener("touchstart", start.bind(this), false);
db.addEventListener("touchmove", move.bind(this), false);
function start(aEvent) {
aEvent.preventDefault();
tracking = true;
orgX = aEvent.changedTouches[0].pageX;
}
function move(aEvent) {
if (!tracking) return;
newX = aEvent.changedTouches[0].pageX;
if (orgX - newX > 100) {
tracking = false;
this.forward();
} else {
if (orgX - newX < -100) {
tracking = false;
this.back();
}
}
}
}
Dz.setupView = function() {
document.body.addEventListener("click", function ( e ) {
if (!Dz.html.classList.contains("view")) return;
if (!e.target || e.target.nodeName != "SECTION") return;
Dz.html.classList.remove("view");
Dz.setCursor(Dz.slides.indexOf(e.target) + 1);
}, false);
}
/* Adapt the size of the slides to the window */
Dz.onresize = function() {
var db = document.body;
var sx = db.clientWidth / window.innerWidth;
var sy = db.clientHeight / window.innerHeight;
var transform = "scale(" + (1/Math.max(sx, sy)) + ")";
db.style.MozTransform = transform;
db.style.WebkitTransform = transform;
db.style.OTransform = transform;
db.style.msTransform = transform;
db.style.transform = transform;
}
Dz.getNotes = function(aIdx) {
var s = $("section:nth-of-type(" + aIdx + ")");
var d = s.$("[role='note']");
return d ? d.innerHTML : "";
}
Dz.onmessage = function(aEvent) {
var argv = aEvent.data.split(" "), argc = argv.length;
argv.forEach(function(e, i, a) { a[i] = decodeURIComponent(e) });
var win = aEvent.source;
if (argv[0] === "REGISTER" && argc === 1) {
this.remoteWindows.push(win);
this.postMsg(win, "REGISTERED", document.title, this.slides.length);
this.postMsg(win, "CURSOR", this.idx + "." + this.step);
return;
}
if (argv[0] === "BACK" && argc === 1)
this.back();
if (argv[0] === "FORWARD" && argc === 1)
this.forward();
if (argv[0] === "START" && argc === 1)
this.goStart();
if (argv[0] === "END" && argc === 1)
this.goEnd();
if (argv[0] === "TOGGLE_CONTENT" && argc === 1)
this.toggleContent();
if (argv[0] === "SET_CURSOR" && argc === 2)
window.location.hash = "#" + argv[1];
if (argv[0] === "GET_CURSOR" && argc === 1)
this.postMsg(win, "CURSOR", this.idx + "." + this.step);
if (argv[0] === "GET_NOTES" && argc === 1)
this.postMsg(win, "NOTES", this.getNotes(this.idx));
}
Dz.toggleContent = function() {
// If a Video is present in this new slide, play it.
// If a Video is present in the previous slide, stop it.
var s = $("section[aria-selected]");
if (s) {
var video = s.$("video");
if (video) {
if (video.ended || video.paused) {
video.play();
} else {
video.pause();
}
}
}
}
Dz.setCursor = function(aIdx, aStep) {
// If the user change the slide number in the URL bar, jump
// to this slide.
aStep = (aStep != 0 && typeof aStep !== "undefined") ? "." + aStep : ".0";
window.location.hash = "#" + aIdx + aStep;
}
Dz.onhashchange = function() {
var cursor = window.location.hash.split("#"),
newidx = 1,
newstep = 0;
if (cursor.length == 2) {
newidx = ~~cursor[1].split(".")[0];
newstep = ~~cursor[1].split(".")[1];
if (newstep > Dz.slides[newidx - 1].$$('.incremental > *').length) {
newstep = 0;
newidx++;
}
}
this.setProgress(newidx, newstep);
if (newidx != this.idx) {
this.setSlide(newidx);
}
if (newstep != this.step) {
this.setIncremental(newstep);
}
for (var i = 0; i < this.remoteWindows.length; i++) {
this.postMsg(this.remoteWindows[i], "CURSOR", this.idx + "." + this.step);
}
}
Dz.back = function() {
if (this.idx == 1 && this.step == 0) {
return;
}
if (this.step == 0) {
this.setCursor(this.idx - 1,
this.slides[this.idx - 2].$$('.incremental > *').length);
} else {
this.setCursor(this.idx, this.step - 1);
}
}
Dz.forward = function() {
if (this.idx >= this.slides.length &&
this.step >= this.slides[this.idx - 1].$$('.incremental > *').length) {
return;
}
if (this.step >= this.slides[this.idx - 1].$$('.incremental > *').length) {
this.setCursor(this.idx + 1, 0);
} else {
this.setCursor(this.idx, this.step + 1);
}
}
Dz.goStart = function() {
this.setCursor(1, 0);
}
Dz.goEnd = function() {
var lastIdx = this.slides.length;
var lastStep = this.slides[lastIdx - 1].$$('.incremental > *').length;
this.setCursor(lastIdx, lastStep);
}
Dz.toggleView = function() {
this.html.classList.toggle("view");
if (this.html.classList.contains("view")) {
$("section[aria-selected]").scrollIntoView(true);
}
}
Dz.setSlide = function(aIdx) {
this.idx = aIdx;
var old = $("section[aria-selected]");
var next = $("section:nth-of-type("+ this.idx +")");
if (old) {
old.removeAttribute("aria-selected");
var video = old.$("video");
if (video) {
video.pause();
}
}
if (next) {
next.setAttribute("aria-selected", "true");
if (this.html.classList.contains("view")) {
next.scrollIntoView();
}
var video = next.$("video");
if (video && !!+this.params.autoplay) {
video.play();
}
} else {
// That should not happen
this.idx = -1;
// console.warn("Slide doesn't exist.");
}
}
Dz.setIncremental = function(aStep) {
this.step = aStep;
var old = this.slides[this.idx - 1].$('.incremental > *[aria-selected]');
if (old) {
old.removeAttribute('aria-selected');
}
var incrementals = $$('.incremental');
if (this.step <= 0) {
$$.forEach(incrementals, function(aNode) {
aNode.removeAttribute('active');
});
return;
}
var next = this.slides[this.idx - 1].$$('.incremental > *')[this.step - 1];
if (next) {
next.setAttribute('aria-selected', true);
next.parentNode.setAttribute('active', true);
var found = false;
$$.forEach(incrementals, function(aNode) {
if (aNode != next.parentNode)
if (found)
aNode.removeAttribute('active');
else
aNode.setAttribute('active', true);
else
found = true;
});
} else {
setCursor(this.idx, 0);
}
return next;
}
Dz.goFullscreen = function() {
var html = $('html'),
requestFullscreen = html.requestFullscreen || html.requestFullScreen || html.mozRequestFullScreen || html.webkitRequestFullScreen;
if (requestFullscreen) {
requestFullscreen.apply(html);
}
}
Dz.setProgress = function(aIdx, aStep) {
var slide = $("section:nth-of-type("+ aIdx +")");
if (!slide)
return;
var steps = slide.$$('.incremental > *').length + 1,
slideSize = 100 / (this.slides.length - 1),
stepSize = slideSize / steps;
this.progressBar.style.width = ((aIdx - 1) * slideSize + aStep * stepSize) + '%';
}
Dz.postMsg = function(aWin, aMsg) { // [arg0, [arg1...]]
aMsg = [aMsg];
for (var i = 2; i < arguments.length; i++)
aMsg.push(encodeURIComponent(arguments[i]));
aWin.postMessage(aMsg.join(" "), "*");
}
function init() {
Dz.init();
window.onkeydown = Dz.onkeydown.bind(Dz);
window.onresize = Dz.onresize.bind(Dz);
window.onhashchange = Dz.onhashchange.bind(Dz);
window.onmessage = Dz.onmessage.bind(Dz);
}
window.onload = init;
</script>
<script> // Helpers
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
// closest thing possible to the ECMAScript 5 internal IsCallable
// function
if (typeof this !== "function")
throw new TypeError(
"Function.prototype.bind - what is trying to be fBound is not callable"
);
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply( this instanceof fNOP ? this : oThis || window,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
var $ = (HTMLElement.prototype.$ = function(aQuery) {
return this.querySelector(aQuery);
}).bind(document);
var $$ = (HTMLElement.prototype.$$ = function(aQuery) {
return this.querySelectorAll(aQuery);
}).bind(document);
$$.forEach = function(nodeList, fun) {
Array.prototype.forEach.call(nodeList, fun);
}
</script>
<!-- vim: set fdm=marker: }}} -->
|