Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


Linux Programming (2) GCC and GDB

Says : good tools to do a good first sharpen his tools. In Linux, not Windows so convenient What are configured in a VC. Generally, the traditional ways are to write makefile, using terminals in order IMG and debug. Is the most frequently used GCC compiler, GDB is the compiler. GCC and GDB GNU previous G represents. Detailed information on the GNU, no nonsense here, and are interested in themselves http://www.gnu.org/. In fact, the use of GCC and GDB have a lot online, I found this information is taken from the Internet to sort out, not the front, but is the most commonly used functions. Meanwhile, the most commonly used only against me, all my personal preferences to prevail, and after all this is a series of essays.

1, the use of GCC
The GCC is very simple to use, in general 装linux security systems were installed have the time. GCC manual checking, import terminals
Man gcc
On the list. Withdraw from manual input q. GCC also supports C + + version, called Linux.
First, we look at a compiler directives :
Gcc common.c epoll.c epollserver.c -o epollserver -lpthread
If the procedure is not wrong, after the completion of the translation will not mandate the screen output will immediately return to order under which suggest that this time the importation
. /epllserver
On the implementation procedures. Translation : the order to explain this here in front of the three. C documents that the three documents translated, -o document can be specified goals, and our goal here is epollserver Document. And the last link to linuxthread Secretary -lpthread said. If the procedures are analyzed using a thread, it is necessary to add this option. Under normal circumstances, basically enough to the top of the order. Now tell us about other options :
(1) E
E pretreatment only compiler directives, such as documents are ordered pre test.c, tabulated the results and the reality on the standard output :
Gcc-E test.c
(2) - S
S instruction compiler generated code compilation, translation and then stop. Following orders from the C language source code compilation of documents created test.s
Gcc-S test.c
(3) g
G mean generating executable files with the original code debugging symbols. I wait for the next time will be used in the tests on GDB.

Next, how to create static reservoir.
Some static reservoir. O Set document. Static archive library another name, often called a tool to manage the archive ar.
If we create a static reservoir, the first goal of the compiler library modules.
Gcc-e test1.c test2.c
Test1.o test2.o two goals then generated documents. Next, build arsenals and target document to join.
Ar-r libtest.a test1.o test2.o
R reservoir to be built up, if there is no library named as the target module, they will be joined target module (module can replace the original target).
Has now been completed, thus the need to use these two functions together with the need to store and the original code compiler, for example, we now call the test3.c Lane libtest.a Secretary of the function, so the compiler :
Gcc test3.c libtest.a -o test3

Static Library named 1986-1991 Judge habit is beginning to. A end. L Bank in order to allow the use of the reservoir using the initials. See the following order :
Gcc test3.c -ltest -o test3
This effect is the same name above. The only difference is the different library search path. L reservoir can specify the path, but the compiler can be directed to the library to find out the system.

Dynamic Shared reservoir creation
The first step is to create dynamic shared library objects in the database and editing module.
Gcc-c -fpic test1.c test2.c
-fpic Here is the output of the module can be targeted under the Heavily produced by means of URL. Abbreviations pic representative position independent code (position independent code). Sharing the goal is to document the reservoir pool, but these objectives document is in accordance with the special way by the compiler generated, each object module addresses (including variable and function invoked Call) addresses are relative, not absolute addresses, allowing the operating procedures, and implementation of dynamic load sharing library). DLL general. So were for expansion.
The formation of two top order. O document, then use the following order Sharing Secretary testlib.so
Gcc -shared test1.o test2.o testlib.so
When it comes to sharing the Secretary translated into a certain procedure with the following order :
Gcc test3.c testlib.so -o test3


GCC to introduce the basic methods used here as a more complicated, a good man to go, for me anyway now that the above is enough of these.

2, the use of GDB
GDB is not detailed description of nonsense, since the room knows this is a debugger.
The first translation of a message with debugging process
Gcc-g os.c -o os
Now with debugging information generated by the executable files is os.
Next import terminal in gdb, a lot will pop version of the copyright information and information. Finally, there is a
(Gdb)
This is GDB orders suggest, at the back of which can be imported directly order is from the radio.
Bother typing the following order, from the Internet to find a complicated system :
First of all, we can set up the screen size LINUX. Dialing :
(Gdb) set width 70
70 as the standard screen is shown.
Let us then set up breakpoint. Set up is simple : break the back or bank in a simple fight b name or function
For instance, we can set up a breakpoint in the main function :
(Gdb) break main
Or
(Gdb) b main
Remind os.c : Breakpoint 1 at 0x8049552 : file, line 455.
Then we can operation of this procedure, when the main function of the operating procedure when the procedure will stop and return to the LINUX
Under its suggested. The operation is run orders or r (gdb has many alias, you can read the transcript of help, help under gdb)
Behind can run with the parameters specified procedures for the command line parameters.
For example, r abcd, the procedure would abcd as a parameter. (I should say here can be used to set args designated Senate
Nos). R or run into, the operational procedures to stop until the entrance into the main show :
Starting program : the "path" ;/os

Breakpoint 1, main () at os.c:455
Initial 455 ();
Initial here 455 (); The order is to be implemented or function.
LINUX provides two ways : 1. Single-step into, step inside, take into function is tracking. The order's step or
2. Single-step, next, is a simple one-step, they will not enter the function. Is the next order or n
It also ordered two other usage later date.
We n order dialing :
(Gdb) n
Success forking process# 1, is 31,474 pid

Success forking process# 2, pid is 31,475

Success forking process# 3, pid is 31,476

Success forking process# 4, pid is 31,477

Success forking process# 5, pid is 31,478

Success forking process# 6, pid is 31,479

FIFO Dispatching Algorithm :
********************************************************************************

State PCB# PID Priority PC
1 31474 24 0 WAITING
2 31475 19 0 WAITING
3 31476 16 0 WAITING
4 31477 23 0 WAITING
5 31478 22 0 WAITING
6 31479 20 0 WAITING

******************************************************************************

NO process running CPU :
No IO process :
Waiting CPU!!! 31474 31475 31476 31477 31478 31479
Waiting IO NONE
456 State=WAITING;
The final sentence is to implement his orders. We now add another function breakpoint. We note
L/list orders can be used to show the source code. Here we dialing
(Gdb) l
451 main ()
452 {
453 int message;
454
Initial 455 ();
456 State=WAITING;
457 printf ( "Use Control-C to halt \n");
458 signal (SIGALRM, AlarmMessage);
459 signal (SIGINT, InteruptMessage);
460 signal (SIGUSR2, IoMessage);
(Gdb) l
461 alarm (TimeSlot);
462 for (; to)
463 {
464 message=GetMessage ();
465 switch (message)
466 {
467 case INTERRUPT : printf ( "Use Control-Ct;

468 break;
WaitingIo case CHILD_IO : 469 ();
470 break;
Suggests that the code now in AlarmMessage add breakpoint.
(Gdb) b AlarmMessage
Breakpoint 2 at 0x8048ee3 os.c : file, line 259.
(Gdb)
We then continue operating procedures.
(Gdb) c
C or continue order to allow us to continue the process was interrupted. Show :
Continuing.
Use Control-C to halt

Breakpoint 2, AlarmMessage () at os.c:259
259 ClearSignal ();
We pay attention to the following sentence phrases is ClearSignal ();
We see it with s/step tracking into the function were doing.
(Gdb) s
ClearSignal () at os.c:227
227 signal (SIGINT, SIG_IGN);
L original code with the order listed :
(Gdb) l
222 }
223
224
225 void ClearSignal () LEAVES OF 13 SPECIES OF LAURACEAE Clear other signals */
226 {
227 signal (SIGINT, SIG_IGN);
228 signal (SIGALRM, SIG_IGN);
229 signal (SIGUSR2, SIG_IGN);
230 }
231
(Gdb)
We can continue to follow orders's. Let us now try bt backtrace or orders. This order can be
Stack the show.
(Gdb) bt
Gallery ClearSignal () at os.c:227
0x8048ee8 in AlarmMessage # 1 () at os.c:259
0xbffffaec in # 2 - ()
# 3 0x80486ae in ___crt_dummy__ ()
(Gdb)
They must be able to read the meaning of show. Stack is AlarmMessage, followed by the function name -- is not
No source code symbols. This shows that the transfer function of the nest.
Well, we have not inspected the track for a long time the value of this variable. Check the value of the order is obtained or print p
P format is the "expression";
Let us come to a variable to see 444,444. :-)
(Gdb) l 1
L still remember the role? L list shows the original code or symbols, or list-plus "routine"; On the show from the "routine"; Started
The original code. Let us find a better look at the contents WaitingQueue
(Gdb) p WaitingQueue
$1 = {1, 2, 3, 4, 5, 6, 0}
(Gdb)
WaitingQueue is an array, gdb also support structure,
(Gdb) p Pcb
{{Pid $ 2 = = 0, State = 0, Prior = 0, pc = u, {Pid = 31474, State = 2
Prior = 24 pc = u, {Pid = 31475, State = 2, Prior = 19 pc =), (
Pid = 31476, State = 2, Prior = 16 pc = u, {Pid = 31477, State = 2
Prior = 23 pc = u, {Pid = 31478, State = 2, Prior = 22 pc =), (
Pid = 31479, State = 2, Prior = 20 pc = 0}}
(Gdb)
Here we can see the control procedures for the original.
There is a debugging process of the original document, but I think here I have already commonly used functions on the LINUX again, basically
Debugging procedure can be used. :-)

Note : This article quoted from most of the information
1:<GCC technical reference bible "Tsinghua University Publishing House
2 : System_Killer letter : (a bad thing trance) written name : Linux[4590]
Abstract : GDB letters (1)
The original point : China University of Science and sent most popular BBSs (Fri, 27 Mar 1998 02:31:49)




The author Blog:http://blog.csdn.net/elssann/



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com