Abstract:
A method is disclosed of generating program analysis data for analysing the operation of a computer program. The method includes running a first instrumented version of machine code representing the program, wherein said running defines a reference execution of said program, capturing a log of non-deterministic events during reference execution to reproduce states of a processor and memory during the re-running, generating a second instrumented version of machine code to replay execution of said machine code representing the program and to capture and store program state information, wherein said program state information comprises one or both of one or more values of registers of said processor and one or more values of memory locations used by said program, running said instrumented machine code whilst reproducing said non-deterministic events during said running to reproduce said reference execution; and capturing said program state information whilst reproducing said reference execution.

Description:
FIELD OF THE INVENTION 
       [0001]    This invention relates to methods, apparatus and computer program code to facilitate the debugging of computer programs. 
       BACKGROUND TO THE INVENTION 
       [0002]    In WO2007/045920, hereby incorporated by reference in its entirety, we described techniques which allow a program, more particularly the machine code of a program, to effectively be run backwards. This is helpful as it allows an error to be traced backwards from, say, the moment it caused the program to crash until the error first appeared. As the skilled person will appreciate, crashing can take many forms, generally summarised as the program not running as intended—for example a segmentation fault, and unhandled exception, or an infinite loop. 
         [0003]    Whilst backwards execution of a program is invaluable in debugging, it would be useful to have additional tools to make the debugging process easier and faster. There is a particular problem in locating and dealing with bugs which are not readily reproducible—for example a user may occasionally report a program as slow but identifying and correcting the cause of the problem can be difficult if the fault is intermittent. 
       SUMMARY OF THE INVENTION 
       [0004]    According to the present invention there is therefore provided a method of generating program analysis data for analysing the operation of a computer program, the method comprising: running a first instrumented version of machine code representing the program, wherein said running defines a reference execution of said program; capturing a log of non-deterministic events during said reference execution such that the machine code can be re-run in a deterministic manner to reproduce states of a processor and memory during the re-running; generating a second instrumented version of said machine code comprising instrumented machine code to replay execution of said machine code representing the program and to capture and store program state information during said replayed execution, wherein said program state information comprises one or both of one or more values of registers of said processor and one or more values of memory locations used by said program; running said instrumented machine code whilst reproducing said non-deterministic events during said running to reproduce said reference execution; and capturing said program state information whilst reproducing said reference execution to generate said program analysis data. 
         [0005]    In embodiments a first instrumented version of the machine code is generated in order to capture the log of non-deterministic events, and then a second instrumented version of the machine code generates additional/different instrumentation on replay for further analysis. Thus the first instrumented version of the machine code may be a relatively lightweight version of the code, since no data for analysis need be captured at this stage (in principle the two instrumented versions of the code could be the same, but this would be undesirable). 
         [0006]    In embodiments the technique allows the execution in which the bug appears to be captured and then replayed, as many times as desired, in an essentially identical manner so far as the actions of the program are concerned. This facilitates offline analysis to identify the source of an error. More particularly by logging and replaying non-deterministic events the replayed execution can be made essentially deterministic, in effect providing another method for the user to go back in time over the program execution. Thus embodiments of the invention avoid the need to instrument the program to capture extensive details of its operation during execution of the program, which is slow and can involve the programmer guessing in advance where the fault might lie. In some preferred embodiments, as well as values of registers and memory locations the instrumented machine code may generate program state information comprises information identifying which registers and/or memory locations were read and/or written, in particular by which instructions and thus by which line(s) of source code. 
         [0007]    In some preferred embodiments implementation of the method also includes performing (or providing the ability to perform) a reverse search on the program analysis data. Thus preferred embodiments provide the ability to effectively search backwards in time from a debug point in the program, to identify a defined condition in the program analysis data. The debug point from which the reverse search is commenced is typically a count of the number of machine code instructions that have been executed to this point, or an approximation thereof. The defined condition is typically a defined event—this may be defined by a condition of a register and/or memory location which may include, for example, a program counter value (which can map to a defined line of source code). 
         [0008]    In preferred embodiments the reverse search is performed by running the instrumented machine code forwards one or more times to reproduce the reference execution and, in particular, to identify a most recent time at which the defined condition is met prior to the debug point. For example a particular register value may be modified several times and the reverse search then identifies the most recent time at which, say, the value was modified. In the case of a loop the reverse search may identify the last (most recent before the debug point) occasion on which the loop was performed. 
         [0009]    In some preferred implementations the method involves capturing a succession of snapshots of the program state at successive times during running of the reference execution of the program, or during deterministic replay of the reference execution. Since embodiments of the method provide the ability for deterministic replay such snapshots need not be created at the same time as the initial, reference execution is captured. In embodiments of the techniques we describe a snapshot may comprise a complete or substantially complete copy of memory and registers used by the program, or a ‘delta’ (record of changes) since a previous snapshot. 
         [0010]    In embodiments of the method the most recent time at which a condition is met is identified by an instruction count, that is by a count of executed instructions. In a refinement the time is also determined by a count of the number of (non-deterministic) events executed up to that time—thus in embodiments “time” may be defined by a combination of the number of executed instructions and a number of events in the log of non-deterministic events. Counting the number of events can be advantageous as it is possible for two different events to occur at the time of a single instruction, in particular where an interrupt stalls an instruction. 
         [0011]    Thus in embodiments the instrumented machine code is instrumented to permit deterministic replay of non-deterministic events, and further instrumented for post hoc execution analysis, for example to perform a reverse search and/or to capture program analysis data as described below. 
         [0012]    In embodiments, when the reference execution is replayed in a deterministic manner one advantageous category of data to collect is data relating to ‘functions’ (of whatever type) defined in the source code. Thus in embodiments data identifying a calling function and/or a called function is collected, for example a memory address of the respective function(s) which may then be mapped to a name or identifier of the relevant function(s) in the source code. Some preferred embodiments of the method build a function-call graph from such data, that is a tree or graph whose nodes represent functions, edges connecting the nodes defining which function called which (since the instrumented code is able to determine the calling function and target function). Optionally such edges may be weighted by the number of calls, for example incrementing the weight by +1 for each call made. The use of a deterministically replayed execution of a reference execution allows more data to be obtained from such an approach than, say, by mere static analysis of the code. 
         [0013]    Additionally or alternatively program state information captured by the instrumented replayed execution may include heap memory control information. In some preferred embodiments this control information may comprise a tally of what memory is allocated and freed, in particular by what line(s) of source code. Again, whilst this information may be collected in other ways these generally involve having to guess at a likely problem, then instrument the executable code, then run the executable code to determine whether or not the guess was correct. This approach is slow and, more importantly, fails to capture one-off problems caused by particular, often unusual combinations of circumstances such as a particular user input, network value or the like. By contrast embodiments of the method we describe are able to use the captured reference execution with its associated log of non-deterministic events to guarantee that an error in the captured reference execution is repeatable, and hence analysable to resolve the problem. 
         [0014]    One particularly useful function is the ability to ask the question ‘where did this [register] value come from?’. This information can be provided by capturing and storing program state information identifying a write to a processor register (using the instrumented machine code), and then using this information to identify a most recent write to the register prior to a debug point in the program (machine code). In preferred embodiments the automatic identifying comprises sub-dividing the machine code into blocks of instructions where in each block if one instruction (a first instruction) executes then (all) subsequent instructions in the block also execute. The instrumented machine code is then used to generate register write data for each executed block of instructions, this data identifying one or more registers written by the blocks of instructions. Then a most recent write to the register may be identified by searching blockwise through the register write data to determine the most recent instruction modifying the register. This procedure may be implemented as part of the reverse search previously described. The skilled person will appreciate that watchpoints which trigger an exception when a particular memory address is changed may be implemented by the processor itself. 
         [0015]    Although it is helpful to identify when (at which instruction) a processor register is most recently changed, it is particularly desirable to be able to trace back the modification to the register, potentially to the origin of the data. The ultimate source of the data may be, for example, an input to a program, data from a file, data from a user input, data defined by an instruction, and so forth. Although the techniques we have described above in principle enable data flow to be traced back manually, this is preferably automated. In broad terms, this can be achieved by re-running the deterministic reference execution multiple times, tracing back one step each time, an approach which is facilitated by the use of ‘snapshots’ as described earlier. 
         [0016]    In some cases the modification to a register has a single source, but in others there may be multiple different sources, potentially with some shared history. An attempt to track changes in register values running forwards in time would, in effect, construct a tree showing modifications of the all the registers and memory locations, which is impracticable. However embodiments of the techniques we describe, in particular the technique of performing a reverse search, in effect trace back from a leaf node towards the trunk rather than attempting to build the complete tree, and therefore become practicable to implement. It is relatively straightforward to deterministically replay the reference execution to, in effect, step backwards in time asking the question “where did the value modifying the register come from?”, and then “where did that value come from?” and so forth. It will be appreciated that where a data structure is constructed with such a chain then this may also be used to identify where, for example, a value is modified in two different ways which are then recombined to modify a further register value. 
         [0017]    Thus embodiments of the techniques we describe facilitate determining where register (and other) values come from. This can be extremely difficult using other methods. 
         [0018]    One application of such techniques is in analysing the security of a program, in particular by identifying the origin of a data value and whether or not the data is subject to any intermediate modification or checks/validation. This can be used, for example, to identify whether a value such as a memory allocation value or buffer size is subject to security validation—embodiments of the techniques we describe make it relatively straightforward to perform this check. This is useful because a large percentage of viruses make use of some form of buffer overrun including, for example, the Heartbleed security bug in the OpenSSL cryptography library. 
         [0019]    We have described techniques for identifying writes to a processor register. In embodiments the method includes a procedure for looking forwards to identify subsequent (next) register and/or memory changes dependent upon an identified read from memory or a processor register. 
         [0020]    The skilled person will appreciate that the stored program analysis data may be processed in any convenient manner. For example it may be output in raw or graphical form for user evaluation. Thus in embodiments of the above described method a user interface is provided, for interacting with the program analysis method/system. Optionally an interface may also be provided to facilitate selective capturing of the log of non-deterministic events in response to a detected condition such as an error or bug in the program. For example a circular buffer of the non-deterministic events may be maintained and saved to a file on a non-transient medium when a program/bug/error/fault is detected. 
         [0021]    In a related aspect the invention provides a non-transitory carrier medium carrying a data structure for use in a method as described above, the data structure comprising: at least a portion of said machine code representing the program; a log of non-deterministic events for at least a portion of said program having a bug; and at least one of: program starting state data comprising data defining a complete or substantially complete copy of memory and registers used by the program at a starting state of said portion of said machine code representing said program, and reference state data, wherein said reference state data comprises data defining content read from pages of memory accessed during a reference execution of at least said portion of said machine code, wherein said content comprises content read from the page the first time that the page is accessed during said reference execution. 
         [0022]    Embodiments of such a data structure provide information for remote post-hoc analysis of the code. In one approach a snapshot (as defined elsewhere herein) is taken to define a starting state for the machine code; this may but need not be an initial start point—that is the program/machine code may be started at some intermediate point. The event log contains the non-deterministic changes and other changes can be computed by re-executing. 
         [0023]    In another approach, however, there is no need to take a snapshot at the start of the machine code. Instead the contents of a page of memory are read the first time the (each) page is accessed during the reference execution. Trapping accesses to memory locations is described later with reference to non-determinism but it should be recognised that the technique described here is not related to non-determinism—instead it is effectively a way to record the starting state “just in time”. This is thus a deterministic approach defined by the (portion of) machine code in the data structure. Such an approach can, for example, provide reduced memory usage in some situations. 
         [0024]    Thus in embodiments of this approach one can begin, for example, with an empty starting state and then during the reference execution “fault in” state when it is first accessed. For example, one approach may begin with all (relevant) memory set up by the MMU (memory management unit) to fault when accessed, and then as the reference execution is run, respond to faults by creating events in the event log of the page that has been accessed, and then changing the page&#39;s permissions so that it the page does not fault the next time. Alternatively the machine code may be instrumented to achieve a similar effect. 
         [0025]    Optionally the machine code in the data structure may additionally (or alternatively) comprise instrumented machine code instrumented to permit deterministic replay of non-deterministic events and/or post hoc execution analysis. As the skilled person will also appreciate, embodiments of such a data structure may be distributed between a plurality of coupled components in communication with one another. 
         [0026]    Thus in a related aspect the invention provides a method of capturing reference state data for deterministic re-execution of a computer program for analysis, wherein said program is represented by machine code, the method comprising: recording data defining content read from portions of memory accessed during a reference execution of said machine code; wherein said content comprises content read from a portion of memory the first time that the page is accessed during a reference execution of said computer program for re-execution; and wherein said recorded data comprises reference state data defining said content is usable for deterministic re-execution of said machine code. 
         [0027]    In some preferred embodiments the method further comprises configuring the portion of memory accessed by the machine code, to generate a fault when accessed. In preferred embodiments this portion of memory is a page of memory. The procedure then responds to the fault to capture data read from the portion/page of memory in an event log and changes an access permission for the portion/page such that said fault is not generated when that portion/page of memory is accessed subsequently. 
         [0028]    The invention further provides processor control code to implement embodiments of the above described method, and corresponding systems. The code is provided on a non-transitory physical data carrier such as a disk or programmed or non-volatile memory. Again the code may be distributed amongst coupled components in a system. 
         [0029]    The invention further provides a backwards debugger configured to generate program analysis data, the backwards debugger comprising software to: run a first instrumented version of machine code representing the program, wherein said running defines a reference execution of said program; capture a log of non-deterministic events during said reference execution such that the machine code can be re-run in a deterministic manner to reproduce states of a processor and memory during the re-running; generate a second instrumented version of said machine code, said instrumented version of said machine code comprising instrumented machine code to replay execution of said machine code representing the program and to capture and store program state information during said replayed execution, wherein said program state information comprises one or both of one or more values of registers of said processor and one or more values of memory locations used by said program; run said instrumented machine code whilst reproducing said non-deterministic events during said running to reproduce said reference execution; and capture said program state information whilst reproducing said reference execution to generate said program analysis data. 
         [0030]    The invention still further provides a backwards debugger configured to implement a method of returning to a state in the history of execution of a computer program, said state comprising a set of values of one or more of registers of a processor on which the program is running and of working memory space to which the program has access, the method comprising: identifying, in machine code representing said program, instances of machine code instructions associated with substantially non-deterministic events; generating a first instrumented version of said program machine code instructions to handle said substantially non-deterministic events; executing said first instrumented version of said program machine code; storing a time series of said states, including a log of said non-deterministic events, during said executing to define a reference execution; restoring a said stored state; and executing said first instrumented version of said program machine code forward in time starting at said restored state to return to said state in said program history of execution; wherein the backwards debugger is further configured to: input data defining an analysis to be performed on said machine code; generate a second instrumented version of said program machine code to perform said analysis; run said second instrumented version of said program machine code whilst reproducing said non-deterministic events during said running to reproduce said reference execution of said program; and store program analysis data generated by said second instrumented version of said machine code when said second instrumented version of said program machine code is run. 
         [0031]    As previously described, in embodiments the first time the machine code representing the program is run it is modified to capture and log non-deterministic events. Thereafter it is instrumented to replay the log in a deterministic manner, in order to facilitate one or multiple instances of deterministic execution replay for analysis. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0032]    The operation of an example backwards debugging system, in the context of which aspects and embodiments of the invention may operate, is described with reference to the accompanying drawings in which: 
           [0033]      FIG. 1  shows a running program with snapshots at regular 2 second intervals; 
           [0034]      FIG. 2  shows an example Linux program; 
           [0035]      FIG. 3  shows an example of a computer system; 
           [0036]      FIG. 4  shows a flowchart showing the instrumentation algorithm; 
           [0037]      FIG. 5  shows the program P and its instrumented counterpart P′; and 
           [0038]      FIG. 6  shows interception of asynchronous events. 
       
    
    
     DETAILED DESCRIPTION OF PREFERRED EMBODIMENTS 
       [0039]    To provide context for describing the operation of embodiments of the invention we first describe some backwards debugging systems in the context of which some preferred embodiments of the invention operate. 
       Backwards Debugging Systems 
       [0040]    Broadly a backwards debugger allows a program to be executed in such a manner that it appears that the execution is backwards, that is in a reverse direction to the normal direction of program code execution. Thus in a backwards debugger is a debugger that allows a program being debugged to be rewound to an earlier state, and then allows the user to inspect the program&#39;s state at that earlier point. Such a debugger ideally provides commands to allow the user to step the program back in small well-defined increments, such as single source line; a machine instruction; step backwards into, out of, or over function calls and the like. 
         [0041]    We will describe bidirectional or backwards debugging where (preferably) substantially the complete state of a running computer program can be examined at any point in that program&#39;s history. This uses a mechanism to ‘unwind’ the program&#39;s execution. This is a difficult problem, because as a program executes previous states are generally irretrievably lost if action is not taken to record them (for example, writing to a memory location causes whatever information was previously at that memory location to be lost). There are two approaches to solving this problem: firstly to log every state transition as the program executes; secondly, to re-execute the program from an earlier recorded state to reach the desired point in its history. The first suffers from several problems, including slow forwards execution of the program, and the generating of large amounts of data as the program executes. The second approach is generally more efficient but requires that non-determinism be removed on re-execution so that the program follows exactly the same path and transitions through exactly the same states each time it is re-executed. 
         [0042]    We describe a mechanism whereby a ‘snapshot’ is periodically taken of a program as it runs. To determine the program&#39;s state at a given time t in its history, we start with the snapshot taken most recently before time t, and execute the program forwards from that snapshot to time t. For example,  FIG. 1  depicts a program under execution. The program has been running for a little over 7 seconds, with snapshots having been taken every 2 seconds. In order to find the state of this program at t=5s the snapshot taken at 4s is replayed for 1s. We use the inherent determinism of a computer to ensure that the when the snapshot of the program is replayed to time t, it will have exactly the same state as had the original program at time t. The UNIX fork system call provides one mechanism to snapshot a process. 
         [0043]    Unfortunately, while a computer itself is deterministic, computer programs do not run deterministically, due to non-deterministic inputs. That is, when we say a computer is deterministic we mean that given the same set of inputs, it will always run through the same state changes to the same result. Therefore, if we wish to ensure that a snapshot of a program is replayed exactly as the original, we should ensure that exactly the same inputs are provided to the replayed program as were provided to the original. 
         [0044]    Fortunately, most modern, ‘protected’ operating systems provide a sanitised ‘virtual environment’ in which programs are run, commonly referred to as a process. An important feature of processes in this context is that they strictly limit the computer resources that are accessible to a program, making it practical to control all sources of non-determinism that may influence a program&#39;s execution. These resources include the memory that is accessible by the process, as well as operating system resources, such as files and peripherals. We define all such resources as the process state. The memory and register set of a process make up its internal state, while operating system resources that it may access make up its external state. The controlled environment of a process means that with the help of instrumentation (see section 3) it is practical to eliminate substantially all significant sources of non-determinism during execution of the process. 
         [0045]    We have identified four categories of non-determinism for a computer process executing on a protected operating system: 
         [0000]    1) Non-deterministic instructions are instructions which may yield different results when executed by a process in a given internal state. The most common form of non-deterministic instruction is the system call (i.e. the instruction used to make a request of the operating system). For example, if a process issues a system call to read a key press from the user, the results will be different depending on which key the user presses. Another example of a non-deterministic instruction is the Intel IA32 rdtsc instruction, which obtains the approximate number of CPU clock ticks since power on.
 
