<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57657>57657</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Xray produces an invalid/confusing log file when no data is generated. 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          HFTrader
      </td>
    </tr>
</table>

<pre>
    Xray produces a confusing log file when no data is generated. 

For debugging this I wrote a small test

```c++
// test.cpp
void doit() {
}

void passthrough() {
    doit();
}

int main() {
    passthrough();
    return 0;
}
```
Then I compile and run the above example using clang 14.0.6 (taken straight from Github)
```bash
$ clang++ -O3 test.cpp -fxray-instrument -fxray-instruction-threshold=1 -o test
$ ls -1
attr.txt
test.cpp
test
```
According to the documentation, I have to run with environment variables set up 
```bash
$ XRAY_OPTIONS="patch_premain=true  xray_mode=xray-basic verbosity=1"  ./test
==219673==XRay: Log file in 'xray-log.test.ym8oWP'
==219673==Skipping buffer for TID: 219673; Offset = 0
==219673==Cleaned up log for TID: 219673
```
The trace file at first seems generated ok
```bash
$ ls -1
attr.txt
test
test.cpp
xray-log.test.ym8oWP
```
But when I try to look into the trace file, I get the following error

```
$ llvm-xray account --instr_map=./test xray-log.test.ym8oWP 
llvm-xray: Failed loading input file 'xray-log.test.ym8oWP'
Unsupported version for Basic/Naive Mode logging: 768
```

When I look into the binary, everything seems fine
```bash
$ llvm-xray extract ./test 
---
- { id: 1, address: 0x420EA0, function: 0x420EA0, kind: function-enter, always-instrument: false, function-name: '', version: 2 }
- { id: 1, address: 0x420EAC, function: 0x420EA0, kind: function-exit, always-instrument: false, function-name: '', version: 2 }
- { id: 2, address: 0x420EC0, function: 0x420EC0, kind: function-enter, always-instrument: false, function-name: '', version: 2 }
- { id: 2, address: 0x420ECC, function: 0x420EC0, kind: function-exit, always-instrument: false, function-name: '', version: 2 }
- { id: 3, address: 0x420EE0, function: 0x420EE0, kind: function-enter, always-instrument: false, function-name: '', version: 2 }
- { id: 3, address: 0x420EEE, function: 0x420EE0, kind: function-exit, always-instrument: false, function-name: '', version: 2 }
...
```
The trace file has 0x0300 as the first 2 bytes while the [source code](https://llvm.org/doxygen/XRay_2Trace_8cpp_source.html) states that the enumeration is either 0x01 (basic) or 0x02 (fdr).
```bash
$ hexdump -C ./xray-log.test.d0IxRD 
00000000  03 00 00 00 ff 7f 00 00  68 4e 9d 1e 01 00 00 00  |........hN......|
00000010  00 20 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |. ..............|
00000020
```
The sources for that compiler version show that the headers should look like one of the following 
```
  //   0x01 0x00 0x00 0x00 - version 1, "naive" format
  //   0x01 0x00 0x01 0x00 - version 1, "flight data recorder" format
  //   0x02 0x00 0x01 0x00 - version 2, "flight data recorder" format
```
I can see these 4 bytes starting at position 12 but wondering why llvm-xray is looking at byte zero? 

I guess the key is the message " Skipping buffer for TID: 219673; Offset = 0" 

The generated empty file looks okay also - I can see the same prefix bytes in another populated log file. It is the parsing of the log file that is causing it to display a confusing message. 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFV0tz4jgQ_jVw6cJly4THgUNCwm6qdidTM9ma7Ckl2zLWxpZckhxgf_12ywZMBrKTw9RQxgg9PrW-firR2W7xZPgOaqOzJhUWOKRa5Y2Vag2lXkMuSwGbQihQGjLuOEgLa6GE4U5kAQzC20F43b5X2kAmkma9ptWuwJn3sDHaCYS1FS9LcMK6_pLBJGyfdMBu6Gl72QofPzlI67rtfNUyg0xLN2CzAZvDYLqfPb3tQ_p5NbfWFUY36-LtdMDPEWYQn0eRykHFpTq3-jvwAwiNGuEaoyA8g7w_bPv3kVi9R76rmkjmKgPTKOQN24l-FSC2vKpxpNVGWnJ8R-MgDCaA-zr-guutM1yuCwe50RX8Jl3RJCTR6YYJt8We2nGL1PINo4f4wDOM8i0aw0gqRG0qgRSc9KROajXCkwt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