linuxreverse-engineeringltrace

How does ltrace() display rand()


When ltrace hits a rand function, it shows it with 4 paramters, like this: rand(0, 0x5649bd4e6010, 0x7f0955490760, 0x7f09551cf7b0) = 0x17382962

rand doesn't take any arguments. What is ltrace showing here?

Edited to add example:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand((unsigned int)time(NULL));
    int r = (rand() % 4096);
    printf("The number is: %d\n", r);
} 

Compile and run with ltrace:

$ ltrace ./demo 
__libc_start_main(0x4005f6, 1, 0x7ffe1e719fa8, 0x400650 <unfinished ...>
time(0)                                                                                                      = 1515331941
srand(0x5a522165, 0x7ffe1e719fa8, 0x7ffe1e719fb8, 0)                                                         = 0
rand(0x7f75b1a4b620, 0x7ffe1e719e7c, 0x7f75b1a4b0a4, 0x7f75b1a4b11c)                                         = 0x354b8023
printf("The number is: %d\n", 35The number is: 35
)                                                                            = 18
+++ exited (status 0) +++

$ ltrace ./demo 
__libc_start_main(0x4005f6, 1, 0x7fffa0bf3a18, 0x400650 <unfinished ...>
time(0)                                                                                                      = 1515331963
srand(0x5a52217b, 0x7fffa0bf3a18, 0x7fffa0bf3a28, 0)                                                         = 0
rand(0x7f6e22884620, 0x7fffa0bf38ec, 0x7f6e228840a4, 0x7f6e2288411c)                                         = 0x6667c0f4
printf("The number is: %d\n", 244The number is: 244
)                                                                           = 19
+++ exited (status 0) +++

What are the parameters being shown for rand?

rand(0x7f6e22884620, 0x7fffa0bf38ec, 0x7f6e228840a4, 0x7f6e2288411c)                                         = 0x6667c0f4

What are 0x7f6e22884620, 0x7fffa0bf38ec, 0x7f6e228840a4, 0x7f6e2288411c?


Solution

  • ltrace shows the content of the few registers passing arguments, according to x86-64 ABI conventions.

    For other functions, ltrace knows their API (i.e. their signature) so show arguments more cleverly.

    See ltrace(1) and the PROTOTYPE LIBRARY DISCOVERY section.