2) A program executing multiple threads will show non-determinism because the threads&#39; respective transactions on the program&#39;s state will occur in an order that is non-deterministic. This is true of threads being time-sliced onto a single processor (because the operating system will time-slice at non-deterministic times), and of threads being run in parallel on multiprocessor systems (because concurrent threads will execute at slightly different rates, due to various external effects including interrupts).
 
3) Asynchronous events are events issued to the process from the operating system that are not the direct result of an action of that process. Examples include a thread switch on a multithreaded system, or a timer signal on a UNIX system.
 
4) Shared memory is memory that when a location read by the program being debugged does not necessarily return the value most recently written to that location by the program being debugged. For example, this might be because the memory is accessible by more than one process, or because the memory is written to asynchronously by the operating system or by a peripheral device (often known as DMA—Direct Memory Access). As such out-of-band modifications are performed outside of the context of the program being debugged, this may result in non-determinism during re-execution.
 
         [0046]    Preferably a bidirectional or backwards debugging system should be able to work in all circumstances, and preferably therefore the aforementioned sources of non-determinism should be eliminated. To achieve this, all non-deterministic events are recorded as the debugged process executes. When replaying from a snapshot in order to obtain the program&#39;s state at some earlier time in history, the recorded non-deterministic events are faithfully replayed. The mechanism used to employ this is described in the following section. 
         [0047]    We employ a technique of machine code instrumentation in order to record and replay sources of non-determinism. Our instrumentation is lightweight, in that it modifies the instrumented program only slightly, and is suitable for use with variable length instruction sets, such as Intel IA32. 
         [0048]    We instrument by intercepting control flow at regular intervals in the code. Sections of code between interception are known as basic blocks. A basic block contains no control flow instructions, and no non-deterministic instructions—that is, a basic block contains no jumps (conditional or otherwise) or function calls, nor system calls or other non-deterministic instructions, or reads from shared memory. Control flow and non-deterministic instructions are therefore termed basic block terminators. 
         [0049]    An instrumented program is run such that all the basic blocks are executed in the same order and with the same results as would be the case with its equivalent uninstrumented program. The instrumentation code is called between each basic block as the instrumented program executes. Each of the program&#39;s original basic blocks are copied into a new section of memory, and the basic block terminator instruction is translated into one or more instructions that ensure the instrumentation code is called before control continues appropriately. 
         [0050]    As an example, consider the Linux program shown in  FIG. 2 , written in Intel IA32 assembler (using GNU/AT&amp;T syntax): 
         [0051]    This simple program reads characters from stdin, and echos them to stdout. The program contains four basic blocks, terminated respectively by the two int $0x80 instructions, the jne and the ret instruction at the end. 
         [0052]    For convenience, we term the uninstrumented program P, and its instrumented equivalent P′. For each basic block there is an uninstrumented basic block B n , and a corresponding instrumented basic block B′ n . 
         [0053]      FIG. 3  shows an example of a computer system on which the program may be executed and on which bi-directional debugging may be performed. The target program and the debugger both reside in physical memory. Processor registers may be captured and stored in snapshots along with memory used by the target program process. The debugger may operate within the virtual memory environment provided by the processor and the operating system, or it may operate on a single process computer. 
         [0054]      FIG. 4  shows a flowchart that illustrates the instrumentation algorithm. (Note that algorithm instrumented code in an ‘on-demand’ fashion, as that program executes; an ahead of time algorithm is also practical.) 
         [0055]      FIG. 5  shows the program in the previous example broken into its four basic blocks, and how those basic blocks are copied, and how the basic block terminator instruction for B n  is replaced in B′ n  with one or more instructions that branch into the instrumentation code. The label target is used to store the uninstrumented address at which control would have proceeded in the uninstrumented version of the program; the instrumentation code will convert this to the address of the corresponding instrumented basic block and jump there. 
         [0056]    The copying and modifying of basic blocks for instrumentation may be carried out statically before the program is executed, or may be done dynamically during the program&#39;s execution (i.e. on demand). Here, when the instrumentation code looks up the address of an instrumented basic block given the corresponding uninstrumented address, if the instrumented version cannot be found then the uninstrumented block is copied and the basic block terminator translated. (Our implementation uses the dynamic approach.) 
         [0057]    We will next describe making replay deterministic. Using the instrumentation technique described in 3 we are able to remove all sources of non-determinism from a process. We deal with each of the four kinds of determinism separately in subsections below. 
         [0058]    Non-deterministic instructions: During the reference execution the results of all non-deterministic instructions (including system calls) are recorded in an event log. When playing a process forwards from a snapshot in order to recreate a previous state, the process is said to be in ‘replay mode’. Here, the instrumentation code ensures that non-deterministic instructions are not executed, and instead their results are synthesised using data stored in event log. There the process&#39; internal state is artificially reconstructed to reflect the results of the corresponding non-deterministic instruction produced during the reference execution. 
         [0059]    For example, when replaying a system call, this means restoring the system call&#39;s return code, as well as any of the process&#39;s memory that was modified as a result of the system call. 
         [0060]    External state (operating system resources): Note that it is not necessary to reconstruct the process&#39; external state when recreating the results of non-deterministic instructions, because the process&#39; interaction with its external state is in general governed entirely through system calls. For example, consider a process the opens a file for reading during the reference execution. The process will receive a file descriptor (also known as a file handle) which it will use with future calls to the OS to read from the file. The file descriptor is obtained and used with system calls. These system calls will be shortcut in the replay process. In effect, the instrumentation code will ensure that the replay process ‘believes’ that it has the file open for writing, but in fact it does not. 
         [0061]    However, this is not true for OS resources that are visible from the process&#39; internal state. As an example, consider a call to the OS to expand a process&#39; address space (i.e. the memory it can access). Since this affects a resource which the replay process will access directly (i.e. memory), this system call should be reissued on replay to ensure that the effects of the non-deterministic instruction in question are faithfully replayed. 
         [0062]    Note that memory mapped files are not treated specially; the entire contents of the file that is mapped are preferably recorded in the event log so that the effects of the memory map operation may be replayed. This is because the memory mapped file may be in a different state (or may not even exist) during replay. However, it is possible to optimise this case by recording and replaying the on-demand mapping of pages of such files. Here, when a process maps a file during the reference execution, the instrumentation code ensures that the process does not really map the file, although the instrumented program is ‘unaware’ of this. This means that when the process attempts to access the pages of the file it believes are mapped, it will fault. The instrumentation code intercepts these faults, and maps the pages from the file, recording the contents of those pages in the event log. On replay, again the file is not mapped. However, this time when the replay process faults accessing the pages, the instrumentation code obtains the contents of those pages from the event log, and maps the pages and initialises them appropriately. Alternatively, memory mapped files may be considered as shared memory, and dealt with as described below. 
         [0063]    Asynchronous events: It is important that asynchronous events are replayed substantially exactly as they occur during the reference execution. During the reference execution, we use instrumentation to obtain a sufficient level of control over when asynchronous events happen, so that these events may be faithfully reproduced in replay mode. This means that all asynchronous events are preferably delivered to the instrumented program at basic block boundaries. 
         [0064]    Asynchronous messages: Many modern operating systems provide a facility where an application can register an asynchronous event handling function. When the asynchronous event occurs, the operating system interrupts the program, transferring control directly to the handler function. When the handler function returns, the program proceeds as before interruption. This mechanism may be referred to as asynchronous signal delivery, or software interrupt servicing. 
         [0065]    Such asynchronous events are preferably controlled to ensure that they are essentially entirely repeatable. To achieve this, during the reference execution, the instrumentation code intercepts system calls to set up a handler for an asynchronous message. The request is manipulated such that the instrumentation intercepts asynchronous messages. 
         [0066]    This is depicted in  FIG. 6 . The instrumentation code does not deliver the asynchronous notification directly to the program (i.e. it will not directly call the program&#39;s asynchronous event handler function). Instead the instrumentation code&#39;s event handling function record the asynchronous event to the event log, and then arrange for the event handler to be executed under the control of instrumentation. 
         [0067]    When replaying, asynchronous events are not delivered to the replay process at all. Instead, each time a basic block is executed, the event log is checked. If an event is scheduled for the current basic block, then the process&#39;s event handling function is called, thus faithfully replaying the asynchronous event. 
         [0068]    As well as providing determinism, this mechanism also ensures that the asynchronous event handling function is instrumented when it is called. Otherwise, if the operating system is allowed to call the program&#39;s event handling function directly, then the original, uninstrumented code will be called, and we will ‘lose’ instrumentation. 
         [0069]    Note that message-based systems such as Microsoft Windows® use a system call to retrieve the next message from a message queue; the mechanism outlined in section 4.1 covers this case. 
         [0070]    Threads: There are two main ways to implement multithreading within a process: kernel managed threads, and user managed threads. With user-managed threads, a user-mode library is responsible for threading. Thread pre-emption is performed by the library by responding to asynchronous timer events—hence any non-determinism resulting from user-managed multithreading can be removed using the techniques described in the section on Asynchronous events. 
         [0071]    However, most modern computer systems use kernel-managed threads. Here the operating system kernel is responsible for switching and otherwise managing threads, in general entirely without direct support from the application. There are several mechanism that can be employed to obtain deterministic kernel-managed threads. 
         [0072]    One technique is to use the instrumentation code to implement ‘virtual-kernel-managed threads’, which involves the instrumentation code effectively providing user-managed threads, but letting the application ‘believe’ it is using kernel managed threads. Here, the system call to create a new kernel managed thread is intercepted by the instrumentation code, and subverted such that the instrumentation code creates a virtual kernel-managed thread within the single real kernel managed thread. The instrumentation code multiplexes all virtual kernel-managed threads onto a single real kernel-managed thread. This means that thread switching is under control of the instrumentation code and can be made essentially entirely deterministic. The instrumentation code can provide pre-emptive multithreading by effecting a virtual kernel-managed thread switch every n basic blocks (e.g. where n=10,000). 
         [0073]    Here, care must be taken if we wish to ensure deadlock is avoided. If a virtual kernel-managed thread blocks waiting for the action of another virtual kernel-managed thread, since both virtual threads are running within a single real thread, deadlock can result. (A particularly common example of this problem is when two virtual kernel-managed threads contend on a mutual exclusion primitive; if care is not all virtual kernel-managed threads will deadlock). One way to avoid deadlock on a UNIX system to periodically arrange for the process to be delivered an asynchronous timer signal, such that blocking system calls will be interrupted, returning EINTR. 
         [0074]    An alternative mechanism involves letting the program create kernel-managed threads as normal, but subverting the thread creation such that the instrumentation code has control over which thread is executing at which time. This might involve modifying the threads&#39; priorities such that the instrumentation code can control which thread the OS will execute, or perhaps artificially blocking all but one thread at a time by e.g. having all kernel managed threads contend on a single kernel-managed mutex (which we shall call ‘the debugging mutex’). This technique would also suffer a similar deadlock problem referred to above. Here if the kernel-managed thread that owns the mutex waits for an operation to be completed by another thread, the system will deadlock. (This is because the other thread will never be able to complete its work because it is waiting for the debugging mutex, yet the thread that owns the debugging mutex will never release it because it is waiting for that other thread.) Fortunately, the only way a thread can block awaiting the result of another is through a system call. Therefore, this problem can be overcome by ensuring that any thread drops the debugging mutex before entering any system call that may block, and then takes it again on return from said system call (note that there is no problem if a thread “busy-waits” because eventually it will execute a maximum number of basic blocks and then drop the debugging mutex). However, if the debugging mutex is to be dropped when a system call is issued, care must be taken to ensure that the system call does not modify the program&#39;s internal state in a way that voids determinism. For example, if the system call is reading data off the network and writing that data into the program&#39;s address space while concurrently another thread that holds the debugging mutex is reading that same memory, non-deterministic behaviour will result. Fortunately, this problem can be avoided be having the system call read not into the program&#39;s internal state, but into the event log. After the debugging mutex has been taken on behalf of the thread that issued the system call, then the data that was read by the system call into the event log can then be copied into the program&#39;s internal state. This trick can be implemented with relatively little work, since we already have the requirement that system calls that write into user memory have their results stored in the event log. Therefore, rather than have the system call read into program memory and then copying that data into the event log, we instead subvert parameters to the system call such that data is read directly into the event log, and have the instrumentation code subsequently copy from the event log into program memory, but only once the debugging mutex has been taken. 
         [0075]    Shared memory: If a process being debugged shares memory with another process, it is possible to exploit the operating system&#39;s memory protection mechanism to provide deterministic replay. 
         [0076]    Suppose that there are two processes, A and B, that share some portion of memory M, such that both processes have read and write permissions to access M. Process A is being run under instrumentation for bidirectional or backwards debugging, but process B is not. The shared memory M is initially mapped such that process B has read-only access, and A has full access. We describe this situation as process A having ownership of memory M. Any attempt by process B to read memory M will succeed as normal, but any attempt by process B to write to M will result in a page fault. This fault is responded to by memory M being mapped read/write to process B, and unmapped completely from process A. We refer to this process B taking ownership of the memory. Here, any attempt to access M (either for reading or for writing) by A will result in a page fault. This is responded to by reverting ownership of M to A, but in addition sufficient state being stored in the event log to replay the changes to M made by B. That is, the difference of the memory M between the point when A last had ownership of that memory and the current time is stored in the event log. 
         [0077]    When replaying, the difference in memory is retrieved from the event log and applied at the appropriate time. Thus the effect on A of B&#39;s asynchronous modification of memory M can be replayed deterministically. 
         [0078]    Note that the above scheme can easily by generalised so that process B is actually a group of one or more processes. 
         [0079]    An alternative approach is to record in the event log every memory read performed by A on the shared memory M. This has the advantage of being a simpler implementation, but depending on the usage of the shared memory may result in the recording of an unacceptable amount of state in the event log, as well as adversely affecting temporal performance. 
         [0080]    We will next describe implementation and structure of the event log. As we have seen, there are several kinds of events that need to be recorded in the event log: Non-deterministic instruction results (including the return codes and memory modifications made by system calls), Asynchronous events (including asynchronous signal delivery), Thread Switches, and Shared memory transactions. 
         [0081]    Preferably the memory used to store the event log is accessible by the process in record and replay mode. This means that if the UNIX fork facility is used to snapshot processes, then the memory used to store the event log should be shared between each process created with these forks. However preferably the event log (and all memory used by the instrumentation code) is not usable as the internal state of the program being debugged; to prevent this all memory transactions by the program being debugged can be intercepted by the instrumentation code, and access to memory used by the instrumentation code (including the event log) can be denied to the program being debugged. 
         [0082]    Preferably the event log itself is stored as a linked list, where each node contains the type of event, data sufficient to reconstruct that event during replay, and the time at which that event happened (where time is based on the number of instructions executed to that point or some approximation thereof, preferably combined with the number of non-deterministic or asynchronous events executed to that point). 
         [0083]    Then when in replay mode, between each basic block it is necessary only to inspect the current time, and compare it with the time of the next non-deterministic event in the event log. In the common case that the current time is less than the time for the next non-deterministic event, the coming basic block can be executed without further delay. If there is a non-deterministic event to replay in the coming basic block then the instrumentation must arrange for the effects of the said non-deterministic event to reconstructed at the corresponding time in the coming basic block. 
         [0084]    We will next describe searching history. In general, it is more useful for a bidirectional or backwards debugger to be able to search history for a particular condition, as opposed to wind a program back to an absolute, arbitrary time. Some examples of the kinds of conditions it is useful to be able to search are: 
         [0000]    The previously executed instruction
 
The previously executed source code line
 
The previously executed source code line at the current function call depth
 
The call site for the current function The previous time an arbitrary instruction or source code line was executed
 
         [0085]    More generally, it is useful to be able to rewind a debugged program to the previous time an arbitrary condition held, such as a variable containing a given value, or even completely arbitrary conditions, such as some function returning a particular value. 
         [0086]    We have implemented an algorithm to search an execution history for such arbitrary conditions. The most recent snapshot is taken, and played forward testing for the condition at the end of each basic block. Each time the condition holds, the time is noted (if a time is already recorded because the condition held earlier, it is overwritten). When the history is replayed up to the debug point, the most recent time at which the condition held will be stored. If no such time has been recorded because the condition did not hold since the most recent snapshot, then the search is repeated starting from the next most recent snapshot, up to the most recent snapshot. That is, suppose that the debugged program is currently positioned at time 7,000, and there are snapshots at times 0; 2,000; 4,000; and 6,000. We start at the snapshot at time 6,000 and play forwards until time 7,000, testing for the condition between each basic block. If the condition never holds between times 6,000 and 7,000, then we rewind to the snapshot taken at 4,000, and play that forwards to 6,000, searching for the event. If the condition still isn&#39;t found to hold, we check 2,000-4,000, and so on. 
         [0087]    Note that this algorithm will not work reliably with the instrumentation technique described in section 3 if searching for the most recent time at which a variable held a particular value. This is because a variable&#39;s value may change to and then from the required value entirely within a basic block. To overcome this, there is an enhancement to the instrumentation technique described in section 3—each memory write operation is considered a basic block terminator. (This approach can also be used to ensure that a program that has gone hay-wire does not write over the event log or other instrumentation data structures.) This form of instrumentation will operate less efficiently than the one described in section 3; however should the performance become problematic, it is possible to run with both forms of instrumentation, switching between the two as necessary. 
         [0088]    (Note that the algorithm described in this section does work reliably when searching for particular values of the program counter with the instrumentation technique described in section 3.) 
         [0089]    We have described a bidirectional or backwards debugging mechanism that can be conveniently implemented on most modern operating systems for example including, but not limited to, Linux and Windows®. A process can be rewound and its state at any time in its history can be examined. This is achieved by regularly snapshotting the process as it runs, and running the appropriate snapshot forward to find the process&#39; state at any given time. Non-determinism may be removed using a machine code instrumentation technique. 
         [0090]    Our implementation for the Linux operating system is responsive and pleasant to use, and promises to greatly reduce debugging times for particularly subtle and difficult bugs. We have also implemented a searching technique that permits the most recent time that an arbitrary condition holds in a process&#39;s history. 
         [0091]    Our technique of instrumenting machine code rather than source-level analysis is particularly important, because it means the system copes with bugs where the compiler-dictated control flow is subverted (e.g. overwriting a function&#39;s return address on the stack). 
       Further Techniques for the Deterministic Replay of Computer Programs 
       [0092]    We now describe some further techniques for instrumenting execution of a computer program such that sufficient information may be recorded in an efficient manner to provide deterministic replay of the said computer program in the light of shared memory accesses, and when the said computer program is multithreaded. 
         [0093]    Techniques we describe for identifying processes with shared memory access, such as threads or multicore processes, comprise arranging process (thread) memory ownership to deliberately provoke memory page faults, to identify and handle concurrent memory access by multiple threads in such a manner as to enable deterministic replay, and hence backwards debugging. 
         [0094]    Deterministic replay of a recording of a computer program can be achieved providing that (a) the program is replayed using the same starting state as the recording, and (b) inputs and other non-deterministic effects are synthesised to be replayed exactly as occurred during the recording. Such sources of non-determinism include:
       i) non-deterministic instructions, including system calls (e.g. reading from a file or network)   ii) asynchronous signals   iii) reads from shared memory   iv) ordering of accesses to memory by concurrent threads       
 
         [0099]    Here we describe techniques to address both (iii) and (iv) by using the MMU (memory management unit) to fault in (i.e. trap) accesses to certain memory locations and use such traps to determine the ownership of memory and track different users of the memory. 
         [0100]    Shared memory: We define “shared memory” as memory whose contents when a given location is read by the program being debugged does not necessarily return the value most-recently written to that location by the program being debugged. Typically this is because the memory is shared with another program which may write a new value to the memory location between the program being debugged writing and reading it back. Shared memory may also be updated in such asynchronous fashion by the operating system (asynchronous IO), or by a device to which the program being debugged has direct access (e.g. with kernel-bypass IO such as Direct Memory Access, also known as DMA). 
         [0101]    One way to record reads from shared memory such that they may be later replayed non-deterministically is to instrument all reads from memory, and for each access determine whether the address of the memory read is shared memory, and if it is, record to the event log the value read from shared memory. This imposes significant overheads, in particular, every single access to shared memory must be checked. 
         [0102]    A better way is to use the system MMU to determine which instructions access shared memory. Here, all shared memory is remapped to a new virtual address, which is unknown to the program being debugged. This is termed the “really shared memory”. In its place is mapped a new mapping, which we refer to as the “emulated shared memory”. The MMU is programmed (e.g. via a call to mprotect on the Linux operating system) such that any access to the emulated shared memory shall result in a memory protection fault (also referred to as a memory protection trap). All such faults are intercepted, and in response to this the code that triggered the fault is retranslated such that when accessing memory it first checks the address to determine whether it is shared, and so (a) redirects the access to the “really shared memory” address, and (b) stores values read from the really shared memory location in the event log for later deterministic replay. 
         [0103]    A further optimisation is to record in the event log only those shared memory locations that have been modified since the previous access by the program being debugged. To achieve this, a third mapping accompanies the “emulated shared memory” and the “really shared memory”, which is known as “the third copy”. The third copy is kept in sync with the really shared memory from the perspective of the program being debugged (in other words with the logical contents of the emulated shared memory, in that it contains the contents of the shared memory that will be read at the corresponding time during replay). On access to the shared memory by the program being debugged, an event is added to the event log only if the really shared memory and third copy differ. 
         [0104]    The algorithm for a retranslated instruction that accesses shared memory is: 
         [0105]    For each address A, that the instruction accesses: 
         [0106]    If address A is shared:
       compute address A 1  as the equivalent really shared memory address   compute address A 2  as the equivalent third copy address   alloctae a temporary variable T 1         
 
         [0110]    If the instruction reads at A
       If the instruction also writes at A   lock address A 1         
 
         [0113]    copy contents of A 1  into T 1   
         [0114]    if the instruction reads at A and the contents of T 1  differs from the contents of A 2 :
       copy contents of T 1  into A 2     create a new event in the event log to indicate that this read from   address A should be replayed to use the value now in T 1 .       
 
         [0118]    substitute T 1  for A in the instruction 
         [0000]    Execute instruction as modified
 
