In this document, we use "Visual Etch" to mean the graphical user interface and its environment; we use "Etch" to mean the binary rewriting program that Visual Etch invokes to instrument or optimize programs. The Etch rewriting program can be run without the Visual Etch interface; details of how to do this are contained in the Etching Applications from the Command Line document.
Visual Etch supports four generic functions:
The process of "etching" an application and its DLLs is a complex one, in part because of the task of discovering code in the various binaries, and in part because of the general NT execution environment. Etch is designed to make instrumenting executables straightforward; however, some applications cause particular problems that must be dealt with by the user. See the Etch FAQ for a series of steps that will aid you in analyzing and dealing with problems that arise while using Etch.
While the default project is sufficient for running most experiments,
you may wish to create different project directories to help manage
and organize your experiments. You can use the "Project" menu in the main
Visual Etch window to select or create a different project folder.
If you start Visual Etch from the command line, you can specify a different
default project directory on the command line
(e.g. "vetch d:\myproject").
Projects are an organizational abstraction implemented entirely by the
Visual Etch front-end; therefore, if you run the Etch rewriting engine
-- rather than Visual Etch -- directly from the command line, Etch
will not create this project structure.
Under the "Status Windows" menu at the top of the Running Experiment window,
you can choose two options: console window or debug window. These windows
are selectable as well through the "Options" menu in the main Visual Etch
control window, from which you selected the application and tool.
The debug window is used by the UI for debugging purposes, so it may not be of
interest to you. The console window provides a record of commands executed
and tool output produced during execution.
When you click on "Execute Action", Visual Etch runs the etched
application as the last step of its checklist, as shown
in the Check List subwindow of the Running Experiment window.
In addition to the Execute Action button in the main Visual Etch window,
there are two additional options as indicated by the two adjacent buttons:
When you re-run a specific experiment (a particular tool on a particular
executable, using a particular project directory), Etch replaces
the .output and .log files from previous runs of that experiment.
These files are also erased from the experiment folder
if you select "Options|Clear Cache"
from the UI.
Visual Etch also allows you to save the log and output
files of previous runs to a different directory for later use. In the
Visual Etch control window, use the "Previous Runs" menu and select
"View/Copy Output and Log Files for Selected Tool."
Select a .log or .output file of interest in the Source Files list by
clicking on that file name; double click on the file (or click the View button)
to view it, or move it to the selected Destination Directory by clicking on
"Copy As".
In the project folder, Visual Etch creates a structure of files and folders,
which includes (among other things) the following:
This file contains a log of the actions taken by the Etch runtime as
it intercepts calls from etched or patched modules to the Win32
functions LoadLibrary, GetModuleHandle, and GetModuleFileName. The
log includes the name of the DLL requested by the application and the
name of the etched or patched DLL loaded in its place. The
information in etchwrap.log can be helpful in diagnosing problems
loading etched or patched modules at runtime (see Section 3.3 for a description of patching).
Visual Etch uses a number of checks to ensure that cached versions
of etched modules are up to date. First,
it compares the modification time of the cached modules with the
modification time of the source binaries, to ensure
that the cached modules are newer than source binaries. Second,
it checks to make sure that the cached binaries were produced with
the same arguments to Etch as required by the new command.
In the Visual Etch control panel, you can select the "Options|Clear
Cache|All" menu item if you wish to delete cached versions of etched
modules in the current project, either to save disk space, or to
guarantee that the entire application is freshly etched.
You should not clear the cache while an
Etch experiment is in progress.
Specifying command-line arguments is straightforward: the user can
simply type them into the "Application Command Line Options" line
in the main Visual Etch Window.
To specify that Visual Etch should not run the application directly,
but instead should use a user-provided script, you should check
the "Use Script to Drive Application" box. You can then use the
"Script..." button in the Visual Etch command window
to browse the file system for the script. If command-line arguments
are specified and the "Use Script.." box is checked, the arguments
are passed to the script.
The name of the etched application is communicated from Visual Etch to
the script via the "NEWEXENAME" environment variable. In some cases
"NEWEXENAME" will include the name of an auxiliary program such as
DLLwatch that must be invoked to run the application. Thus, typical
values for "NEWEXENAME" when instrumenting the program "foo.exe" would
be "foo-etch.exe" or "dllwatch foo.exe".
In summary, a trivial perl script -- a script that simply invokes the
etched program without performing any additional tasks -- would be:
#!/usr/local/bin/perl
The DLLwatch tool monitors and summarizes calls to LoadLibrary
and other routines that load or manipulate DLLs.
DLLwatch imposes negligible overhead. You should first run the application
using the DLLwatch tool in exactly the environment you will use
for your experiments, in order to ensure that the full
set of explicitly-loaded DLLs are discovered.
After you run DLLwatch, DLLwatch automatically adds the DLLs
it found to the Modules list.
In general all of the DLLs will be identified by DLLwatch, but it is
still possible that some DLLs will not be found. In particular, if
the DLLs to be loaded are data or input specific, and the application
is later run with different data or input, then DLLwatch may not catch
those DLLs. See Section 3.4 for more
information on this problem.
For each module shown by the Modules window, the interface allows the
user to set the parameters used to etch the module. Typically these
parameters can be left at the default settings. (The default settings
are stored in tables in the MS Access file etch\bin\params.mdb:
global defaults live in the table "Etch", while per-module defaults live in
the table "Module EtchFlags".) If you wish to change the parameter settings,
the following options are available:
Etch - This controls whether the
transformation specified in the "Action" menu is applied to the given
module (i.e., whether to etch the module with the given tool). This
option is on by default except for DLLs that can
fail when instrumented. These DLLs are noted with an asterisk;
clicking on the DLL will cause a brief description of the problem with
the DLL to appear at the bottom of the window. Some more information
is provided in "DLL Limitations"
(Subsection 3.5) below.
Patch - This option indicates that whether or not a
module is etched, its import table should be updated to import
etched DLLs rather than the original DLLs. As with the Etch option,
this option is on by default except for DLLs that are known to
fail, even if simply patched. This option should only be used
if it is necessary to instruct Etch to ignore the presence
of the particular module for the etched application to run correctly.
Aggressive Code Discovery - On by default, this option controls
the aggressiveness of the rules that Etch uses to distinguish
code from data. In general, we have found that misidentification of
data as code is rare, so it is not necessary to use this option.
Fast Context Save - Off by default, turning on this option
indicates that the context save code surrounding procedure calls from
the etched application to the tool runtime DLL does not need to
preserve data above the top of the stack. A small number of DLLs keep
live data above the top of the stack: the default "slow context save"
code path preserves this data on entry and exit to the tool runtime
code. For DLLs that have been determined not to have this deprecated
behavior, it is possible to speed up
experiments somewhat by choosing fast context save.
(You can use the "Check Data References" tool to
monitor runtime stack references and determine whether an application
has this stack problem.) FastContextSave can also reduce disk space and memory usage.
Save Floating Point State - Off by default, this option
controls whether floating point is saved and restored on calls into
the tool runtime. Specifying this option will add considerable
overhead to these calls, and is only necessary when both the tool and
the DLL being transformed use floating-point operations.
Use Original Image Base - Off by default except
for USER32.DLL, this option indicates that the etched or patched image
file should be linked at the same image base as the input image. The
default behavior is for Etch to choose a new image base automatically.
Currently all etched and patched DLLs are simply linked to a single
image base. This strategy prevents etched DLLs from being linked at
addresses that conflict with the addresses of some system DLLs.
USER32.DLL is handled differently, because the kernel effectively has
hardwired entry points into this DLL that assume that the image base
is fixed. So far we have not identified any other DLLs with this
behavior.
Use Original Module Name - Off by default, this option indicates
that the etched module should be given the same name as the original
module. Normally the etched or patched version of a module with the
name "application.exe" would be named "application-etch.exe" or
"application-patch.exe", respectively. This option is needed for
programs that look for data files based on the application name,
and will not find the files if the application name changes. It is
strongly recommended that this option not be enabled for etched or
patched DLLs, because it can lead to unetched applications using
the modified DLLs, or the etched application using the unmodified DLLs.
An Etch runtime library (etchwrap.dll) intercepts calls to
LoadLibrary and GetModuleHandle and redirects references
to the transformed version. If etchwrap.dll detects a
reference to a module that was not Etched, it pops up a
dialog box and asks the user how to proceed. The safest course is to
terminate the application. A slightly less conservative approach is to
return a failure code to the caller and hope for the best. Finally,
the user can choose to load the original library, although this may
result in unetched versions of various system DLLs
being mapped. When unetched or unpatched modules are identified, the runtime sytem
writes entries in the file etchwrap-orphans.log identifying those
modules, so that the modules can be etched or patched in future runs.
The tools fall into several categories.
2. Running Visual Etch
This section describes the Visual Etch interface used to run Etch
experiments.
2.1 How To Start Visual Etch
You can start Visual Etch either using the File Explorer or through
a command window. Through the File Explorer, go to the Etch distribution
directory, open the bin folder, and double click on vetch.exe
to execute Visual Etch. Through a command window, cd to the bin directory and
type "vetch".2.2 Choosing a Project
Visual Etch uses projects as its primary abstraction for
organizing the work that a user does. A project is a directory
used to hold a variety of state about an experiment or set of
experiments using Etch. The following information is stored in a
project: (1) the executable name of the last etched object, (2) all
the DLL modules referenced by the executable, (3) a set of flags
associated with each DLL indicating how it should be etched, and (4) a
cache of etched objects (.exe and DLL files).
When you start up Visual Etch, it creates a default project area for
you to work in. The default project directory is named
DefaultProject.vpr-DIR, and is created in folder
%USERNAME%.temp within the temporary directory set by the
environment variable %TMP%. For example, if your temporary
directory is "Temp" and your username is "Fred," Visual Etch creates
the project directory
\Temp\Fred.temp\DefaultProject.vpr-DIR. Along with the
DefaultProject.vpr-DIR folder in the
%USERNAME%.temp folder, Visual Etch creates a file,
DefaultProject.vpr, containing parameters describing the
project.2.3 Choosing an Application
After you've set the project directory, the next step is to choose an
application that you want to re-write with Etch. First, click on
the "Application..." button in the upper-left corner of the Visual Etch
window. A file dialog box will appear, and you can navigate through
the file system to select the executable you wish to etch.
Select the executable using the "Open" button or
doubleclick on the executable.
2.4 Choosing a Tool
The window labeled "Action" that is part of the top-level Visual Etch
window contains a list of the available tools. Clicking on a label in
the Action window gives a one-line description of the selected tool at
the bottom left of the main Visual Etch window. Once you've decided
which tool you want to use and you've set the appropriate settings
using the "Application..." button, you are ready to invoke the
selected action on the application and modules that you've chosen,
i.e., to run the Etch experiment.
For applications using dynamically-loaded libraries (DLLs), you will
typically need to invoke the DLLwatch tool on the program
before any other action; DLLwatch determines the set of dynamically-loaded
libraries that are used by the application and adds them to the list of
modules to be processed by Etch. This is
described in detail in Section 2.2 and
Section 2.3; or, see the example experiment
walk-through in Section 2.8.
2.5 Executing an Action
Once you've selected an application and tool, you run the experiment by
clicking on "Execute Action." When you click "Execute Action,"
a window "Visual Etch: Running Experiment" appears.
The information panel at the top of the window describes the application
and experiment, and the progress bar tells (loosely) how much of the experiment is
done. Within the window are three subwindows that give status
information about the Etch experiment being performed.
At the top, the Check List window gives you an overview of the steps needed to
perform the experiment; this usually includes one or more runs of
Etch on the application executable and DLLs, running of the etched
application executable, and postprocessing of the data output produced by
the tool, if any. Each step is highlighted as it is carried out.
The middle window, titled "Run Details," gives more precise messages
about the progress of the various steps being executed.
The bottom "Problems" window gives a summary of problems,
if any, that occurred while the experiment was running.
2.6 Examining the Results of Experiments
The final step in the checklist for each tool is typically
a postprocessing of the output produced by the tool. When this step
is initiated, Visual Etch displays the output produced. For viewing
output results, Visual Etch supports tables, graphs, and text
output windows. Most experiments produce
a file called "tool.output" in the experiment folder of the
cache directory; here "tool" is the name
of the tool you invoked, e.g., "ophisto.output" for
the histogram tool, or "countiref.output" for the instruction
count tool. (Selecting
"Options|Command Prompt|In Experiment Folder" in the control window will give you
a command window set to the experiment folder.)
The experiment scripts also produce a log that
describes how the program modules were etched and any errors or warnings that
occurred while etching. This log is stored in
the file etch.log in the experiment folder. You can see this log
by selecting "Show Log File for Selected Tool" in the "Previous Runs" menu
in the Visual Etch control window. 2.7 Files Used and Created by Visual Etch
As noted above, Visual Etch creates a default project directory
(DefaultProject.vpr-DIR) in the temp directory. Users can create new
project directories in other locations by using the "Open Project"
menu item.
2.8 Walk-through of an Etch Experiment
In this section we walk through an experiment using Visual Etch to give you
the experience of the process you will typically go through when instrumenting
applications. Let's assume that you wish to make some simple
measurements of the application calc.exe, a visual desk calculator. In
particular, assume that you wish to do an opcode histogram of the application as
it is used. The steps below show how to perform this experiment.
Note that DLLwatch needs to be run only once for this set of experiments. If we
wish to run other tools on the same executable using the same project file, we
do not need to re-run DLLwatch.
2.9 The Visual Etch Cache
To facilitate running an etched binary multiple times, Visual Etch
implements a caching mechanism; that is, previously-etched binaries
are cached in files stored in the experiment folders (e.g.,
Project.vpr-DIR\cache\ApplName.exe-tool for the default
project). 2.10 Running Applications with Scripts and Command-Line Arguments
Some applications need to be invoked with special command-line
arguments or in a specific environment. To support this, Visual Etch
allows the user to specify a script that should be used to run the
etched application; as well, the user can specify command-line arguments that
should be passed to the application or to the script.Writing a Script
Consider a script whose task is to run the program foo.exe.
The script is responsible for setting up the environment needed by
foo.exe to run properly (generating input files, setting the path,
etc.) and to invoke the newly-etched version of foo.exe (rather than
the original program).
Special Cases: Perl and Microsoft Test
Visual Etch provides special support for two common cases, perl
scripts and Microsoft Test scripts. If
the script has the extension ".pl", Visual
Etch invokes the script via perl;
if the script has the extension ".mst", Visual Etch invokes the script
via Microsoft Test.2.11 Computer Parameters for Cache Simulation
The "Options" menu in the Visual Etch UI contains a menu item called
"computer parameters." Selecting this item brings up a form in which
you can define processor configuration parameters, including processor
speed, cache size and associativity, TLB size and associativity, etc.
You can easily initialize the values for those for various standard
platforms by selecting one of those platforms from the top menu item,
or else enter or modify parameters individually. The values entered
do not necessarily represent the hardware on which Etch is executing;
the purpose of this menu is simply to provide an interface for Etch
users to define hardware parameters for tools performing hardware
(e.g., cache or TLB) simulation. Visual Etch passes these parameters
to the simulation tools in the project.txt file in the project directory; Visual
Etch also places a copy of project.txt in the experiment directory for
use as diagnostic information. Otherwise, neither Visual Etch nor Etch
uses this information directly.
2.12 Selective Etching of Procedures
If debug information is available in any modules, Visual Etch allows you to
selectively choose the specific procedures to be etched in those
modules. In the main Visual Etch window, choose "Options|Selective
Etching" to open the selective etching control window. Within that
window, check "Use Selective Etching" to enable selective etching of
procedures. The list shows the procedures identified; Etch will
process procedures with a "+" in front of their names and will not
process procedures with a "-" in front of their names.
2.13 Rebasing Etched Modules
Loading an application is often slow, because the loader must
relocate all of the modules so that they occupy unique address
ranges. The Visual Etch option "Options|Rebase Etched Modules"
performs this function ahead of time, so that loading occurs
more rapidly. This is useful if an etched executable and its
associated modules will be run multiple times. If the etched executable
will not be run multiple times, it is probably not worth the overhead
of relocating the modules in advance. (This function requires
editbin.exe to be in the path.)
3. Dynamically Loaded Libraries (DLLs)
An important
job of Visual Etch is to identify the set of DLLs used by an
application, rewrite them, and arrange that the new versions of the
DLLs are used by the rewritten version of the application. This section
gives a brief introduction to DLLs on NT and Win95 and then describes the
strategy used by Etch for dealing with DLLs correctly.3.1 DLLs on NT
The DLLs used by a given NT application can be divided into two
categories: "implicitly-loaded" DLLs and "explicitly-loaded" DLLs.
Sometimes these are called "statically" and "dynamically" loaded DLLs.
Implicitly-loaded DLLs are loaded automatically during process
initialization, because they are referenced in the executable's import
table. Explicitly-loaded DLLs are loaded by the application after process
initialization through the Win32 LoadLibrary call.3.2 DLLwatch
To instrument a program correctly, Etch needs to know about
all the dynamically-loaded libraries used by the application. This is
necessary for two reasons: to capture additional activity caused by the
application, and to prevent NT from loading both the instrumented and the
original DLL at the same time. The implicitly-loaded DLLs are
discovered by a recursive scan of the import table, however,
discovering explicitly-loaded DLLs is more difficult. Etch therefore
provides a tool, "DLLwatch", to help you identify the set of
explicitly-loaded DLLs used by the application. If you know the
entire set of DLLs then you can enter them directly using the Modules
dialog window (described below).3.3 The Modules Window
To facilitate control of which modules are etched and how they are
etched, Visual Etch provides the Modules window. To see the Modules
window, click on the "Modules..." button in the main Visual Etch
window. The Modules window shows one line for the target executable
and one line for each of the DLLs known to Visual Etch. Initially,
this list includes only the set of implicitly-loaded DLLs (these were
discovered by Etch automatically when you selected the application).
The "options" menu in the Modules window provides entries to add and remove explicitly-loaded
DLLs from the list. You can add explicitly-loaded DLLs one at a time
by browsing the file system. However, typically you will add them en
masse by running DLLwatch, which automatically adds the DLLs it
finds, when you respond positively to its dialog box following the experiment.
DLLs whose names are shown in upper case letters are
explicitly-loaded DLLs, while those shown in lower case are
implicitly-loaded DLLs. A "*" preceding the name of a DLL indicates a
DLL that is not normally etched.3.4 What Can Go Wrong
If the full set of DLLs used by an application is not specified at
Etch time, it is possible that the etched application will try to load
unetched or unpatched versions of DLLs as it runs. This could result
in both an original and transformed version of a DLL being mapped
into the process address space, leading to unpredictable behavior.3.5 Current DLL Limitations and Special Cases
The primary DLLs that are by default not etched in the current
implementation are NTDLL.DLL, KERNEL32.DLL, and USER32.DLL. NTDLL.DLL
and KERNEL32.DLL are not modified at all, while USER32.DLL is patched
but not etched. Code in these DLLs is not
instrumented and therefore not measured. (Note: It appears that etching
KERNEL32.DLL will work, but we have not done enough testing to warrant
enabling by default.) 4. Etch Tools
Etch tools direct the rewriting, for instrumentation or optimization,
of application binaries. Etch is designed and structured to make it simple to write and
use such tools. As part of the Etch distribution, we provide a
set of standard tools that can be used for measurement and optimization.
These tools are briefly described below and are listed in the "Action"
window of the main Visual Etch interface window.4.1 Etch support tools
4.2 Stable tools
4.3 Optimization Tools
4.4 Tools That Require Debug Information
4.5 Cutting Edge Tools
4.6 Etch Debug Tools