LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-08-2004, 06:19 AM   #1
altosys
LQ Newbie
 
Registered: Jun 2004
Posts: 6

Rep: Reputation: 0
Linux Kernel Internals


Can any1 provide me with details of :
strace ./a.out

where ./a.out is the output of a simple program printf-ing Welcome ONLY.

I would like a detailed explaination of all the memory mapping, et al. of the strace command.
 
Old 06-08-2004, 12:26 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
the mapping is for libC and ld.
 
Old 06-08-2004, 02:52 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Strace is not about memory mappings. It just shows system function calls. Memory mappings of a running program can be found in /proc/PID/maps.
 
Old 06-08-2004, 05:16 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
strace shows every *syscall* that your program makes, along with the arguments to it and the return value. You can look up any syscall in the man pages like "man 2 open" "man 2 mmap" etc. I'm not sure why I get "old_mmap" in my output; there is only a page for mmap. Other than that you should be able to look up all the calls your program is making.
 
Old 06-08-2004, 07:40 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
u guys seemed to have missed his question. he is asking why mmap() is called, even tho your program doesn't call it. and the answer is as i said above, every program that is linked with libC maps the libc text segment into its address space, and also the libc data and bss sections; in addition the dynamic linker, ld has it's text and data areas mapped as well.
Code:
[n00b@localho.outernet] cat > hw.c
int main() { printf("foo\n"); return 0;}
[n00b@localho.outernet] gcc hw.c 
[n00b@localho.outernet] strace -v ./a.out 
execve("./a.out", ["./a.out"], [/* 31 vars */]) = 0
brk(0)                                  = 0x80494a8
open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)
#open up ld and get its size for mapping
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_dev=makedev(3, 67), st_ino=586277, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=72, st_size=33730, st_atime=2004/06/08-20:34:14, st_mtime=2004/06/08-14:13:40, st_ctime=2004/06/08-14:13:40}) = 0
old_mmap(NULL, 33730, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40015000
close(3)                                = 0 
#open up libc and get its size
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0p\\\1\000"..., 1024) = 1024
fstat64(3, {st_dev=makedev(3, 67), st_ino=649660, st_mode=S_IFREG|0755, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=2816, st_size=1435624, st_atime=2004/06/08-20:34:14, st_mtime=2003/03/05-00:57:19, st_ctime=2004/01/01-13:42:43}) = 0
old_mmap(NULL, 1256740, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x4001e000
mprotect(0x40148000, 36132, PROT_NONE)  = 0
#now map its data section
old_mmap(0x40148000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0x12a000) = 0x40148000
#and .bss
old_mmap(0x4014d000, 15652, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x4014d000
close(3)                                = 0
#and ld's data section 
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40151000
munmap(0x40015000, 33730)               = 0
fstat64(1, {st_dev=makedev(0, 6), st_ino=2, st_mode=S_IFCHR|0620, st_nlink=1, st_uid=1000, st_gid=5, st_blksize=1024, st_blocks=0, st_rdev=makedev(136, 0), st_atime=2004/06/08-20:34:14, st_mtime=2004/06/08-20:34:14, st_ctime=2004/06/08-15:20:16}) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40015000 
write(1, "foo\n", 4foo
)                    = 4
munmap(0x40015000, 4096)                = 0
semget(IPC_PRIVATE, 4096, IPC_CREAT|0x40149140|0400) = -1 ENOSYS (Function not implemented)
_exit(0)                                = ?
 
Old 06-08-2004, 08:57 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
mmap() is being called to load shared library code, but I read the "et al" part as meaning he's only using mmap as one example of strace output.
 
Old 07-23-2004, 10:00 AM   #7
altosys
LQ Newbie
 
Registered: Jun 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks ppl,.......

I googled around a lot on the basis of all you rreplies..........
I guess....all of you helped be in building the answer i wanted.

Anyways, Im back with a nw query :-)

Inside the Kernel sourse there are a lot of macros which resemble the following :
"__KERNEL__" (double under scores) and even "_something_"
Do these have any special meanings

Also when a source says :
#ifndef _LINUX_FS_H
#define _LINUX_FS_H

what exactly is it trying to say or do?
 
Old 07-23-2004, 11:08 AM   #8
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
The

#ifndef SOME_MACRO
#define SOME_MACRO

/* stuff */

#endif

is a header guard. It's so the file can be included multiple times without causing multiple definition errors.
 
Old 07-23-2004, 12:36 PM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
To elaborate on what btmiller said,
when you write foo.h, you should always wrap it in
Code:
#ifndef _FOO_H
# define _FOO_H

....

#endif /* _FOO_H */
This causes the entire foo.h to only be read once. e.g. you could have bar.c #include foo.h and baz.h, and baz.h #includes foo.h, Then foo.h would be #included twice and you'd get compiler warnings about previous declarations of everything in foo.h.

The whole problem exists because the #include directive is very literal. The compiler sees it as if everything in the #included file is cut and pasted directly in place of the #include.

It's also common to have
Code:
#ifndef SOMETHING
# define SOMETHING blah blah
#endif
which simply defines SOMETHING if it isn't already defined; this has nothing to do with protecting the entire file from being included multiple times.
 
Old 07-24-2004, 01:51 AM   #10
altosys
LQ Newbie
 
Registered: Jun 2004
Posts: 6

Original Poster
Rep: Reputation: 0
wait wait.......

I'm still a bit confused !

So I write a file foo.h and I include
#ifndef _FOO_H
#define _FOO_H
#endif

Now, I have another file bar.c which:
#include foo.h
#define baz.h

baz.h , in turn, might :
#include foo.h

So the above #ifndef and #define pair will prevent the warnings of "previous declarations of foo.h"

Is that correct? If so, how is that actually taking place? and does the macro _FOO_H expand on its own to foo.h ?
 
Old 07-24-2004, 03:13 PM   #11
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
#defines are available to all subsequent included files.

Since baz.h is included after foo.h, the _FOO_H #define is within its scope.
 
  


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
query rgding linux gui internals karmic_nirvana Programming 2 09-21-2005 11:34 PM
see linux kernel internals kpachopoulos Linux - General 4 07-05-2005 06:15 PM
os internals shakeeb General 2 05-18-2004 06:04 AM
Linux kernel internals RussoIsraeli Linux - General 2 11-02-2003 09:11 PM
what is unix or linux internals programming? cybercop12us Programming 3 05-01-2002 07:32 AM

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

All times are GMT -5. The time now is 05:52 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