<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p style="margin-top:0;margin-bottom:0">Hi there,</p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">I have a problem using the function call tracing tools that is designed in llvm tools set. My aim is to record every function call that a program makes when it run. However, for whatever reason, a simple matrix multiply
 c program that I wrote cannot record all the function calls that happened when the program run. </p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">Here is the program: matrix.c</p>
<p style="margin-top:0;margin-bottom:0"></p>
<div>#include <stdio.h></div>
<div><br>
</div>
<div>void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);</div>
<div>void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) __attribute__((xray_always_instrument));</div>
<div>void display(int mult[][10], int rowFirst, int columnSecond);</div>
<div><br>
</div>
<div>int main()</div>
<div>{</div>
<div><span style="white-space:pre"></span>int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>printf("Enter rows and column for first matrix: ");</div>
<div><span style="white-space:pre"></span>scanf("%d %d", &rowFirst, &columnFirst);</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>printf("Enter rows and column for second matrix: ");</div>
<div><span style="white-space:pre"></span>scanf("%d %d", &rowSecond, &columnSecond);</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>// If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.</div>
<div><span style="white-space:pre"></span>while (columnFirst != rowSecond)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>printf("Error! column of first matrix not equal to row of second.\n");</div>
<div><span style="white-space:pre"></span>printf("Enter rows and column for first matrix: ");</div>
<div><span style="white-space:pre"></span>scanf("%d%d", &rowFirst, &columnFirst);</div>
<div><span style="white-space:pre"></span>printf("Enter rows and column for second matrix: ");</div>
<div><span style="white-space:pre"></span>scanf("%d%d", &rowSecond, &columnSecond);</div>
<div><span style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>// Function to take matrices data</div>
<div>        enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);</div>
<div><br>
</div>
<div>        // Function to multiply two matrices.</div>
<div>        multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);</div>
<div><br>
</div>
<div>        // Function to display resultant matrix after multiplication.</div>
<div>        display(mult, rowFirst, columnSecond);</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>return 0;</div>
<div>}</div>
<div><br>
</div>
<div>void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)</div>
<div>{</div>
<div><span style="white-space:pre"></span>int i, j;</div>
<div><span style="white-space:pre"></span>printf("\nEnter elements of matrix 1:\n");</div>
<div><span style="white-space:pre"></span>for(i = 0; i < rowFirst; ++i)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>for(j = 0; j < columnFirst; ++j)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>printf("Enter elements a%d%d: ", i + 1, j + 1);</div>
<div><span style="white-space:pre"></span>scanf("%d", &firstMatrix[i][j]);</div>
<div><span style="white-space:pre"></span>}</div>
<div><span style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>printf("\nEnter elements of matrix 2:\n");</div>
<div><span style="white-space:pre"></span>for(i = 0; i < rowSecond; ++i)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>for(j = 0; j < columnSecond; ++j)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>printf("Enter elements b%d%d: ", i + 1, j + 1);</div>
<div><span style="white-space:pre"></span>scanf("%d", &secondMatrix[i][j]);</div>
<div><span style="white-space:pre"></span>}</div>
<div><span style="white-space:pre"></span>}</div>
<div>}</div>
<div><br>
</div>
<div>void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)</div>
<div>{</div>
<div><span style="white-space:pre"></span>int i, j, k;</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>// Initializing elements of matrix mult to 0.</div>
<div><span style="white-space:pre"></span>for(i = 0; i < rowFirst; ++i)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>for(j = 0; j < columnSecond; ++j)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>mult[i][j] = 0;</div>
<div><span style="white-space:pre"></span>}</div>
<div><span style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span style="white-space:pre"></span>// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.</div>
<div><span style="white-space:pre"></span>for(i = 0; i < rowFirst; ++i)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>for(j = 0; j < columnSecond; ++j)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>for(k=0; k<columnFirst; ++k)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];</div>
<div><span style="white-space:pre"></span>}</div>
<div><span style="white-space:pre"></span>}</div>
<div><span style="white-space:pre"></span>}</div>
<div><br>
</div>
<div>}</div>
<div><br>
</div>
<div>void display(int mult[][10], int rowFirst, int columnSecond)</div>
<div>{</div>
<div><span style="white-space:pre"></span>int i, j;</div>
<div><span style="white-space:pre"></span>printf("\nOutput Matrix:\n");</div>
<div><span style="white-space:pre"></span>for(i = 0; i < rowFirst; ++i)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>for(j = 0; j < columnSecond; ++j)</div>
<div><span style="white-space:pre"></span>{</div>
<div><span style="white-space:pre"></span>printf("%d  ", mult[i][j]);</div>
<div><span style="white-space:pre"></span>if(j == columnSecond - 1)</div>
<div><span style="white-space:pre"></span>printf("\n\n");</div>
<div><span style="white-space:pre"></span>}</div>
<div><span style="white-space:pre"></span>}</div>
<div>}</div>
<div><br>
</div>
Here is the process when I tried to generate the function call:
<p></p>
<p style="margin-top:0;margin-bottom:0"></p>
<ol style="margin-bottom: 0px; margin-top: 0px;">
<li><span style="font-size: 12pt;"></span>I wrote a cmake file to configure it, since later on I would like to use xray on more complicated programs:</li></ol>
<p></p>
<p style="margin-top:0;margin-bottom:0">         </p>
<div>         set(CMAKE_C_COMPILER "/home/shangatlab/Desktop/llvm_01_23_19/build/bin/clang")</div>
<div>         set(CMAKE_CXX_COMPILER "/home/shangatlab/Desktop/llvm_01_23_19/build/bin/clang++")</div>
<div><br>
</div>
<div>         project("trace test project")</div>
<div>         set(CMAKE_C_FLAGS "-fxray-instrument -fxray-instruction-threshold=1")</div>
<div>         set(CMAKE_CXX_FLAGS "-fxray-instrument -fxray-instruction-threshold=1")</div>
<div><br>
</div>
<div>         add_executable(matrix matrix.c</div>
<div>                    ) </div>
<div>     As the official document said, I put on instruction threshold flag to 1, and is using the most recently released version of clang-9 that is built from source together with compiler-rt built with source.</div>
<div>     2.  I built it using command "make", and checked with objdump that there is xray_instr_map:</div>
<div>          
<div>          ./matrix:     file format elf64-x86-64</div>
<div><br>
</div>
<div>           Sections:</div>
<div>           Idx Name                 Size      VMA               LMA               File off  Algn</div>
<div>           27 xray_instr_map 00000100  0000000000637ac0  0000000000637ac0  00037ac0  2**0</div>
<div>                                     CONTENTS, ALLOC, LOAD, DATA</div>
<div>     3.  Then I use command "<span>XRAY_OPTIONS="patch_premain=true xray_mode=xray-basic verbosity=2" ./matrix </span>" to generate log file, which works perfectly:</div>
<div>           
<div>         ==22294==XRay: Log file in 'xray-log.matrix.G42i0m'</div>
<div>          Enter rows and column for first matrix: 1</div>
<div>          1</div>
<div>          Enter rows and column for second matrix: 1</div>
<div>          1</div>
<div><br>
</div>
<div>          Enter elements of matrix 1:</div>
<div>          Enter elements a11: 1</div>
<div><br>
</div>
<div>          Enter elements of matrix 2:</div>
<div>          Enter elements b11: 1</div>
<div><br>
</div>
<div>          Output Matrix:</div>
<div>          1  </div>
<div><br>
</div>
<div>          ==22294==Cleaned up log for TID: 22294</div>
<div>     4. Then I used the llvm-xray convert tool to make the trace log readable, by "<span style="font-size: 12pt;">llvm-xray convert -f yaml -symbolize -instr_map=./matrix xray-log.matrix.</span><span style="font-size: 12pt;">G42i0m</span>". However, it
 never shows the log trace of the function "<span>multiplyMatrices" which is definitely called  when the program run:</span></div>
