<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> The runtime system (ocamlrun) </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual023.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual025.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc110"><B><FONT SIZE=6>Chapter 10</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>The runtime system (ocamlrun)</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="c:runtime"></A> <BR> The <TT>ocamlrun</TT> command executes bytecode files produced by the linking phase of the <TT>ocamlc</TT> command.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc111"><B><FONT SIZE=5>10.1</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Overview</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> The <TT>ocamlrun</TT> command comprises three main parts: the bytecode interpreter, that actually executes bytecode files; the memory allocator and garbage collector; and a set of C functions that implement primitive operations such as input/output.<BR> <BR> The usage for <TT>ocamlrun</TT> is: <PRE> ocamlrun <I>options</I> <I>bytecode-executable</I> <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> ... <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB> </PRE> The first non-option argument is taken to be the name of the file containing the executable bytecode. (That file is searched in the executable path as well as in the current directory.) The remaining arguments are passed to the Caml program, in the string array <TT>Sys.argv</TT>. Element 0 of this array is the name of the bytecode executable file; elements 1 to <I>n</I> are the remaining arguments <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> to <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB>.<BR> <BR> As mentioned in chapter <A HREF="manual022.html#c:camlc">8</A>, the bytecode executable files produced by the <TT>ocamlc</TT> command are self-executable, and manage to launch the <TT>ocamlrun</TT> command on themselves automatically. That is, assuming <TT>caml.out</TT> is a bytecode executable file, <PRE> caml.out <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> ... <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB> </PRE> works exactly as <PRE> ocamlrun caml.out <I>arg</I><SUB><FONT SIZE=2>1</FONT></SUB> ... <I>arg</I><SUB><FONT SIZE=2><I>n</I></FONT></SUB> </PRE> Notice that it is not possible to pass options to <TT>ocamlrun</TT> when invoking <TT>caml.out</TT> directly.<BR> <BR> <FONT COLOR=purple>Windows:</FONT> <BLOCKQUOTE> Under several versions of Windows, bytecode executable files are self-executable only if their name ends in <TT>.exe</TT>. It is recommended to always give <TT>.exe</TT> names to bytecode executables, e.g. compile with <TT>ocamlc -o myprog.exe ...</TT> rather than <TT>ocamlc -o myprog ...</TT>. </BLOCKQUOTE> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc112"><B><FONT SIZE=5>10.2</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Options</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="ocamlrun-options"></A><BR> The following command-line options are recognized by <TT>ocamlrun</TT>. <DL COMPACT=compact><DT><B><TT>-b</TT></B><DD> When the program aborts due to an uncaught exception, print a detailed ``back trace'' of the execution, showing where the exception was raised and which function calls were outstanding at this point. The back trace is printed only if the bytecode executable contains debugging information, i.e. was compiled and linked with the <TT>-g</TT> option to <TT>ocamlc</TT> set. This is equivalent to setting the <TT>b</TT> flag in the <TT>OCAMLRUNPARAM</TT> environment variable (see below). <DT><B><TT>-I</TT> <I>dir</I></B><DD> Search the directory <I>dir</I> for dynamically-loaded libraries, in addition to the standard search path (see section <A HREF="#s-ocamlrun-dllpath">10.3</A>). <DT><B><TT>-v</TT></B><DD> Direct the memory manager to print some progress messages on standard error. This is equivalent to setting <TT>v=63</TT> in the <TT>OCAMLRUNPARAM</TT> environment variable (see below).</DL> The following environment variables are also consulted: <DL COMPACT=compact><DT> <B><TT>CAML_LD_LIBRARY_PATH</TT></B><DD> Additional directories to search for dynamically-loaded libraries (see section <A HREF="#s-ocamlrun-dllpath">10.3</A>).<BR> <BR> <DT><B><TT>OCAMLLIB</TT></B><DD> The directory containing the Objective Caml standard library. (If <TT>OCAMLLIB</TT> is not set, <TT>CAMLLIB</TT> will be used instead.) Used to locate the <TT>ld.conf</TT> configuration file for dynamic loading (see section <A HREF="#s-ocamlrun-dllpath">10.3</A>). If not set, default to the library directory specified when compiling Objective Caml.<BR> <BR> <DT><B><TT>OCAMLRUNPARAM</TT></B><DD> Set the runtime system options and garbage collection parameters. (If <TT>OCAMLRUNPARAM</TT> is not set, <TT>CAMLRUNPARAM</TT> will be used instead.) This variable must be a sequence of parameter specifications. A parameter specification is an option letter followed by an <TT>=</TT> sign, a decimal number, and an optional multiplier. There are nine options, six of which correspond to the fields of the <TT>control</TT> record documented in <a HREF=libref/Gc.html>Module <tt>Gc</tt></a>. <DL COMPACT=compact><DT> <B>b</B><DD> (backtrace) Trigger the printing of a stack backtrace when an uncaught exception aborts the program. This option takes no argument. <DT><B>p</B><DD> (parser trace) Turn on debugging support for <TT>ocamlyacc</TT>-generated parsers. When this option is on, the pushdown automaton that executes the parsers prints a trace of its actions. This option takes no argument. <DT><B>s</B><DD> (<TT>minor_heap_size</TT>) Size of the minor heap. <DT><B>i</B><DD> (<TT>major_heap_increment</TT>) Minimum size increment for the major heap. <DT><B>o</B><DD> (<TT>space_overhead</TT>) The major GC speed setting. <DT><B>O</B><DD> (<TT>max_overhead</TT>) The heap compaction trigger setting. <DT><B>v</B><DD> (<TT>verbose</TT>) What GC messages to print to stderr. This is a sum of values selected from the following: <DL COMPACT=compact><DT> <B>1 (= 0b0000000001)</B><DD> Start of major GC cycle. <DT><B>2 (= 0b0000000010)</B><DD> Minor collection and major GC slice. <DT><B>4 (= 0b0000000100)</B><DD> Growing and shrinking of the heap. <DT><B>8 (= 0b0000001000)</B><DD> Resizing of stacks and memory manager tables. <DT><B>16 (= 0b0000010000)</B><DD> Heap compaction. <DT><B>32 (= 0b0000100000)</B><DD> Change of GC parameters. <DT><B>64 (= 0b0001000000)</B><DD> Computation of major GC slice size. <DT><B>128 (= 0b0010000000)</B><DD> Calling of finalization functions <DT><B>256 (= 0b0100000000)</B><DD> Startup messages (loading the bytecode executable file, resolving shared libraries). </DL> <DT><B>l</B><DD> (<TT>stack_limit</TT>) The limit (in words) of the stack size. <DT><B>h</B><DD> The initial size of the major heap (in words). </DL> The multiplier is <TT>k</TT>, <TT>M</TT>, or <TT>G</TT>, for multiplication by 2<SUP><FONT SIZE=2>10</FONT></SUP>, 2<SUP><FONT SIZE=2>20</FONT></SUP>, and 2<SUP><FONT SIZE=2>30</FONT></SUP> respectively. For example, on a 32-bit machine, under <TT>bash</TT> the command <PRE> export OCAMLRUNPARAM='b,s=256k,v=1' </PRE> tells a subsequent <TT>ocamlrun</TT> to print backtraces for uncaught exceptions, set its initial minor heap size to 1 megabyte and print a message at the start of each major GC cycle.<BR> <BR> <DT><B><TT>PATH</TT></B><DD> List of directories searched to find the bytecode executable file. </DL> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc113"><B><FONT SIZE=5>10.3</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Dynamic loading of shared libraries</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="s-ocamlrun-dllpath"></A><BR> On platforms that support dynamic loading, <TT>ocamlrun</TT> can link dynamically with C shared libraries (DLLs) providing additional C primitives beyond those provided by the standard runtime system. The names for these libraries are provided at link time as described in section <A HREF="manual032.html#dynlink-c-code">18.1.4</A>), and recorded in the bytecode executable file; <TT>ocamlrun</TT>, then, locates these libraries and resolves references to their primitives when the bytecode executable program starts.<BR> <BR> The <TT>ocamlrun</TT> command searches shared libraries in the following directories, in the order indicated: <OL type=1><LI> Directories specified on the <TT>ocamlrun</TT> command line with the <TT>-I</TT> option. <LI>Directories specified in the <TT>CAML_LD_LIBRARY_PATH</TT> environment variable. <LI>Directories specified at link-time via the <TT>-dllpath</TT> option to <TT>ocamlc</TT>. (These directories are recorded in the bytecode executable file.) <LI>Directories specified in the file <TT>ld.conf</TT>. This file resides in the Objective Caml standard library directory, and lists directory names (one per line) to be searched. Typically, it contains only one line naming the <TT>stublibs</TT> subdirectory of the Objective Caml standard library directory. Users can add there the names of other directories containing frequently-used shared libraries; however, for consistency of installation, we recommend that shared libraries are installed directly in the system <TT>stublibs</TT> directory, rather than adding lines to the <TT>ld.conf</TT> file. <LI>Default directories searched by the system dynamic loader. Under Unix, these generally include <TT>/lib</TT> and <TT>/usr/lib</TT>, plus the directories listed in the file <TT>/etc/ld.so.conf</TT> and the environment variable <TT>LD_LIBRARY_PATH</TT>. Under Windows, these include the Windows system directories, plus the directories listed in the <TT>PATH</TT> environment variable. </OL> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc114"><B><FONT SIZE=5>10.4</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Common errors</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE><BR> This section describes and explains the most frequently encountered error messages. <DL COMPACT=compact><DT><B><I>filename</I><TT>: no such file or directory</TT></B><DD> If <I>filename</I> is the name of a self-executable bytecode file, this means that either that file does not exist, or that it failed to run the <TT>ocamlrun</TT> bytecode interpreter on itself. The second possibility indicates that Objective Caml has not been properly installed on your system.<BR> <BR> <DT><B><TT>Cannot exec ocamlrun</TT></B><DD> (When launching a self-executable bytecode file.) The <TT>ocamlrun</TT> could not be found in the executable path. Check that Objective Caml has been properly installed on your system.<BR> <BR> <DT><B><TT>Cannot find the bytecode file</TT></B><DD> The file that <TT>ocamlrun</TT> is trying to execute (e.g. the file given as first non-option argument to <TT>ocamlrun</TT>) either does not exist, or is not a valid executable bytecode file.<BR> <BR> <DT><B><TT>Truncated bytecode file</TT></B><DD> The file that <TT>ocamlrun</TT> is trying to execute is not a valid executable bytecode file. Probably it has been truncated or mangled since created. Erase and rebuild it.<BR> <BR> <DT><B><TT>Uncaught exception</TT></B><DD> The program being executed contains a ``stray'' exception. That is, it raises an exception at some point, and this exception is never caught. This causes immediate termination of the program. The name of the exception is printed, along with its string and integer arguments (arguments of more complex types are not correctly printed). To locate the context of the uncaught exception, compile the program with the <TT>-g</TT> option and either run it again under the <TT>ocamldebug</TT> debugger (see chapter <A HREF="manual030.html#c:debugger">16</A>), or run it with <TT>ocamlrun -b</TT> or with the <TT>OCAMLRUNPARAM</TT> environment variable set to <TT>b=1</TT>.<BR> <BR> <DT><B><TT>Out of memory</TT></B><DD> The program being executed requires more memory than available. Either the program builds excessively large data structures; or the program contains too many nested function calls, and the stack overflows. In some cases, your program is perfectly correct, it just requires more memory than your machine provides. In other cases, the ``out of memory'' message reveals an error in your program: non-terminating recursive function, allocation of an excessively large array or string, attempts to build an infinite list or other data structure, ...<BR> <BR> To help you diagnose this error, run your program with the <TT>-v</TT> option to <TT>ocamlrun</TT>, or with the <TT>OCAMLRUNPARAM</TT> environment variable set to <TT>v=63</TT>. If it displays lots of ``<TT>Growing stack</TT>...'' messages, this is probably a looping recursive function. If it displays lots of ``<TT>Growing heap</TT>...'' messages, with the heap size growing slowly, this is probably an attempt to construct a data structure with too many (infinitely many?) cells. If it displays few ``<TT>Growing heap</TT>...'' messages, but with a huge increment in the heap size, this is probably an attempt to build an excessively large array or string.</DL> <HR> <A HREF="manual023.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual025.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>