Sign in to follow this  
Followers 0
ansichart

ltrace shows that srand() has 5 paramaters.

9 posts in this topic

Here's my C code


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

int main(int argc, char **argv){
int i;

srand( time(NULL) );
for (i = 0; i < 15; i++) printf("%d ",(rand() % 100));
printf("\n");
return 0;
}

Here is me executing ltrace on the compiled program

$ ltrace -e srand ./hello

srand(0x4cd4fadb, 0x2a4794, 878082, 0x20c49ba5e353f7cf, 0xffffffff) = 0

17 15 44 67 54 47 63 68 99 59 13 56 16 19 67

+++ exited (status 0) +++

For some reason, ltrace is showing that there are 5 parameters being passed to srand... I was expecting one. What's going on here?

0

Share this post


Link to post
Share on other sites

What OS/distro and compiler are you using?

OS: Ubuntu 10.04 LTS

Kernel: 2.6.32-25-generic

Architecture: x86-64

Compiler: GCC version 4.4.3

0

Share this post


Link to post
Share on other sites

I just looked at it with dtrace. I'll take a look at it with ltrace on a linux system this week sometime. I need to spin up a linux VM.


shawn@shawn-work:~$ uname -a
SunOS shawn-work 5.11 oi_147 i86pc i386 i86pc Solaris
shawn@shawn-work:~$ cat test.d
pid$target:libc:srand:entry
{
trace(arg0);
}
shawn@shawn-work:~$ pfexec dtrace -s test.d -c ./test
dtrace: script 'test.d' matched 1 probe
16 60 58 83 23 49 19 96 72 70 49 92 5 47 60
dtrace: pid 21456 has exited
CPU ID FUNCTION:NAME
0 71978 srand:entry 1289239423

edit[0]: I know that showing dtrace doesn't help much in your case, but I thought I'd take the time to market it anyways. ;)

Edited by lattera
0

Share this post


Link to post
Share on other sites

Pretty interesting. I checked it out and i'm getting the same result. Could srand() be a wrapper for something? If anyone knows i'd be interested in the answer.

0

Share this post


Link to post
Share on other sites

I'm not sure about how it works in D, but in the C and C++ languages, srand() is the "Seed Random" function. You supply it with a base value that all random numbers generated will be based on.

I'm pretty sure the following example demonstrates what I mean:

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

int main(void) {
srand(time(NULL));
int r = rand();
printf("%d\n", r);
return 0;
}

In this case, we are saying that all random numbers generated will be based on the time of the system whenever the function is called.

0

Share this post


Link to post
Share on other sites

*SOLVED*

Jester01 on ##asm@freenode figured out why.

srand is missing from /etc/ltrace.conf

Thus, adding "void srand(uint);" to it fixes the problem.

0

Share this post


Link to post
Share on other sites

*SOLVED*

Jester01 on ##asm@freenode figured out why.

srand is missing from /etc/ltrace.conf

Thus, adding "void srand(uint);" to it fixes the problem.

So what are the other values? Random values on the stack or something?

0

Share this post


Link to post
Share on other sites

So what are the other values? Random values on the stack or something?

If it can't find the function prototype in the configuration file, it will display 5 parameters. I'm not sure if those parameters were grabbed from the stack, or if they were in the registers. Since this is on x86-64 architecture, the function parameter are actually passed via registers.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0