For each address A, . . . that the original unmodified instruction would access:
 
         [0119]    If the instruction writes at A and address A is shared
       copy contents of T 1  into A 1     If the instruction also reads at A
           unlock address A 1     
               
 
         [0123]    Locking an address prevents any other process from writing to it. The mechanism used to lock and unlock an address depends on the facilities provided by the hardware. On some types of computer, it may be possible to simply lock and unlock as described. On others, it may be possible only to determine whether another process has changed the contents of the address before writing back to it, and if so repeat the algorithm above again. 
         [0124]    For the vast majority of programs, even those that make relatively heavy use of shared memory, the proportion of instructions actually accessing the shared memory is relatively small (compared e.g. to accesses to the stack). This technique ensure that only the relatively small proportion of instructions that access the shared memory have the extra overhead of dealing with shared memory, whereas most instructions run entirely unencumbered. 
         [0125]    Multithreaded execution: The simplest way to deal with non-determinism due to differently-ordered accesses to memory by concurrent threads is to force serialisation of all threads when recording, and ensure that during deterministic replay each thread is executed in the same order as during record. In short, this means serialising all threads and recording thread switch events in the event log. However, such serialisation is unfortunate because it (a) slows down execution on multicore systems (i.e. only one of the CPU cores can be used at any one time), and (b) it changes the behaviour of the system compared to it being run normally (such lower fidelity can be mean bugs that are being investigated do not appear under the control of the debugger). 
         [0126]    Here we present an improved mechanism that allows concurrent threads (processes) to be executed on multiple cores concurrently. It relies on the observation that multiple threads within a single (common) program is a similar arrangement to multiple programs using shared memory. i.e. the main difference between multiple threads and multiple programs is that multiple threads share memory. A variation on the technique described above for supporting deterministic replay of programs using shared memory can be used, allowing concurrent threads to be replayed without requiring strict ordering of memory accesses between those threads. 
         [0127]    The idea is based on the observation that most memory locations referenced by most multithreaded programs will not in reality be “shared” between the multiple threads (e.g. most stack accesses are local to a specific thread). If memory within the program can be assigned an owning thread, and each thread is given its own event log, threads&#39; accesses to memory locations that they do not own can be treated as accesses to conventional shared memory, as described above and threads&#39; accesses to memory locations that they do own can proceed as normal 
         [0128]    Here, each memory location (or group of memory locations such as a page), is in one of the following states:
       unused (all memory is initially in this state)   single-threaded (i.e. owned by a single thread; said thread is the only thread recently to have accessed this memory)   multithreaded—i.e. shared between multiple threads (more than one thread has recently accessed the memory)       
 
         [0132]    Memory locations can change ownership over time in the following ways:
       from unused to single-threaded,   from single-threaded to unused, or   from single-threaded to multithreaded, or   from multithreaded to unused
 
