u/linux 系统编程问答

来源:互联网 发布:sqlserver存储过程list 编辑:程序博客网 时间:2024/06/02 01:30

 *如何统计一个进程进行的系统调用? (转自水木)

1) gcc中有关于profile的选项,一般好像用-pg
2)strace -c 产生类似下面的统计信息
strace -c -p 14653 (Ctrl-C)

 

* 必须学会的gcc 选项

The common syntax is gcc [option] [filename]. All options listed below are c
ase sensitive. The option -v differs completely from -V . Also options may b
e put together in pairs. So, -vc is different from -v -c.

1) Extensions do matter: The extension given to a file determines its type a
nd the way it should be compiled.
.c => c source file - preprocess, combine and assemble.
.m => objective-c source file - preprocess, combine and assemble.
.i => pre-processed C file - compile and assemble.

2) -x language [c, c++, objective-c etc.] - Compiles the file with respect t
o the given option. A file with '.m' extension can be compiled as a '.c' ext
ension file using the command "gcc -x c filename.m". If no language is speci
fied the file is compiled depending upon the extension.

3) -c : Compiles without linking. All successfully compiled binaries are sav
ed as "filename.o".

4) -S : Stop after compilation. Does not generate the binary. Just to check
whether the code is correct.

5) -o filename: The compiled binary is saved with specified filename. If not
 specified, the default is the save the binary as a file named a.out in the
directory.

6) -v : Provides the release number of your compiler. Useful if one is getti
ng an unexplainable error. The compiler may be a old version.

7) -pipe : For a big program involving a lot of inter linked files and funct
ions, temporary files tend to get created while compilation. When this optio
n is specified, the compiler makes use of pipes rather than creating tempora
ry files. The output of each function is piped to the next. The swap space i
s used instead.

8) -ansi : Turns off features of gnu C which are incompatible with ANSI C. T
his does not put a complete ban on non-ANSI programs. Certain Turbo C featur
es will work. A "-pedantic" option is required to force strict conformance t
o the ANSI standard.

9) -ffreestanding : Compiles in a freestanding environment. This makes use o
f the '-fno-builtin' option along with main having no special requirement.

10) -funsigned-char: Declares character S as unsigned. Since character is ma
chine specific, this serves to avoid confusion and common program errors whe
n run on various platforms. For example, suppose a Windows compiler takes a
defined character [syntax : char a;] as signed, while the Linux takes it as
unsigned. Now, suppose you are having a "if -else" statement -- if (a==0) {
/*statements }. Different compilers have different defaults for the signedne
ss of char. Specifying whether a char should be signed or unsigned, removes
ambiguity. For example, on the Windows compiler, this statement will be exec
uted considering character as signed while on Linux they are executed consid
ering character as unsigned. This shall result in dubious results because Wi
ndows will interpret the object differently. "-fsigned-char" declares a char
acter as signed.


Pre-processor options

1.) -E : Do only preprocessing, no compiling or assembling. Output results t
o standard output file a.out. Some of the options like -imacros work in tand
em with -E and require -E to be present along with it.

2.) -include file : Compiles the file first. The options such as -D, -U are
compiled first, regardless of the order in which they are listed. The -inclu
de and -imacros get compiled in accordance to the way they appear at the com
mand line.

3.) -imacros : Compiles macros irrespective of the output. The only aim here
 is to compile the macro and make it available to the program.

4.) -C : Do not discard comments (used with -E option)

5.) -P : Do not generate "#line" commands (used with -E option)

6.) -M : Describes the dependencies of each object file.

Linker options

1.)-laddition : Use library bearing the name addition. Searches a list of st

andard directories for the library named addition. A "-L /place/where/librar
y/exists" searches the directory specified.


Directory options

1.) -Idirectory : Append a list of directories to the standard directory lis
t.

2.) -I- : Specifying -I after -I- searches for #include directories. If -I-
is specified before, it searches for #include "file" and not #include .


Warning messages

1.) -w : Inhibit all warning messages.

2.) -W : Print extra warning messages. For example, a function not returning
 a value, a defined variable not being used. The warning messages issued ove
r here do not imply that the program has not compiled successfully. It just
helps to make a perfect program. Code can be cleaned up as you will be notif
ied of unused variables.

3.) -Wswitch : Warn when 'switch' command is used. There are more options he
re. The man page is the best resource for these additional switches.


Debugging options (Kill those bugs)

1.) -g : Produce debugging information in the native O.S. format. A line of
junk characters got appended to my a.out file when I tried this option. Mayb
e, the option was not meant for a novice like me but for a more advanced pro
grammer. -gstab gives debugging information in stab format.

2.) -gdwarflevel : Request debugging information along with level. Default l
evel is set to 2.
Level 1 : Minimal amount of code to backtrack.
Level 3 : Maximum amount of code to backtrack.

3.) -save-temps : Store the temporary generated files, permanently in the cu
rrent directory. The files are named on basis of the source file.

4.) -O (0 or 1 or 2 or 3)
0: Do not optimize.
1: Optimize. Requires a little more time and a lot of memory.
2 and 3 : Memory hogs but essential when dealing with large functions.

 

 

*linux下的汇编语言设计(BBS)

http://www.newsmth.net/bbscon.php?bid=69&id=1208

 

 *linux 下的多播编程

http://www.newsmth.net/bbscon.php?bid=69&id=1218

 

 

*在linux下如何将函数编译成动态连接库?

3. vi our_printf.c
int printf ( const char * format, ... )
{
    puts( "hello world." );
    return 0;
}  /* end of printf */

[scz@ /home/scz/src]> gcc -c -fPIC our_printf.c -O3 -o our_printf.o
[scz@ /home/scz/src]> gcc -shared -Wl,-soname,our_printf.so -O3 -o our_printf.so our_printf.o
[scz@ /home/scz/src]> export LD_PRELOAD=/home/scz/src/our_printf.so
[scz@ /home/scz/src]> ./printftest
hello world.
[scz@ /home/scz/src]>

 

 

*linux 下如何查看某个命令调用了哪些系统调用

strace du

 

* GDB里怎么打印出宏的数据? (转载)
编译加-gdwarf-2 -g3(gcc3.1以上版本支持)
还得gdb支持

 

*请问用什么命令可以查看一个so的接口函数呀?

readelf -s

原创粉丝点击