ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves. Thank you markush for reporting.
Regarding the original question, you can't look at register values until the program starts running:
Code:
~/tmp$ make name
gcc -Wall -Wextra -Wformat=2 -std=gnu99 -g name.c -o name
name.c:3:5: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
name.c: In function ‘main’:
name.c:8:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
name.c:9:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
name.c:9:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
name.c:12:5: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
name.c:12:5: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default]
name.c:12:5: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [enabled by default]
name.c:12:5: note: expected ‘const char *’ but argument is of type ‘char **’
~/tmp$ ./name AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~/tmp$
~/tmp$ gdb ./name
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/npostavs/tmp/name...done.
(gdb) break main
Breakpoint 1 at 0x40059c: file name.c, line 7.
(gdb) run
Starting program: /home/npostavs/tmp/name
Breakpoint 1, main (argc=1, argv=0x7fffffffea08)
at name.c:7
7 if(argc == 1) {
(gdb) info reg
rax 0x7ffff7dd9ee8 140737351884520
rbx 0x0 0
rcx 0x0 0
rdx 0x7fffffffea18 140737488349720
rsi 0x7fffffffea08 140737488349704
rdi 0x1 1
rbp 0x7fffffffe920 0x7fffffffe920
rsp 0x7fffffffe810 0x7fffffffe810
r8 0x7ffff7dd8320 140737351877408
r9 0x7ffff7deb060 140737351954528
r10 0x0 0
r11 0x7ffff7a74db0 140737348324784
r12 0x4004a0 4195488
r13 0x7fffffffea00 140737488349696
r14 0x0 0
r15 0x0 0
rip 0x40059c 0x40059c <main+24>
eflags 0x202 [ IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
Regarding the original question, you can't look at register values until the program starts running:
Code:
~/tmp$ make name
gcc -Wall -Wextra -Wformat=2 -std=gnu99 -g name.c -o name
name.c:3:5: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
name.c: In function ‘main’:
name.c:8:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
name.c:9:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
name.c:9:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
name.c:12:5: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration]
name.c:12:5: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default]
name.c:12:5: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [enabled by default]
name.c:12:5: note: expected ‘const char *’ but argument is of type ‘char **’
~/tmp$ ./name AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~/tmp$
~/tmp$ gdb ./name
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/npostavs/tmp/name...done.
(gdb) break main
Breakpoint 1 at 0x40059c: file name.c, line 7.
(gdb) run
Starting program: /home/npostavs/tmp/name
Breakpoint 1, main (argc=1, argv=0x7fffffffea08)
at name.c:7
7 if(argc == 1) {
(gdb) info reg
rax 0x7ffff7dd9ee8 140737351884520
rbx 0x0 0
rcx 0x0 0
rdx 0x7fffffffea18 140737488349720
rsi 0x7fffffffea08 140737488349704
rdi 0x1 1
rbp 0x7fffffffe920 0x7fffffffe920
rsp 0x7fffffffe810 0x7fffffffe810
r8 0x7ffff7dd8320 140737351877408
r9 0x7ffff7deb060 140737351954528
r10 0x0 0
r11 0x7ffff7a74db0 140737348324784
r12 0x4004a0 4195488
r13 0x7fffffffea00 140737488349696
r14 0x0 0
r15 0x0 0
rip 0x40059c 0x40059c <main+24>
eflags 0x202 [ IF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
sir.thank u.its working. BUt if i give more number of "A" as argument (suppose i give 500 "A" as argument). Then the esp will overwrite with A(0x41). But i can't view the registers that are overwriten by "A". What i want to do for that. If u know pls help me sir.
you need to step over. It means you enter the command next in gdb and you will see the lines as they executed. you need to step further to see what will happen....
you need to step over. It means you enter the command next in gdb and you will see the lines as they executed. you need to step further to see what will happen....
i didn't get u. in gdb i first create a break point and run it. Then when i use next command that u had said,the running program exit.I didn't see any overwritten registers.
what i will do.??
strcpy will not overwrite registers. it will overwrite the area where those register points (that is the stack or heap itself).
I do not really understand why do you expect that?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.