(ownership need never directly change from one thread to another). When recording, any read or write of memory address P by a thread T 1  results in different behaviour depending on the ownership of memory P:
   Memory P is already owned by thread T 1 : continue as normal.   Memory P is currently unused: thread T 1  takes ownership of memory P,   a memory ownership event is added to T 1 &#39;s event log, and then continue as normal.   Another thread T 2  owns memory P: memory P is marked as multithreaded, a memory ownership event is added to T 2 &#39;s event log, and the memory access continues as for shared memory accesses described above.   Memory P is multithreaded: the memory is accessed as per conventional shared memory as documented above; if necessary, a memory-read event is added to T 1 &#39;s event log.       
 
         [0142]    It is desirable to allow memory locations to be changed from multithreaded to single-threaded in the case that the memory&#39;s access pattern changes (e.g. perhaps the memory is on the heap, and is being used by multiple threads but is then freed and then reallocated for use by a single thread). To facilitate such a case, associated with each multithreaded memory location P is:
       An identifier Tprev describing the most recent thread to access it, and   An integer N that holds of the number of consecutive accesses to it by thread Tprev       
 
         [0145]    For each access to multithreaded location P by thread T 1 , if P&#39;s Tprev is not T 1 , then P&#39;s Tprev is set to T 1  and P&#39;s N is set to 0; otherwise, P&#39;s N is incremented. If P&#39;s N exceeds some threshold, the memory P is marked as being single-threaded and owned by T 1 , and a memory ownership event is added to T 1 &#39;s event log. 
         [0146]    When replaying, it is important to synchronise memory ownership events to preserve ordering of memory accesses between threads. Specifically, when thread T 1  replays a memory ownership event such that it becomes the owner of memory P, it should not proceed until the previous owner thread T 2  has replayed its memory ownership change event marking the corresponding memory as not owned by it. 
         [0147]    To implement this model, it is desirable to be able reliable to track which memory locations are accessed by which threads. 
         [0148]    Implementation: The overheads of running on multiple cores concurrently (as opposed to running serially) will depend on the following factors (the values of which will depend on the implementation):
       the relative proportion of the memory accesses by a thread that does not already own the memory being accessed, and   the extra overhead incurred by a thread when accessing memory that it does not own (i.e. the memory is multithreaded), and   the extra overhead incurred by a thread when accessing memory that it does own (i.e. the memory is single-threaded)       
 
         [0152]    Three possible implementations are described below, each with different trade offs. In particular, the MMU can be used to trap accesses by a thread to memory which it does not own. This is attractive as it implies no extra 
         [0153]    overhead for a thread accessing memory it does own. Multiple threads within a process are not usually able to have different MMU mappings, but below we describe two ways this can be achieved (i and ii). A third implementation alternative (iii) is suggested, whereby the MMU is not used. This imposes some overhead even to accesses to single-threaded memory (as ownership must first be checked), but at the benefit of lower cost of accessing multithreaded memory. 
         [0154]    i. MMU-based multiprocess: As alluded to above, multiple threads within a process is logically equivalent to multiple processes sharing memory. For each thread created by the program being debugged, in reality a new process is created, where all the memory is shared with the other “threads” of the program being debugged (where these other “threads” are in reality other processes). 
         [0155]    Memory not owned by a thread (where in this context thread is in reality a process) should be mapped into the process at a different virtual address (effectively the “really shared mapping”, as described above). Each process should also maintain a “third copy” for memory it does not own, again as described above. 
         [0156]    In such an implementation, care would need to be taken to ensure correct operation of pan-process resources, including file-descriptors, signal handlers, resource limits, and newly-created memory mappings. In Linux, it is possible to create new processes with the CLONE_FS and CLONE_FILES flags, which eases this problem. Maintaining a perfect illusion of different processes being the same process may still be difficult to achieve, however. 
         [0157]    ii. MMU-based single-process: It is possible to effectively give each thread within a process its own address space by offsetting each thread&#39;s memory access by some fixed amount, where each thread is assigned a unique offset such that the adding of any valid address to the offset does not yield another valid address. 
         [0158]    This could be achieved by translating code differently for each thread, applying the relevant constant offset to all memory accesses. 
         [0159]    The main disadvantage with such an approach is that it will put considerable pressure on the virtual address space. This is unlikely to be a problem in practice for programs with a 64-bit virtual address spaces, but may be prohibitive for 32-bit address spaces. 
         [0160]    iii. MMU-less: Alternatively one can avoid use of the MMU, and keep a “meta-data” that gives a thread owner for each address, and every access would do a software check of ownership. This would impose overheads for single-threaded accesses to memory, although accesses to unused memory and some accesses to multithreaded memory would be cheaper (because in such cases there would be no memory protection faults). 
       Post-Hoc Analysis of Program Execution 
       [0161]    We now describe methods and systems for analysing the operation of a computer program according to some preferred embodiments of the invention. These preferably, but not essentially, employ a backwards debugging system as previously described: to allow deterministic replay of machine code; to perform a reverse search to find a most recent time when a condition held; and to facilitate single or multiple deterministic execution replays with instrumentation code (which may be the same or different for different executions), to facilitate debugging analysis. 
         [0162]    Thus broadly speaking we will describe a method for using customised instrumentation and a recording of a computer program&#39;s execution to perform arbitrary analysis of the execution, where the choice of how to analyse a program&#39;s execution does not have to be made before the reference execution: Instead embodiments of the system are able to analyse the (exact) same execution in multiple different ways. In particular, by using the concept of a ‘reverse search’, it becomes possible to analyse program execution in ways that were previously impractical. 
         [0163]    Typically a recording of a program&#39;s execution comprises a starting state, and a log of non-deterministic events that happened during execution. Non-deterministic events have been described above and include the effects on registers and the process&#39;s memory of system calls, reads from shared memory, non-deterministic CPU instructions, delivery of asynchronous signals and the like. 
         [0164]    One method of creating such a recording is to instrument the binary code of the program, so that during the reference execution the system can get control when a non-deterministic event happens and record the exact time and effects of the event, so that the event can be stored for use later to faithfully replay execution. When replaying such a recording, the system uses instrumentation in a similar way, so that control is acquired at the exact point in execution at which each event needs to be replayed. A recording of the execution may be kept in memory, or stored in a file to allow multiple instances of replay. 
         [0165]    Preferred embodiments of the invention concern the use of customised instrumentation when replaying. By embellishing the instrumentation with extra functionality, it is possible to log and analyse the execution of the original program in arbitrarily complicated ways. Furthermore, working with a recording means that a user does not have to decide in advance what to analyse—instead a user is able to decide after the system has recorded the execution, and to employ custom instrumentation when replaying execution in order to find out about different aspects of the execution. 
         [0166]    This gives great flexibility—for example an analysis of memory usage may lead to a desire to look at where a particular value came from (for example a large value passed to malloc( )). Without access to a recording and custom instrumentation, this would be slow and inconvenient and would often not give consistent information (if the program does not behave exactly the same way each time it is run). 
       Example: Generating Function Call Graphs 
       [0167]    It can be useful to establish details about which functions call other functions, for example to generate a call-graph. While this is possible to do by inserting special code at compile, link, or run time, this requires special actions before execution. 
         [0168]    If the system has a recording of a program&#39;s execution then it is possible to add extra functionality to the replay instrumentation that tracks all ‘call’ instructions, storing the caller&#39;s address and the callee&#39;s address. 
         [0169]    For example, whenever a ‘call’ instruction is instrumented, typically it is translated into code that ensures that the code at callee&#39;s address has been instrumented, and jumps to the instrumented address instead of the non-instrumented address. The system can customise the instrumentation to insert extra code at this point to first jump to instrumentation code operating with a dedicated internal stack that writes information relating to the call to a file or an in-memory buffer, for example raw address data of the caller and/or callee. 
         [0170]    Replaying the execution of the program with this special instrumentation can then generate (preferably complete) information about all function calls, and the system can then separately use symbol information to translate the raw addresses to function names. 
         [0171]    In embodiments this instrumentation code (and the instrumentation code described hereafter) collects information relating to the program flow without changing the “visible” effect of the program. Thus in embodiments it comprises code and data which is separate to but interweaved with the instrumented program, and has its own private memory (including a stack). 
       Example: Getting a Log of Heap Usage 
       [0172]    Getting information about heap usage usually requires that some special code is inserted at compile, link or run time, to track calls to heap functions such as malloc( ), realloc( ) and free( ). 
         [0173]    If the system has a recording of a program&#39;s execution, then it is possible to add extra functionality to the replay instrumentation so that the malloc( ), realloc( ) and free( ) functions log their parameters and return values. 
         [0174]    This can be done by modifying the “instrumentation engine” (the interweaved instrumentation code described above) to know about, more particularly identify, the addresses of the functions of interest, and instrument these functions differently from normal, for example to log the parameters to a file or in-memory buffer, run the (instrumented) function, log the return value, and then return to the (instrumented) caller. 
         [0175]    Replaying the execution of the program with this instrumentation can then generate complete information about heap usage—for example (preferably all) attempts to allocate, reallocate and free memory, and the success/failure of these calls. 
       Example: Using ‘Register Watchpoints’ to Give Reverse Data-Flow Analysis 
       [0176]    Instrumentation code can track the flow of data through the execution of the program&#39;s execution by logging changes to particular registers. 
         [0177]    A particularly useful way of analysing dataflow is to answer the question ‘where did this value come from?’. When the value in question is in memory, the system can use hardware watchpoints (typically provided by the CPU), together with reverse execution to go back to the most recent time that the value was modified. However if a value is in a CPU register, there is no equivalent mechanism provided by CPU hardware. 
         [0178]    If the system has control over the instrumentation of execution replay however, the system can re-instrument the replay of execution in such a way as to detect all writes to one or more registers, using a technique we refer to as ‘register watchpoints’. In embodiments this involves analysing each CPU instruction to see what registers it reads and/or writes. For each basic block (a sequence of instructions ending in a branch), the system generates a bitfield where bit N is set if any instruction in that basic block writes register N. 
         [0179]    The system can then implement a search algorithm to detect the most recent write to any of a set of registers as follows: 
         [0180]    It is presumed that the system has a starting snapshot at time t 0 , and preferably some other snapshots that were created at intervals when replaying previously, at times t 1 , t 2 , t 3 , . . . (As previously described, once a log of non-deterministic events is available snapshots may be created after-the-event by replaying the “reference execution”). 
         [0181]    It is presumed that the system also has a current snapshot at tn. Go back to tn−1, and run forward until tn, but for each basic block update a global ‘time’ (instruction count) value with the current time if that basic block&#39;s register-write bitfield has any of the specified register bits set. 
         [0182]    If the system detected a basic block that writes to the specified registers, rewind back to the ‘time’, then analyse the instructions in the basic block individually to find the latest instruction that modifies any of the specified registers, and step forward to this latest instruction. 
         [0183]    Otherwise, go back to tn−2 and run forward to tn−1, and repeat. 
         [0184]    Eventually, this procedure will have found the most recent time at which any of the specified registers were written to. Thus the system can effectively use reverse-execution to track data flow. 
         [0185]    Instructions that write to a register may obtain the written value from different places: 
         [0000]    1. Load from memory.
 