<div><span>          
<div>          header:          </div>
<div>          version:         3</div>
<div>          type:            0</div>
<div>          constant-tsc:    true</div>
<div>          nonstop-tsc:     true</div>
<div>          cycle-frequency: 4000000000</div>
<div>        records:         </div>
<div>          - { type: 0, func-id: 1, function: main, cpu: 6, thread: 22294, process: 22294, kind: function-enter, tsc: 38825257620522, data: '' }</div>
<div>          - { type: 0, func-id: 2, function: enterData, cpu: 7, thread: 22294, process: 22294, kind: function-enter, tsc: 38836389634930, data: '' }</div>
<div>          - { type: 0, func-id: 2, function: enterData, cpu: 1, thread: 22294, process: 22294, kind: function-exit, tsc: 38840832349440, data: '' }</div>
<div>          - { type: 0, func-id: 4, function: display, cpu: 1, thread: 22294, process: 22294, kind: function-enter, tsc: 38840832373974, data: '' }</div>
<div>          - { type: 0, func-id: 4, function: display, cpu: 1, thread: 22294, process: 22294, kind: function-exit, tsc: 38840832476724, data: '' }</div>
<div>          - { type: 0, func-id: 1, function: main, cpu: 1, thread: 22294, process: 22294, kind: function-exit, tsc: 38840832485244, data: '' }</div>
<div>        ...</div>
<div><br>
</div>
<div><br>
</div>
<div>All I got is three other function traces, main enterData and display, but we can see from the function id that multiplyMatrices is definitely occupying function id 3. For any reason, it could not be printed out.</div>
<div><br>
</div>
<div><br>
</div>
<div>--I have tried to change the XRAY_OPTIONS <span>patch_premain=false, as suggested in the document that this is the right thing to do when you want to record every function. However, I encounter a question like this:</span></div>
<div><span><br>
</span></div>
<div><span>
<div>==22930==XRay: Log file in 'xray-log.matrix.Uybc6L'</div>
<div>Enter rows and column for first matrix: 1</div>
<div>1</div>
<div>Enter rows and column for second matrix: 1</div>
<div>1</div>
<div><br>
</div>
<div>Enter elements of matrix 1:</div>
<div>Enter elements a11: 1</div>
<div><br>
</div>
<div>Enter elements of matrix 2:</div>
<div>Enter elements b11: 1</div>
<div><br>
</div>
<div>Output Matrix:</div>
<div>1  </div>
<div><br>
</div>
<div>==22930==Skipping buffer for TID: 22930; Offset = 0</div>
<div>==22930==Cleaned up log for TID: 22930</div>
<div><br>
</div>
<div>
<div>llvm-xray: Failed loading input file 'xray-log.matrix.xEr6h8'.</div>
<div>Cannot read log from 'xray-log.matrix.xEr6h8'</div>
<div><br>
</div>
<div>--I have also tried to put some redundant code in multiplyMatrices function like scanf & printf, which are the only difference between this function and other two function enter and display data, that there is no interact with console in multiply function.
 With the same command lines being used, it printed out all the real function calls trace:</div>
