Memory View
There’s one more debug-oriented view that shows up by default in the bottom-tabbed window of the Debug perspective. The Memory view lets you monitor and modify process memory. Memory is organized as a list of memory monitors , where each monitor represents a section of memory specified by a base address. Each memory monitor can be displayed in one or more of four predefined formats known as memory renderings . The predefined renderings are hexadecimal (default), ASCII, signed integer, and unsigned integer.
Figure 4.11 shows a memory monitor of the area allocated for temp
just after the first fscanf()
call in read_file()
. The Memory view is split into two panes, one that lists the currently active monitors and another that displays the renderings for the selected monitor. A monitor may have multiple renderings enabled, and these will be tabbed in the renderings pane.
Figure 4.11: Memory view.
The first four bytes hold a pointer to another area allocated for the name. This also happens to be visible. Remember that the x86 is a “little endian” machine. Consequently, when memory is displayed as shown here, most entries appear to be “backwards.”
Each of the panes has a small, fairly intuitive set of buttons for managing the pane. These allow you to either add or remove monitors or renderings, and in the case of monitors, remove all of them.
With this background on the principal debugging features of Eclipse, you should be able to find the two runtime errors that have been inserted in sort_utils.c
. Good luck.
Eclipse allows projects to refer to, and to depend on, other projects. Consider, for example, that you have a family of similar application projects that all make use of a common library. We can have each of the application projects refer to the library project so that any time a change is made in the library, the applications are rebuilt. The library project becomes a dependency of the applications.
To illustrate this idea, we’ll take the five functions in sort_utils.c
and turn them into a static library that we’ll link with record_sort.c
. Switch back to the C/C++ perspective, select File→New… C Projectand select Static Libraryas the project type. Call it “sort.” Click Finish. Right-click on the sort project in Project Explorer and select Import→General→File System. Browse to EclipseSamples/sort/
. Click Select Alland Finish. Five C files and one header file are imported into the project. Build the project. The result is libsort.a
.
Now go back to the record_sort project and select Propertiesfrom the Project Explorer context menu. Select Project Referencesand check sort
, which may be the only entry (Figure 4.12). Delete sort_utils.c
from the file list.
Figure 4.12: Project References.
Now expand the C/C++ Generalentry in the left-hand pane and select Paths and symbols. Click the References tab and check sort. This will expand the sort entry and you’ll see that the Active build configuration is selected. Select the Library paths tab and you’ll see that /sort/Debug has been added (assuming that Debug is the active configuration for sort).
Expand the C/C++ Build entryand select Settings. Then select GCC C Linker→Generaland check No shared libraries. We’ll be using static libraries in this example. Now select Libraries (Figure 4.13). The Library search pathlists the path to the sort project.
Figure 4.13: Library selection.
Click the Addicon in the Librariessection and enter “sort.” [3] I find it a little odd that Eclipse can’t automatically insert the sort library here, since it apparently figured out that sort is a library project.
In the context menu for record_sort in the Project Explorer, clean the project. This will rebuild it using the sort library. To prove that record_sort is now dependent on sort, clean the sort project.
This causes it to be rebuilt with a later timestamp on the library than that of the record_sort executable. Now build record_sort. It will be relinked with the new library.
Refactoring is generally described as a process of restructuring code for the purpose of readability, performance improvements, reuse, or simply for the “regular evolutionary quest for elegance.” [4] http://wiki.eclipse.org/FAQ_How_do_I_support_refactoring_for_my_own_language?www.newnespress.com
Most programmers engage in a process of refactoring without consciously realizing it. As a project progresses we often realize that the approach westarted with isn’t quite working and it’s time to do some prudent restructuring and reorganization.
Eclipse supports an automated approach to refactoring that makes sure that changes are propagated properly throughout a project. The nature of refactoring support is somewhat language-dependent. Eclipse offers extensive refactoring support for Java involving operations on classes, interfaces, and methods. For the moment, anyway, C/C++ refactoring is limited to renaming. While this may seem trivial, propagating a name change accurately throughout a large project can be burdensome if done by hand.
We’ll use the record_sort project to illustrate the refactoring support in CDT. Go back to the C/C++ perspective if you’re not already there and open record_sort.c
if it isn’t already. In line 23 select read_file
. Now from the main menu, select Refactor→Rename…. This brings up the dialog box in Figure 4.14. Let’s change the name read_file
to file_read
by changing it in the Rename to:field. The default scope is related projects. This is what we want, because we do in fact have a related project that includes this symbol name. The rename dialog is also accessible from the editor’s context menu.
Figure 4.14: Renaming dialog.
With the Rename to: field modified, the Previewbutton is now active. Click it to bring up the dialog in Figure 4.15. This shows all of the places in both projects where read_file
can be changed to file_read
and offers the option to selectively change any or all of them. Click OKand read_file
will be magically changed to file_read
throughout both projects.
Figure 4.15: Rename change dialog.
Clearly, this example is trivial, but in a large project involving hundreds, perhaps thousands of files, the ability to automatically rename a symbol throughout the project can be quite powerful.
Читать дальше