This is an archive of the discontinued LLVM Phabricator instance.

[intel-pt] Disable/Enable tracing to guarantee the trace is correct
ClosedPublic

Authored by wallace on Aug 4 2020, 12:57 PM.

Details

Summary

As mentioned in the comment inside the code, the Intel documentation states that the internal CPU buffer is forcefully flushed out to RAM right away when tracing is disabled. Otherwise, the buffer on RAM might be stale, as the CPU's automatic flushing happens only from time to time from time and the CPU decides when.

This diff disables tracing when the trace buffer is going to be read. This is a quite safe operation, as the reading is done when the inferior is paused at a breakpoint, so we are not losing any packets because there's no code being executed.

After the reading is finished, tracing is enabled back.

I run the following manual tests

(lldb) b main
Breakpoint 1: where = a.out`main + 15 at main.cpp:4:7, address = 0x000000000040050f
(lldb) r
Process 3078226 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
    frame #0: 0x000000000040050f a.out`main at main.cpp:4:7
(lldb) processor-trace start
(lldb) b 5
Breakpoint 2: where = a.out`main + 22 at main.cpp:5:12, address = 0x0000000000400516
(lldb) c
Process 3078226 resuming
Process 3078226 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 2.1
    frame #0: 0x0000000000400516 a.out`main at main.cpp:5:12
(lldb) processor-trace show-instr-log 
thread #1: tid=3078226
    0x40050f <+15>: movl   $0x0, -0x8(%rbp)

>>> Before, some runs of the script up to this point lead to empty traces

(lldb) b 6
Breakpoint 3: where = a.out`main + 42 at main.cpp:6:14, address = 0x000000000040052a
(lldb) c
Process 3092991 resuming
Process 3092991 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 3.1
    frame #0: 0x000000000040052a a.out`main at main.cpp:6:14
(lldb) processor-trace show-instr-log                                                                                                                                                                                   thread #1: tid=3092991
    0x40050f <+15>: movl   $0x0, -0x8(%rbp)
    0x400516 <+22>: movl   $0x0, -0xc(%rbp)
    0x40051d <+29>: cmpl   $0x2710, -0xc(%rbp)       ; imm = 0x2710 
    0x400524 <+36>: jge    0x400546                  ; <+70> at main.cpp
    0x400524 <+36>: jge    0x400546                  ; <+70> at main.cpp

>>> The trace was re-enabled correctly and includes the instruction of the
first reading.

Those instructions correspond to these lines

3 int main() {
4  int z = 0;
5  for (int i = 0; i < 10000; i++) { 
6    z += fun(z)
...

Diff Detail

Event Timeline

wallace created this revision.Aug 4 2020, 12:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2020, 12:57 PM
wallace requested review of this revision.Aug 4 2020, 12:57 PM
wallace edited the summary of this revision. (Show Details)Aug 4 2020, 1:00 PM
wallace updated this revision to Diff 283009.Aug 4 2020, 1:01 PM

remove an empty line

wallace edited the summary of this revision. (Show Details)Aug 4 2020, 1:27 PM
wallace edited the summary of this revision. (Show Details)Oct 19 2020, 3:21 PM
wallace added a reviewer: labath.
labath accepted this revision.Oct 21 2020, 4:50 AM

Sounds reasonable.

This revision is now accepted and ready to land.Oct 21 2020, 4:50 AM
This revision was landed with ongoing or failed builds.Oct 23 2020, 4:37 PM
This revision was automatically updated to reflect the committed changes.