<div>
<div>header:          </div>
<div>  version:         3</div>
<div>  type:            0</div>
<div>  constant-tsc:    true</div>
<div>  nonstop-tsc:     true</div>
<div>  cycle-frequency: 4000000000</div>
<div>records:         </div>
<div>  - { type: 0, func-id: 1, function: main, cpu: 7, thread: 22687, process: 22687, kind: function-enter, tsc: 40570233068134, data: '' }</div>
<div>  - { type: 0, func-id: 2, function: enterData, cpu: 1, thread: 22687, process: 22687, kind: function-enter, tsc: 40579457699794, data: '' }</div>
<div>  - { type: 0, func-id: 2, function: enterData, cpu: 6, thread: 22687, process: 22687, kind: function-exit, tsc: 40584365232344, data: '' }</div>
<div>  - { type: 0, func-id: 3, function: multiplyMatrices, cpu: 6, thread: 22687, process: 22687, kind: function-enter, tsc: 40584365247874, data: '' }</div>
<div>  - { type: 0, func-id: 3, function: multiplyMatrices, cpu: 6, thread: 22687, process: 22687, kind: function-exit, tsc: 40595952636280, data: '' }</div>
<div>  - { type: 0, func-id: 4, function: display, cpu: 6, thread: 22687, process: 22687, kind: function-enter, tsc: 40595952651332, data: '' }</div>
<div>  - { type: 0, func-id: 4, function: display, cpu: 6, thread: 22687, process: 22687, kind: function-exit, tsc: 40595952712424, data: '' }</div>
<div>  - { type: 0, func-id: 1, function: main, cpu: 6, thread: 22687, process: 22687, kind: function-exit, tsc: 40595952721582, data: '' }</div>
<div><br>
</div>
<br>
</div>
<div>I am really desperate about this. Does anyone has any idea why this will happen? Thanks in advance.</div>
<div><span style="font-size: 12pt;"> </span><br>
</div>
<div><br>
</div>
<br>
</div>
<div><br>
</div>
<br>
</span></div>
<div><br>
</div>
</span></div>
<div><br>
</div>
</div>
<div>          </div>
<div><br>
</div>
</div>
<p></p>
</div>
</body>
</html>