how to produce trace of C program?

K

kj

Is there any good way to produce a trace of a running C program
analogous to what one gets with "sh -x" or "perl -dS"? (I work on
Linux.)

I imagine that, if such trace is possible, it would have to be a
feature of the compiler, but I was not able to find anything like
it in the manpage for mine (gcc 3.0).

TIA,

kj
 
?

=?iso-8859-1?Q?Jos=E9_Mar=EDa?= Mateos

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Is there any good way to produce a trace of a running C program
analogous to what one gets with "sh -x" or "perl -dS"? (I work on
Linux.)

Perhaps strace can do what you want?

Regards.

- --
My real e-mail address: chema (AT) chema.homelinux.org
http://EuropeSwPatentFree.hispalinux.es - EuropeSwPatentFree
I don't read HTML posts / No leo mensajes en HTML
Blog Overflow: http://chema.homelinux.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAw22P9P6GbSlI+hkRAp7dAKCXnw54M4E8XGlCn0DLxIoWcn4u+gCgkxwv
/Oj2m0Na+aAOd4HDVBa26tI=
=DMXJ
-----END PGP SIGNATURE-----
 
K

Kieran Simkin

kj said:
Is there any good way to produce a trace of a running C program
analogous to what one gets with "sh -x" or "perl -dS"? (I work on
Linux.)

I imagine that, if such trace is possible, it would have to be a
feature of the compiler, but I was not able to find anything like
it in the manpage for mine (gcc 3.0).

This question is off-topic. This newsgroup deals only with the standard C
language, not specific compilers and operating systems.

However, what you're looking for is probably gdb. Compile your programs with
the -g flag to gcc, then a gdb session might look like this:

$ gdb ./program
(gdb) break main
Breakpoint 1 at 0x804925c
(gdb) run
Starting program: ./program
Breakpoint 1, 0x804925c in main ()
(gdb) next

etc.

You could also look into strace if you specifically need to attach to an
already-running program, although this will only give you details on system
calls that're being used.
 
K

Karthik

kj said:
Is there any good way to produce a trace of a running C program
analogous to what one gets with "sh -x" or "perl -dS"? (I work on
Linux.)

There is a very big difference in the seemingly similar analogy that
you are making - which is that = sh and perl are interpreter , where C
is compiled.
Having said that, you have to decide what is it primarily that you
want to trace. As Kieran had pointed out already, you can use 'gdb' or
the debugger on your platform to trace the values of variables, function
stack ( bt command in gdb ) etc.
I imagine that, if such trace is possible, it would have to be a
feature of the compiler, but I was not able to find anything like
it in the manpage for mine (gcc 3.0).

The best place to look for it is the GNU Debugger manual and not the
compiler. Probably the only thing you need to take from the compiler
manual is that , you need to compile with the debugging switch on, for
the debugger to load the name symbols from the exec.

HTH
 
S

Stephen L.

kj said:
Is there any good way to produce a trace of a running C program
analogous to what one gets with "sh -x" or "perl -dS"? (I work on
Linux.)

I imagine that, if such trace is possible, it would have to be a
feature of the compiler, but I was not able to find anything like
it in the manpage for mine (gcc 3.0).

You might want to see if `ctrace' is available
on your platform...


DESCRIPTION
The ctrace command allows the user to monitor the sequential
execution of a C program as each program statement executes.
The effect is similar to executing a shell procedure with
the -x option.

...


-
Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top