Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-11-2012, 10:58 AM
|
#1
|
|
LQ Newbie
Registered: May 2012
Posts: 28
Rep: 
|
problem with registers
i try to copy one string to another using strcpy() in c.
and in gdb i typed--info reg. But it says that
The program has no registers now.
Why i get this msg.Because in my opinion all process use registers?? Then why do i can't access registers??
Is strecpy() doesn't use registers?? pls tell me about that.
|
|
|
|
05-12-2012, 02:51 AM
|
#2
|
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,856
|
Hi,
please show us the code you've written, I think this would help us to find out more about your problem.
Also please describe in detail how you've compiled the program.
I'll report this thread and ask a Moderator to move it to the "Programming" forum of LQ.
Markus
|
|
|
|
05-12-2012, 09:23 AM
|
#3
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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.
|
|
|
|
05-13-2012, 10:38 AM
|
#4
|
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
Quote:
Originally Posted by markush
Hi,
please show us the code you've written, I think this would help us to find out more about your problem.
Also please describe in detail how you've compiled the program.
I'll report this thread and ask a Moderator to move it to the "Programming" forum of LQ.
Markus
|
i didn't get u. where can i find the programming forum???
my code is like this,
#include <stdio.h>
int main(int argc, char **argv[]) {
char b[256];
if(argc == 1) {
printf("Usage: %s input\n", argv[0]);
exit(0);
}
strcpy(b,argv[1]);
printf("%s", b);
}
and i compiled like this,
$gcc name.c -o name
$./name AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
$ gdb -c core ./name
(gdb) info reg
|
|
|
|
05-13-2012, 11:18 AM
|
#5
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,016
|
Quote:
|
Originally Posted by mr.cracker
i didn't get u. where can i find the programming forum???
|
This thread has been moved there (here).
Please use [code][/code] tags.
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
|
|
|
1 members found this post helpful.
|
05-14-2012, 11:53 PM
|
#6
|
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
Quote:
Originally Posted by ntubski
This thread has been moved there (here).
Please use [code][/code] tags.
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.
|
|
|
|
05-15-2012, 12:58 AM
|
#7
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,760
|
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....
|
|
|
|
05-15-2012, 03:42 AM
|
#8
|
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
Quote:
Originally Posted by pan64
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.??
|
|
|
|
05-15-2012, 03:53 AM
|
#9
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,760
|
you can try step, next, and also you can set breakpoint on the line strcpy or printf.
|
|
|
|
05-15-2012, 11:25 AM
|
#10
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,016
|
You probably want to run gdb like this:
Code:
gdb --args ./name AAAAAAAAA...
That way you'll actually be debugging the failing case. Also, you see those warnings I got when I compiled your code? You should fix those.
|
|
|
|
05-16-2012, 12:07 PM
|
#11
|
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
Quote:
Originally Posted by ntubski
You probably want to run gdb like this:
Code:
gdb --args ./name AAAAAAAAA...
That way you'll actually be debugging the failing case. Also, you see those warnings I got when I compiled your code? You should fix those.
|
Its working. BUt only edi is overwritten by "0x41" . The esp,eip is not overwritten why??
if any mistake in the code pls. help me??
the commands i used are,
(gdb) break main
(gdb) run
(gdb) step
(gdb) info reg
Then the it will exit from my c program. why??
|
|
|
|
05-16-2012, 12:35 PM
|
#12
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,760
|
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?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:22 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|