2. Calculate a value from another source register, e.g. a ‘move’ instruction.
 
3. Calculate a value from more than one other source register, e.g. an ‘add’ instruction.
 
         [0186]    Often a user will want to continue to follow the data further back in execution time. 
         [0187]    In case 1, the system can follow the data further back in time by setting a hardware (for example CPU) watchpoint and doing a reverse-continue (that is, continuing the reverse search). Alternatively, for example in the case that hardware watchpoints are not available, the instrumentation code could be used to identify instructions that write to a given location in memory. 
         [0188]    In case 2, the system can perform another reverse search with a register watchpoint on the source register. 
         [0189]    In case 3, there are multiple sources of the data, leading to bifurcation. In this case, the system can offer the user a choice of which source to follow, or spawn separate jobs to automatically follow all paths. In some cases the multiple paths may ‘rejoin’ further back in time. In preferred implementations the separate jobs use a shared log to detect if they converge on the same path. This can be done by noting that a search is uniquely identified by its starting time and the register or memory watchpoints that are being used. 
         [0190]    The above approach can be contrasted with an approach that does not employ reverse execution. Without reverse execution there is a combinatorial explosion—all register and memory changes have to be tracked as there is a possibility that one of them may be involved in the ultimate dataflow of interest. In contrast, using the reverse-search technique described above allows the procedure to limit the search to just the registers or memory that are determined to be important at each particular stage. 
       Example: Using ‘Register Watchpoints’ to Give Forwards Data-Flow Analysis 
       [0191]    A modified version of the reverse data-flow analysis described above can be used to track forwards data-flow. As before, if data is in memory a hardware (CPU) watchpoint or instrumentation can be used. However if data is in a register the system operates to run forwards to the next time at which that data is read. This can be done as previously described except that with forwards data-flow analysis the system uses the instrumentation to detect when registers are being read as well as when they are being written. It is still possible to get bifurcation, but this time it happens when the same register is read multiple times without being written to. 
         [0192]    Thus a procedure implemented by embodiments of the system is as follows: Instrument to detect when a particular register is read or written. 
         [0193]    Run forwards from the current position, stopping whenever a target register is read or written, or when the end of execution is reached. 
         [0194]    If the end of execution is reached, the procedure ends. 
         [0195]    If a target register is written, the procedure also ends—the original data that was being tracked has been destroyed. 
         [0196]    Otherwise the system has reached an instruction that reads a target register. The system may then stop (immediately), but there is more information which may be gathered—for example the target register could still be used later. Preferably therefore the system adds information about the current execution time to a data structure (list), and continues. 
         [0197]    After the search has terminated the system can look at each item in the list. Each item will be an instruction that reads from a target register and modifies other registers and/or memory. The system can then use hardware (CPU) watchpoints and/or further register watchpoints to track the data-flow further forward in time. 
         [0198]    No doubt many other effective alternatives will occur to the skilled person. It will be understood that the invention is not limited to the described embodiments and encompasses modifications apparent to those skilled in the art lying within the spirit and scope of the claims appended hereto.