The present invention generally relates to techniques and tools for analysis of computer programs, and more particularly to instrumentation of a computer program during program execution.
Analysis of binary executable programs is performed to analyze program performance, verify correctness, and test correct runtime operation, for example. Some analyses are performed prior to runtime (static analysis), while other analyses are performed during runtime (dynamic analysis). For both static and dynamic analysis, however, the analysis is often performed at the function level.
The term, “function”, refers to named sections of code that are callable in the source program and encompasses routines, procedures, methods and other similar constructs known to those skilled in the art. The functions in the source code are compiled into segments of executable code. For convenience, the segments of executable code that correspond to the functions in the source code are also referred to as “functions”.
A function is a set of instructions beginning at an entry point and ending at an endpoint. The entry point is the address at which execution of the function begins as the target of a branch instruction. The endpoint is the instruction of the function from which control is returned to the point in the program at which the function was initiated. For functions having multiple entry points and/or multiple endpoints, the first entry point and the last endpoint define a function.
One category of analysis performed on executable programs is “instrumentation”. Instrumentation is generally used to gather runtime characteristics of a program. For example, the number times that a function is executed while the application is executing is determined through instrumentation. While the information gathered through instrumentation may be extremely useful for purposes of enhancing program performance, the process of setting up a program for instrumentation can be time-consuming.
Present instrumentation techniques generally involve compilation and linking of the application program along with instrumentation code. For small applications, this may be a straightforward process. However, for large applications, which may encompass hundreds or thousands of modules, the compilation and linking process may be complicated and difficult to modify to enable or disable instrumentation given the amount of time required. Another factor that contributes to the inefficiency of instrumentation of large applications is that oftentimes instrumentation is desired for only a small number of all the functions in the application. Thus, a great deal of time is spent rebuilding the entire application for instrumentation when only small portion of the application is to be instrumented.
A system and method that address the aforementioned problems, as well as other related problems, are therefore desirable.
The present invention provides a method and apparatus for dynamic instrumentation of an executable application program. The application program includes a plurality of functions, each function having an entry point and an endpoint. When the application is executed, a shared memory segment is created for an instrumentation program and the application program. Upon initial invocation of the original functions in the application program, corresponding substitute functions are created in the shared memory segment, the substitute versions including instrumentation code. Thereafter, the substitute functions are executed in lieu of the original functions in the application program.
It will be appreciated that various other embodiments are set forth in the Detailed Description and Claims which follow.
Various aspects and advantages of the invention will become apparent upon review of the following detailed description and upon reference to the drawings in which:
Dynamic instrumentation refers obtaining instrumentation data for an executable program (also, “executable application” or “application”) while the program is executing without any pre-processing, for example, recompilation or relinking, of the application prior to execution. Thus, the same executable program that is used in a production environment is executed and instrumented. The present invention, as illustrated by the various embodiments described herein, performs dynamic instrumentation of an executable application. The dynamic instrumentation is performed by creating instrumented versions of functions when the functions are invoked, and thereafter executing the instrumented functions instead of the original functions.
At step 102, an instrumentation process attaches to a target executable application and obtains control. Those skilled in the art will appreciate that this step is accomplished using known, conventional techniques. At step 104, the process allocates and maps shared memory for use by the instrumentation process and the executable application. The process of allocating and mapping the shared memory is described further in FIG. 2A.
At step 106, optional run-time libraries are added for dynamic instrumentation.
These run-time libraries include, for example, code to dynamically increment the number of counters for indirect branch targets and code to perform a system call to register an instrumented function to the dynamic loader.
At step 108, entry points of the functions in the executable application are located. In addition to those methods that are known in the art, various other techniques for finding function entry points are described in the patent/application entitled, “ANALYSIS OF EXECUTABLE PROGRAM CODE USING COMPILER-GENERATED FUNCTION ENTRY POINTS AND ENDPOINTS WITH OTHER SOURCES OF FUNCTION ENTRY POINTS AND ENDPOINTS”, to Hundt et al., filed concurrent herewith, having patent/application Ser. No. 09/833,299, the contents of which are incorporated herein by reference.
Each of the function entry points is patched with a breakpoint at step 110. The instructions at the function entry points are saved in a table so that they can be restored at the appropriate time. At step 112, control is returned to the executable application.
When a breakpoint is encountered in the executable application, control is returned to the instrumentation process, and decision step 114 directs the process to step 118. Step 118 analyzes the executable, finds the function entry point for the break that was hit, determines the length of the function, and analyzes the function to identify target addresses of branch instructions (“branch targets”). For newly identified branch target(s), the process is directed to step 122, where the branch target(s) is added to the list of function entry points, and the instruction at the branch target is patched with a break-point. The instruction at the branch target is first saved, however, for subsequent restoration. The process is then directed to step 124.
At step 124, a new instrumented function is generated and stored in the shared memory. The function of the executable application from which the new instrumented function is generated is that from which control was returned to the instrumentation process via the breakpoint (decision point 114). In generating the new instrumented function, the saved entry point instruction is restored as the first instruction of the new instrumented function in the shared memory. At step 126, the entry point instruction in the executable application is replaced with a long branch instruction having as a target the new instrumented function in the share memory. The instrumentation process then continues at step 112 where control is returned to the executable application to execute the new instrumented function.
Returning now to decision point 120, if the branch target(s) identified at step 118 has already been instrumented, the branch target is replaced with the address in shared memory of the instrumented function. If the branch instruction is subsequently executed, control will jump to the instrumented function. The instrumentation process then continues at step 124 as described above.
For branch targets that have already been identified as functions, the process continues from decision point 120 directly to step 124.
Returning now to decision point 114, when the end of the executable application is reached, control is returned to the instrumentation process, and the instrumentation process continues at step 130. Selected instrumentation data that were gathered in executing the application are output at step 130 to complete the instrumentation process.
Initially, the executable instrumentation program 302 (
At step 206, the process selects a segment of code within the executable application and saves a copy of the segment 310 in instrumentation memory 304. In addition, the states of registers of the application are saved in instrumentation memory segment 304.
At step 208, the selected segment of code in the application is overwritten with code segment 312 (“injected code”), which includes instructions to allocate and map shared memory (FIG. 2C). At step 210, the registers are initialized for use by the selected thread, and the beginning address of the code segment 312 is stored in the program counter. At step 212, the execution of the thread is resumed at the code segment 312.
In executing code segment 312, system calls are executed (step 214) to allocate the shared memory segment 314 and map the shared memory segment for use by the executable instrumentation program 302 and the executable application 306. A breakpoint at the end of the injected code 312 signals (step 216) the executable instrumentation program 302 that execution of the injected code is complete.
A step 218, the executable instrumentation program 302 restores the saved copy of code 310 to the executable application 302 (
The new executable application 402′ (
In order to execute the instrumented function 404′, the instruction at the entry point of function 404 is replaced with a long branch instruction 410 having as a target address the entry point 412 of instrumented function 404′. In addition, the target addresses of branch instructions elsewhere in the application 402′ that target function 404 are changed to reference instrumented function 404′.
The control flow begins with the dynamic instrumentation code injecting code (508) into the executable application 504 (e.g., FIG. 2C). Control is transferred (510) from the dynamic instrumentation code to the executable application code to execute the injected code. The executable application 504 allocates and maps (512) shared memory for use by the dynamic instrumentation and the executable application. Control returns (514) to the dynamic instrumentation code, which then identifies functions in the executable application 504 and inserts breakpoints (516).
Control is then transferred (518) to the executable application, which executes the application code (520) until a breakpoint is reached. The breakpoint indicates the beginning of a function. The breakpoint transfers control (522) back the dynamic instrumentation code, and the dynamic instrumentation code creates an instrumented version of the function (
The code of the instrumented function along with the probe code (
Other aspects and embodiments of the present invention, in addition to those described above, will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. It is intended that the specification and illustrated embodiments be considered as examples only, with a true scope and spirit of the invention being indicated by the following claims.
Number | Name | Date | Kind |
---|---|---|---|
4802165 | Ream | Jan 1989 | A |
5142679 | Owaki et al. | Aug 1992 | A |
5161216 | Reps et al. | Nov 1992 | A |
5175856 | Van Dyke et al. | Dec 1992 | A |
5212794 | Pettis et al. | May 1993 | A |
5307498 | Eisen et al. | Apr 1994 | A |
5313616 | Cline et al. | May 1994 | A |
5450586 | Kuzara et al. | Sep 1995 | A |
5481713 | Wetmore et al. | Jan 1996 | A |
5491808 | Geist, Jr. | Feb 1996 | A |
5577244 | Killebrew et al. | Nov 1996 | A |
5613118 | Heisch et al. | Mar 1997 | A |
5619698 | Lillich et al. | Apr 1997 | A |
5659752 | Heisch et al. | Aug 1997 | A |
5689712 | Heisch | Nov 1997 | A |
5752038 | Blake et al. | May 1998 | A |
5774724 | Heisch | Jun 1998 | A |
5784275 | Sojoodi et al. | Jul 1998 | A |
5838976 | Summers | Nov 1998 | A |
5857105 | Ayers et al. | Jan 1999 | A |
5889999 | Breternitz et al. | Mar 1999 | A |
5903758 | Walker | May 1999 | A |
5909578 | Buzbee | Jun 1999 | A |
5940618 | Blandy et al. | Aug 1999 | A |
6006033 | Heisch | Dec 1999 | A |
6088525 | Peri | Jul 2000 | A |
6113652 | Lysik et al. | Sep 2000 | A |
6149318 | Chase et al. | Nov 2000 | A |
6158049 | Goodwin et al. | Dec 2000 | A |
6161218 | Taylor | Dec 2000 | A |
6170083 | Adl-Tabatabai | Jan 2001 | B1 |
6175956 | Hicks et al. | Jan 2001 | B1 |
6189141 | Benitez et al. | Feb 2001 | B1 |
6202205 | Saboff et al. | Mar 2001 | B1 |
6216237 | Klemm et al. | Apr 2001 | B1 |
6230316 | Nachenberg | May 2001 | B1 |
6253373 | Peri | Jun 2001 | B1 |
6275981 | Buzbee et al. | Aug 2001 | B1 |
6282707 | Isozaki | Aug 2001 | B1 |
6295644 | Hsu et al. | Sep 2001 | B1 |
6298481 | Kosaka et al. | Oct 2001 | B1 |
6305010 | Agarwal | Oct 2001 | B2 |
6308326 | Murphy et al. | Oct 2001 | B1 |
6327699 | Larus et al. | Dec 2001 | B1 |
6327704 | Mattson et al. | Dec 2001 | B1 |
6397382 | Dawson | May 2002 | B1 |
6397385 | Kravitz | May 2002 | B1 |
6401240 | Summers | Jun 2002 | B1 |
6470493 | Smith et al. | Oct 2002 | B1 |
6662359 | Berry et al. | Dec 2003 | B1 |
6681387 | Hwu et al. | Jan 2004 | B1 |
Number | Date | Country | |
---|---|---|---|
20020152455 A1 | Oct 2002 | US |