I showed you. Sort of. Use "ps aux".
Here's a real example.
I'll compile and run the code to make sure it works. Notice that I compile with debugging information (-g).
Code:
$ cat greet.c
#include <stdio.h>
#include <string.h>
int main() {
char name[BUFSIZ], *p;
printf("Enter your name: ");
fgets(name, sizeof name, stdin);
if((p = strchr(name, '\n'))) *p = 0;
printf("Greetings, %s.\n", name);
return 0;
}
$ gcc -g greet.c -o greet
$ ./greet
Enter your name: dwk
Greetings, dwk.
$
Looks good. Now I'll run the program again:
Code:
$ ./greet
Enter your name:
In another terminal, I now type the following. "px aux" lists the processes which are running. grep searches for the string "greet" in this list; it finds the process and it finds itself too.The second number in ps aux's output is the process ID. The command on the right shows you which program is which.
Code:
$ ps aux | grep greet
dwk 29133 0.0 0.0 1516 328 pts/5 S+ 16:27 0:00 ./greet
dwk 29135 0.0 0.0 3908 668 pts/6 S+ 16:27 0:00 grep greet
$ gdb ./greet 29133
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
Attaching to program: greet, process 29133
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
0x008d8402 in __kernel_vsyscall ()
(gdb)
I've attached onto the process, and it is stopped for now by GDB until I type "continue" (or "c" for short). Now I can see what's happening.
Code:
(gdb) bt
#0 0x008d8402 in __kernel_vsyscall ()
#1 0x0099aa43 in __read_nocancel () from /lib/libc.so.6
#2 0x0093ba28 in _IO_file_read_internal () from /lib/libc.so.6
#3 0x0093cd80 in _IO_new_file_underflow () from /lib/libc.so.6
#4 0x0093d47b in _IO_default_uflow_internal () from /lib/libc.so.6
#5 0x0093e7ed in __uflow () from /lib/libc.so.6
#6 0x00932176 in _IO_getline_info_internal () from /lib/libc.so.6
#7 0x009320c1 in _IO_getline_internal () from /lib/libc.so.6
#8 0x0093103a in fgets () from /lib/libc.so.6
#9 0x08048443 in main () at greet.c:8
(gdb) frame 9
#9 0x08048443 in main () at greet.c:8
8 fgets(name, sizeof name, stdin);
(gdb) print name
$1 = '\0' <repeats 5492 times>,
"�\203*\000\000\000\000\000�%\000\000�����/+\000����@�)\
000\000\220�\000�%\000\000\003\000\000\0002\000\000\000����", '\0'
<repeats 45 times>,
"\220\215\000\000`�\000\234Q�\000\234Q�\000\000\000\000\000\005\000\000\00
0\000`�\000\000\220�\000|\211�\000���\000\000�\023\000\003\000\000
\0001�*\000�/+\000j\002\000\000X6+\0004���e�*\000\b\000\000\000�/+
\000/\000\000\000\000���L���e�*\000\b\000\000\000\017\000\000\000�
��/+\000\230���\\)*\000p����?+\000\017\000\000\000\001\000\000\000
\000"...
(gdb)
Hmm, looks like name is uninitialized. Let's set a breakpoint after this line, to let fgets() finish.
Code:
(gdb) l
3
4 int main() {
5 char name[BUFSIZ], *p;
6
7 printf("Enter your name: ");
8 fgets(name, sizeof name, stdin);
9
10 if((p = strchr(name, '\n'))) *p = 0;
11
12 printf("Greetings, %s.\n", name);
(gdb) break 12
Breakpoint 1 at 0x8048468: file greet.c, line 12.
(gdb) continue
Continuing.
Now GDB has released control of the program and it's running once again. At this point I go to the other terminal and type "dwk" into the program. Back in GDB,
Code:
Breakpoint 1, main () at greet.c:12
12 printf("Greetings, %s.\n", name);
(gdb) print name
$2 = "dwk", '\0' <repeats 5489 times>,
"�\203*\000\000\000\000\000�%\000\000�����/+\000����@�)\
000\000\220�\000�%\000\000\003\000\000\0002\000\000\000����", '\0'
<repeats 45 times>,
"\220\215\000\000`�\000\234Q�\000\234Q�\000\000\000\000\000\005\000\000\00
0\000`�\000\000\220�\000|\211�\000���\000\000�\023\000\003\000\000
\0001�*\000�/+\000j\002\000\000X6+\0004���e�*\000\b\000\000\000�/+
\000/\000\000\000\000���L���e�*\000\b\000\000\000\017\000\000\000�
��/+\000\230���\\)*\000p����?+\000\017\000\000\000\001\000"...
(gdb) continue
Continuing.
Program exited normally.
(gdb) quit
$
So there you have it.