LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-11-2012, 10:58 AM   #1
mr.cracker
Member
 
Registered: May 2012
Posts: 58

Rep: Reputation: Disabled
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.
 
Old 05-12-2012, 02:51 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
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
 
Old 05-12-2012, 09:23 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 05-13-2012, 10:38 AM   #4
mr.cracker
Member
 
Registered: May 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thumbs down

Quote:
Originally Posted by markush View Post
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
 
Old 05-13-2012, 11:18 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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).

Quote:
my code is like this,
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.
Old 05-14-2012, 11:53 PM   #6
mr.cracker
Member
 
Registered: May 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
Exclamation

Quote:
Originally Posted by ntubski View Post
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.
 
Old 05-15-2012, 12:58 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,795

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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....
 
Old 05-15-2012, 03:42 AM   #8
mr.cracker
Member
 
Registered: May 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.??
 
Old 05-15-2012, 03:53 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,795

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
you can try step, next, and also you can set breakpoint on the line strcpy or printf.
 
Old 05-15-2012, 11:25 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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.
 
Old 05-16-2012, 12:07 PM   #11
mr.cracker
Member
 
Registered: May 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
Question

Quote:
Originally Posted by ntubski View Post
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??
 
Old 05-16-2012, 12:35 PM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,795

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem accessing control registers (cr0,cr2,cr3) Phobios Programming 1 09-15-2011 01:44 PM
Problem using SA-1100 GPIO registers Milosman Linux - Embedded & Single-board computer 3 12-14-2010 01:41 AM
I2C problem (registers not writable) lqu Linux - Embedded & Single-board computer 0 11-08-2010 11:45 AM
registers strycnine Programming 4 05-17-2007 11:44 AM
How to access registers yugandhar Linux - Laptop and Netbook 0 06-12-2006 04:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration