Sunteți pe pagina 1din 12

IBM XL C/C++ Compiler for AIX

IBM XL C/C++ Compilers for AIX Debugging Optimized code


By: Grigor Nikolov Level: Introductory May 2012

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 2 of 12

Contents
IBM XL C/C++ Compiler for AIX ................................................................................. 1 About this series ...................................................................................................... 3 About this Tutorial ................................................................................................... 3 Objectives .............................................................................................................. 3 Prerequisites ........................................................................................................... 3 System Requirements............................................................................................... 3 Glossary ................................................................................................................. 4 Introduction ............................................................................................................ 4 Start the Terminal Emulator to AIX System.................................................................. 5 Get Started with IBM Debug Optimized Code ............................................................... 5 Conclusion ............................................................................................................ 11 Trademarks........................................................................................................... 12 Resources ............................................................................................................. 12

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 3 of 12

Before you start


About this series
Walk through this scenario and others online as part of the IBM XL C/C++ Compiler for AIX.

About this Tutorial


This tutorial shows new debug options that can be used to debug optimized C++ code using the dbx debug program and the XL C++ compiler.

Objectives
Show the new options that are available to debug optimized code. Total time: 30 minutes

Prerequisites
Basic Basic Basic Basic Unix skills Source code compile and build experience knowledge of C programming language knowledge of dbx debug program

System Requirements
http://www.ibm.com/software/awdtools/xlcpp/aix/sysreq/

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 4 of 12

Glossary
IBM XL C/C++ Compiler: IBM XL C and C++ compilers offer advanced compiler and optimization technologies and are built on a common code base for easier porting of your applications between platforms. They comply with the latest C/C++ international standards and industry specifications and support a large array of common language features.

Introduction
There are new options available to make debugging optimized code easier, specifically for O2. When the O2 optimization level is in effect the debug capability is completely supported. When an optimization level is higher that O2 is in effect the debug capability is limited. The debug option g has levels that range from 0 to 9. -g0 No debug information is generated. No program state is preserved. -g1 Generates minimal view-only debugging information about line numbers and source files names. No program state is preserved. -g2 Generates minimal read-only debugging information about types, line numbers, variables and source files names. At O2 no program state is guaranteed. -g3, -g4 Same behavior as g2 plus function parameter values are available to the debugger at the beginning of each function. -g5, -g6, -g7 Generates minimal read-only debugging information about line numbers, variables and source files names. When the -O2 optimization level is in effect program state is available to the debugger at if constructs, loop constructs, function definitions, and function calls, and function parameter values are available to the debugger at each function. -g8 Generates read-only debugging information about line numbers, source file names, and variables. When the -O2 optimization level is in effect, program state is available to the debugger at the beginning of every executable statement, and function parameter values are available to the debugger at each function. Same behavior as g8 plus you can modify the value of the variables in the debugger.This is also the traditional level of support at noopt.

-g9

This demo will demonstrate g8 and g5 behavior.

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 5 of 12

Getting Started
Start the Terminal Emulator to AIX System
Figure 1 Get Started

Double click the Launch AIX icon on the desktop (See Figure 1) to start the character terminal to AIX system.

Get Started with IBM Debug Optimized Code


Successful login will result with the user presented with a menu of demo hosted on the server. Type 6 and press Enter to select the Debugging Optimized C/C++Code demo. Figure 2 Demo Prepared

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 6 of 12 On the terminal window you will see important information and directory path to compiler install directory (See Figure 2 Demo Prepared oval red). Note: Starting another command window will start the demonstration setup of your environment. This will result in loss of any work done in your home directory. This will impact any progress you have made on demo steps going forward. This demo does not require more than one terminal window. However, if you prefer more than one terminal window then you may open them before going forward. Terminal window is now ready for commands (See Figure 2 Demo Prepared arrow). Your home directory contains necessary source code to perform the tutorial. Type ls command to see the directory content (See Figure 3 Contents). Command: ls Figure 3 Contents

The program sample.cpp is a C++ program that has a class structure, function overloading, an if statement, a for loop and a loop invariant.

Steps:

1. To show the debug-ability of optimized code compile sample.ccp with debug level 8 and optimization level 2 (See figure 4 Compile 1) Command: xlC g8 O2 o sample sample.ccp Figure 4 Compile 1

Now with the executable program sample run dbx (See figure 5 Run dbx) Command: dbx sample

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 7 of 12 Figure 5 Run dbx

Set the first breakpoint at main, initializing a to 1 (See figure 6 Set breakpoint main) Command stop in main run Figure 6 Set breakpoint main

At this point the variable a is not yet initialized. Print the value of a and step to line 34 (See figure 7 Value of a) Command: print a Figure 7 Value of a

Now we can print the values of a, b, c and f1 (See figure 8 Print Values). Command: print a, b, c, f1

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 8 of 12 Figure 8 Print Values

The next line will be the initialization of a class member. Step to the next line and print the value of b (See figure 9 Stepping into Class X) Command: step print b Figure 9 Stepping into Class X

Continue stepping through the program. Youll see that a value gets added to the class object and we can get the value of the class object. At line 37 the value of b can be printed (See figure 10 Print b). Command: step step step step print b

Figure 10 Print b

Continue stepping through the program to see that the debugger is able to work with function overloading, loops and if statements.
Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 9 of 12

Note: stopped in unnamed block in main is a hint to the user that the add_val function is inlined. Had it not been inlined the user would have instead seen stopped in add_val. Similarly, when a function is inlined the user is capable of stepping into it. On the other hand, when the function is not inlined, the step command moves the user beyond the function call, to the next statement in main.
2. The debug option -g5 makes the program state available to the debugger at if constructs, loop constructs, function definitions, and function calls, and function parameter values are available to the debugger at the beginning of each function. This option reduces the object size and increases the compile time performance while still providing some ability to debug the program. Compile the sample.cpp source code with debug level 5 and optimization level 2 (See figure 11 Compile 2). Command: xlC g5 O2 o sample sample.cpp Figure 11 Compile 2

Figure 12 Set breakpoint at line 41

With g5, the debugger steps to loops, control blocks and function calls. In this case, the next step brings the sessions to the function call, calc-values. Figure 13 Step to the function call calc_values

Before stepping into the function, we can print the values of the parameters passed to calc_values.
Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 10 of 12

Figure 14 Print values of locals variables in calc_values

Inside the function, calc_values we can view the value of the variables used inside the function, t1 & t2. Please note that after stepping to the function call, the next step takes us to the next logic control statement, the if statement shown above inside the function if(t1<t2).

Steps:

1. As in figure 4 above, build the executable using g8 2. Open the executable in the debugger and set a breakpoint at line 39

Figure 15 setting breakpoint at line 39 and stepping though a loop

Notice how in the figure above, at g8 the debugger not only stops at the for loop, but also inside the content of the for loop at each iteration. In a case like this the user would

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 11 of 12 have had to step through the loop 100000 times in order to arrive at the print statement following the loop. With g5 this is not the case. 1. As in Figure 11 above, build sample_2 executable using g5 2. Open the executable in the debugger and set a breakpoint at line 39 Figure 16 setting breakpoint at line 39 and single stepping over a loop

Notice that at g5 we can step over the entire loop, in one simple step. Also notice the length of time it takes to arrive at the following print statement. This is because the debugger is in effect running the loop in the background before getting to the print statement that follows. In situations such as these, the user is better off setting a breakpoint after the loop, to the statement of interest, rather than stepping into or over these loops (especially when their iteration count can be large).

What you have learned


Conclusion
In this demo, you have learned the differences between the debug information displayed at each of g8 and g5 level in combination with optimization level 2.

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

IBM XL C/C++ Compilers for AIX Debugging Optimized code, Page 12 of 12

Trademarks
IBM and the IBM logo are trademarks of International Business Machines Corporation in the United States, other countries or both. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Other company, product and service names may be trademarks or service marks of others.

Resources
XL C/C++ for AIX, Library : http://www.ibm.com/software/awdtools/xlcpp/aix/library/ Community Cafe : http://www.ibm.com/software/rational/cafe/community/ccpp

Copyright 2012, IBM Corporation. Published by IBM developerWorks.

S-ar putea să vă placă și