Skip to content

Instantly share code, notes, and snippets.

@aditya7fb
Created June 3, 2020 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aditya7fb/8d9ed4d86678b409e3b28d885c95e29a to your computer and use it in GitHub Desktop.
Save aditya7fb/8d9ed4d86678b409e3b28d885c95e29a to your computer and use it in GitHub Desktop.
running function entry instrumentation on quicksort example from G4G
/**
./bin/clang++ test.cpp test1.cpp -arch arm64 -mllvm -enable-func-entry-instrumentation -O2 -mllvm -func-entry-instrumentation-writemethod=1
Contents of section __la_symbol_ptr:
10000c000 247f0000 01000000 307f0000 01000000 $.......0.......
10000c010 3c7f0000 01000000 <.......
Contents of section __llvm_funcentry:
10000c020 dcdfe5c6 5cf4ac04 1d030000 00acacac ....\...........
10000c030 dcdb78b3 cc9999ee dd040000 00acacac ..x.............
10000c040 ac .
Contents of section __llvm_femapping:
10000c050 dcdfe5c6 5cf4ac04 1d030000 00f195a9 ....\...........
10000c060 8a2f70a5 840d0000 00d1bbbe e85e7b89 ./p..........^{.
10000c070 3e0e0000 00c3ec88 8923205e 380f0000 >........# ^8...
10000c080 00000000 00000000 00000000 00000000 ................
10000c090 dcdb78b3 cc9999ee dd040000 00420bc1 ..x..........B..
10000c0a0 4550ce40 300d0000 000ee7f6 4ec18b82 EP.@0.......N...
10000c0b0 0e0e0000 000bd51d f65fbf92 300f0000 ........._..0...
10000c0c0 00e8c546 0e951330 ae100000 00 ...F...0.....
*/
//char a[3] ={1,2,3};
//char b[] = {4,5,6,7,8,9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22};
/* C implementation QuickSort */
#include<stdio.h>
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
//printf("Sorted array: n");
//printArray(arr, n);
return 0;
}
#include<stdio.h>
// A utility function to swap two elements
void swap1(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition1 (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap1(&arr[i], &arr[j]);
}
}
swap1(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort1(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition1(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort1(arr, low, pi - 1);
quickSort1(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray1(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main1()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort1(arr, 0, n-1);
printf("Sorted array: n");
printArray1(arr, n);
return 0;
}
@aditya7fb
Copy link
Author

Using xray:

$ ./bin/clang++  test.cpp test1.cpp -O2  -fxray-instrument -fxray-instruction-threshold=10
$ objdump -S a.out

The output of relevant sections:

Contents of section xray_instr_map:
 100002018 c0080000 01000000 c0080000 01000000  ................
 100002028 00000000 00000000 00000000 00000000  ................
 100002038 d8080000 01000000 c0080000 01000000  ................
 100002048 01000000 00000000 00000000 00000000  ................
 100002058 f0080000 01000000 f0080000 01000000  ................
 100002068 00000000 00000000 00000000 00000000  ................
 100002078 50090000 01000000 f0080000 01000000  P...............
 100002088 01000000 00000000 00000000 00000000  ................
 100002098 b0090000 01000000 b0090000 01000000  ................
 1000020a8 00000000 00000000 00000000 00000000  ................
 1000020b8 ae0a0000 01000000 b0090000 01000000  ................
 1000020c8 01000000 00000000 00000000 00000000  ................
 1000020d8 c00a0000 01000000 c00a0000 01000000  ................
 1000020e8 00000000 00000000 00000000 00000000  ................
 1000020f8 140b0000 01000000 c00a0000 01000000  ................
 100002108 02000000 00000000 00000000 00000000  ................
 100002118 300b0000 01000000 300b0000 01000000  0.......0.......
 100002128 00000000 00000000 00000000 00000000  ................
 100002138 8e0b0000 01000000 300b0000 01000000  ........0.......
 100002148 01000000 00000000 00000000 00000000  ................
 100002158 a00b0000 01000000 a00b0000 01000000  ................
 100002168 00000000 00000000 00000000 00000000  ................
 100002178 b80b0000 01000000 a00b0000 01000000  ................
 100002188 01000000 00000000 00000000 00000000  ................
 100002198 d00b0000 01000000 d00b0000 01000000  ................
 1000021a8 00000000 00000000 00000000 00000000  ................
 1000021b8 300c0000 01000000 d00b0000 01000000  0...............
 1000021c8 01000000 00000000 00000000 00000000  ................
 1000021d8 900c0000 01000000 900c0000 01000000  ................
 1000021e8 00000000 00000000 00000000 00000000  ................
 1000021f8 8e0d0000 01000000 900c0000 01000000  ................
 100002208 01000000 00000000 00000000 00000000  ................
 100002218 a00d0000 01000000 a00d0000 01000000  ................
 100002228 00000000 00000000 00000000 00000000  ................
 100002238 f40d0000 01000000 a00d0000 01000000  ................
 100002248 02000000 00000000 00000000 00000000  ................
 100002258 100e0000 01000000 100e0000 01000000  ................
 100002268 00000000 00000000 00000000 00000000  ................
 100002278 de0e0000 01000000 100e0000 01000000  ................
 100002288 01000000 00000000 00000000 00000000  ................
Contents of section xray_fn_idx:
 1000022a0 18200000 01000000 58200000 01000000  . ......X ......
 1000022b0 58200000 01000000 98200000 01000000  X ....... ......
 1000022c0 98200000 01000000 d8200000 01000000  . ....... ......
 1000022d0 d8200000 01000000 18210000 01000000  . .......!......
 1000022e0 18210000 01000000 18200000 01000000  .!....... ......
 1000022f0 58210000 01000000 98210000 01000000  X!.......!......
 100002300 98210000 01000000 d8210000 01000000  .!.......!......
 100002310 d8210000 01000000 18220000 01000000  .!......."......
 100002320 18220000 01000000 58220000 01000000  ."......X"......
 100002330 58220000 01000000 58210000 01000000  X"......X!......

Using function entry instrumentation:

$ ./bin/clang++  test.cpp test1.cpp  -mllvm -enable-func-entry-instrumentation  -O2 -mllvm -func-entry-instrumentation-writemethod=1
$ objdump -s a.out

The output of relevant sections:

Contents of section __llvm_funcentry:
 100002020 dcdfe5c6 5cf4ac04 1d030000 00acacac  ....\...........
 100002030 dcdb78b3 cc9999ee dd040000 00acacac  ..x.............
 100002040 ac                                   .
Contents of section __llvm_femapping:
 100002050 dcdfe5c6 5cf4ac04 1d030000 00f195a9  ....\...........
 100002060 8a2f70a5 840d0000 00d1bbbe e85e7b89  ./p..........^{.
 100002070 3e0e0000 00c3ec88 8923205e 380f0000  >........# ^8...
 100002080 00000000 00000000 00000000 00000000  ................
 100002090 dcdb78b3 cc9999ee dd040000 00420bc1  ..x..........B..
 1000020a0 4550ce40 300d0000 000ee7f6 4ec18b82  EP.@0.......N...
 1000020b0 0e0e0000 000bd51d f65fbf92 300f0000  ........._..0...
 1000020c0 00e8c546 0e951330 ae100000 00        ...F...0.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment