diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 14:37:58 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 14:37:58 -0200 |
commit | f4d67761f1ee3de81ae720703011f5c03f1bfb47 (patch) | |
tree | a8bd8d450b18446f3be3eb9f57cacc4d875822b2 | |
parent | 369c5fe3c056f80dc29a59a21535d2984c880b24 (diff) | |
download | lua-f4d67761f1ee3de81ae720703011f5c03f1bfb47.tar.gz lua-f4d67761f1ee3de81ae720703011f5c03f1bfb47.tar.bz2 lua-f4d67761f1ee3de81ae720703011f5c03f1bfb47.zip |
new section describing the debug interface.
-rw-r--r-- | manual.tex | 151 |
1 files changed, 142 insertions, 9 deletions
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.5 1996/02/05 14:52:47 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.6 1996/02/05 21:32:19 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentstyle[A4,11pt,bnf]{article} | 3 | \documentstyle[A4,11pt,bnf]{article} |
4 | 4 | ||
@@ -32,11 +32,10 @@ Waldemar Celes Filho | |||
32 | Departamento de Inform\'atica --- PUC-Rio | 32 | Departamento de Inform\'atica --- PUC-Rio |
33 | } | 33 | } |
34 | 34 | ||
35 | \date{\small \verb$Date: 1996/02/05 14:52:47 $} | 35 | \date{\small \verb$Date: 1996/02/05 21:32:19 $} |
36 | 36 | ||
37 | \maketitle | 37 | \maketitle |
38 | 38 | ||
39 | |||
40 | \begin{abstract} | 39 | \begin{abstract} |
41 | \noindent | 40 | \noindent |
42 | Lua is an extension programming language designed to be used | 41 | Lua is an extension programming language designed to be used |
@@ -572,7 +571,7 @@ into the variable \verb'var'. | |||
572 | Parameters act as local variables, | 571 | Parameters act as local variables, |
573 | initialized with the argument values. | 572 | initialized with the argument values. |
574 | \begin{Produc} | 573 | \begin{Produc} |
575 | \produc{parlist1}{'name' \rep{\ter{,} name}} | 574 | \produc{parlist1}{name \rep{\ter{,} name}} |
576 | \end{Produc} | 575 | \end{Produc} |
577 | 576 | ||
578 | Results are returned using the \verb'return' statement (\see{return}). | 577 | Results are returned using the \verb'return' statement (\see{return}). |
@@ -704,11 +703,12 @@ is terminated returning an error condition. | |||
704 | The only argument to the error fallback function is a string describing | 703 | The only argument to the error fallback function is a string describing |
705 | the error. | 704 | the error. |
706 | The standard I/O library redefines this fallback, | 705 | The standard I/O library redefines this fallback, |
707 | using the debug API, in order to print some extra informations, | 706 | using the debug facilities (\see{debugI}, |
707 | in order to print some extra informations, | ||
708 | like the stack of calls. | 708 | like the stack of calls. |
709 | For more information about an error, | 709 | For more information about an error, |
710 | the Lua program can include the compilation pragma \verb'$debug'. | 710 | the Lua program can include the compilation pragma \verb'$debug'. |
711 | \index{debug pragma} | 711 | \index{debug pragma}\label{pragma} |
712 | This pragma must be written in a line by itself. | 712 | This pragma must be written in a line by itself. |
713 | When an error occurs in a program compiled with this option, | 713 | When an error occurs in a program compiled with this option, |
714 | the error routine is able to print also the lines where the calls | 714 | the error routine is able to print also the lines where the calls |
@@ -1454,6 +1454,135 @@ hours ([0-23]), minutes, and seconds. | |||
1454 | % This function then returns and the execution of the program continues. | 1454 | % This function then returns and the execution of the program continues. |
1455 | 1455 | ||
1456 | 1456 | ||
1457 | |||
1458 | \section{The Debuger Interface} \label{debugI} | ||
1459 | |||
1460 | Lua has no built in debuger facilities. | ||
1461 | Instead, it offers a special interface, | ||
1462 | by means of functions and {\em hooks}, | ||
1463 | which allows the construction of different | ||
1464 | kinds of debugers, profiles, and other tools | ||
1465 | that need ``inside'' information from the interpreter. | ||
1466 | This interface is declared in the file \verb'luadebug.h'. | ||
1467 | |||
1468 | \subsection{Stack and Function Information} | ||
1469 | |||
1470 | The main function to get information about the interpreter stack | ||
1471 | is | ||
1472 | \begin{verbatim} | ||
1473 | lua_Function lua_stackedfunction (int level); | ||
1474 | \end{verbatim} | ||
1475 | It returns a handle (\verb'lua_Function') to the {\em activation record} | ||
1476 | of the function executing at a given level. | ||
1477 | Level 0 is the current running function, | ||
1478 | while level $n+1$ is the function that has called level $n$. | ||
1479 | When called with a level greater than the stack depth, | ||
1480 | \verb'lua_stackedfunction' returns \verb'LUA_NOOBJECT'. | ||
1481 | |||
1482 | The type \verb'lua_Function' is just another name | ||
1483 | to \verb'lua_Object'. | ||
1484 | Although, in this library, | ||
1485 | a \verb'lua_Function' can be used wherever a \verb'lua_Object' is required, | ||
1486 | a parameter \verb'lua_Function' accepts only a handle returned by | ||
1487 | \verb'lua_stackedfunction'. | ||
1488 | |||
1489 | Three other functions produce extra information about a function: | ||
1490 | \begin{verbatim} | ||
1491 | void lua_funcinfo (lua_Object func, char **filename, int *linedefined); | ||
1492 | int lua_currentline (lua_Function func); | ||
1493 | char *lua_getobjname (lua_Object o, char **name); | ||
1494 | \end{verbatim} | ||
1495 | \verb'lua_funcinfo' gives the file name and the line where the | ||
1496 | given function has been defined. | ||
1497 | If the ``function'' is in fact the main code of a chunk, | ||
1498 | \verb'linedefined' is 0. | ||
1499 | If the function is a C function, | ||
1500 | \verb'linedefined' is -1, and \verb'filename' is \verb'"(C)"'. | ||
1501 | |||
1502 | The function \verb'lua_currentline' gives the current line where | ||
1503 | a given function is executing. | ||
1504 | It only works if the function has been pre-compiled with debug | ||
1505 | information (\see{pragma}). | ||
1506 | When no line information is available, it returns -1. | ||
1507 | |||
1508 | Function \verb'lua_getobjname' tries to find a reasonable name for | ||
1509 | a given function. | ||
1510 | Because functions in Lua are first class values, | ||
1511 | they do not have a fixed name. | ||
1512 | Some functions may be the value of many global variables, | ||
1513 | while others may be stored only in a table field. | ||
1514 | Function \verb'lua_getobjname' first checks whether the given | ||
1515 | function is a fallback. | ||
1516 | If so, it returns the string \verb'"fallback"', | ||
1517 | and \verb'name' is set to point to the fallback name. | ||
1518 | Otherwise, if the given function is the value of a global variable, | ||
1519 | \verb'lua_getobjname' returns the string \verb'"global"', | ||
1520 | while \verb'name' points to the variable name. | ||
1521 | If the given function is neither a fallback nor a global variable, | ||
1522 | \verb'lua_getobjname' returns the empty string, | ||
1523 | and \verb'name' is set to \verb'NULL'. | ||
1524 | |||
1525 | \subsection{Manipulating Local Variables} | ||
1526 | |||
1527 | The following functions allow the manipulation of the | ||
1528 | local variables of a given activation record. | ||
1529 | They only work if the function has been pre-compiled with debug | ||
1530 | information (\see{pragma}). | ||
1531 | \begin{verbatim} | ||
1532 | lua_Object lua_getlocal (lua_Function func, int local_number, char **name); | ||
1533 | int lua_setlocal (lua_Function func, int local_number); | ||
1534 | \end{verbatim} | ||
1535 | The first one returns the value of a local variable, | ||
1536 | and sets \verb'name' to point to the variable name. | ||
1537 | \verb'local_number' is an index for local variables. | ||
1538 | The first parameter has index 1, and so on, until the | ||
1539 | last active local variable. | ||
1540 | When called with a \verb'local_number' greater than the | ||
1541 | number of active local variables, | ||
1542 | or if the activation record has no debug information, | ||
1543 | \verb'lua_getlocal' returns \verb'LUA_NOOBJECT'. | ||
1544 | |||
1545 | The function \verb'lua_setlocal' sets the local variable | ||
1546 | \verb'local_number' to the value previously pushed on the stack | ||
1547 | (\see{valuesCLua}). | ||
1548 | If the function succeeds it returns 1. | ||
1549 | If \verb'local_number' is greater than the number | ||
1550 | of active local variables, | ||
1551 | or if the activation record has no debug information, | ||
1552 | this function fails and returns 0. | ||
1553 | |||
1554 | \subsection{Hooks} | ||
1555 | |||
1556 | The Lua interpreter offers two hooks for debug purposes: | ||
1557 | \begin{verbatim} | ||
1558 | typedef void (*lua_CHFunction) (lua_Function func, char *file, int line); | ||
1559 | typedef void (*lua_LHFunction) (int line); | ||
1560 | \end{verbatim} | ||
1561 | The first one is called whenever the interpreter enters or leaves a | ||
1562 | function. | ||
1563 | When entering a function, | ||
1564 | its parameters are a handle to the function activation record, | ||
1565 | plus the file and the line where the function is defined (the same | ||
1566 | information which is provided by \verb'lua_funcinfo'); | ||
1567 | when leaving a function, \verb'func' is \verb'LUA_NOOBJECT', | ||
1568 | \verb'file' is \verb'"(return)"', and \verb'line' is 0. | ||
1569 | |||
1570 | The other hook is called every time the interpreter changes | ||
1571 | the line of code it is executing. | ||
1572 | Its only parameter is the line number | ||
1573 | (the same information which is provided by the call | ||
1574 | \verb'lua_currentline(lua_stackedfunction(0))'). | ||
1575 | This second hook is only called if the active function | ||
1576 | has been pre-compiled with debug information (\see{pragma}). | ||
1577 | |||
1578 | To set these hooks, there are the functions: | ||
1579 | \begin{verbatim} | ||
1580 | lua_LHFunction lua_setlinehook (lua_LHFunction hook); | ||
1581 | lua_CHFunction lua_setcallhook (lua_CHFunction hook); | ||
1582 | \end{verbatim} | ||
1583 | Both return the previous hook. | ||
1584 | |||
1585 | |||
1457 | \section{Some Examples} | 1586 | \section{Some Examples} |
1458 | 1587 | ||
1459 | This section gives examples showing some features of Lua. | 1588 | This section gives examples showing some features of Lua. |
@@ -1473,7 +1602,8 @@ Arrays can be indexed by 0, negative numbers, or any other value (but \nil). | |||
1473 | Records are also trivially implemented by the syntactic sugar | 1602 | Records are also trivially implemented by the syntactic sugar |
1474 | \verb'a.x'. | 1603 | \verb'a.x'. |
1475 | 1604 | ||
1476 | The best way to implement a set is to use the indices of a table. | 1605 | The best way to implement a set is to store |
1606 | its elements as indices of a table. | ||
1477 | The statement \verb's = {}' creates an empty set \verb's'. | 1607 | The statement \verb's = {}' creates an empty set \verb's'. |
1478 | The statement \verb's[x] = 1' inserts the value of \verb'x' into | 1608 | The statement \verb's[x] = 1' inserts the value of \verb'x' into |
1479 | the set \verb's'. | 1609 | the set \verb's'. |
@@ -1874,8 +2004,8 @@ The authors would like to thank CENPES/PETROBR\'AS which, | |||
1874 | jointly with TeCGraf, used extensively early versions of | 2004 | jointly with TeCGraf, used extensively early versions of |
1875 | this system and gave valuable comments. | 2005 | this system and gave valuable comments. |
1876 | The authors would also like to thank Carlos Henrique Levy, | 2006 | The authors would also like to thank Carlos Henrique Levy, |
1877 | who found the name of the game% | 2007 | who found the name of the game. |
1878 | \footnote{BTW, Lua means {\em moon} in Portuguese.}. | 2008 | Lua means {\em moon} in Portuguese. |
1879 | 2009 | ||
1880 | 2010 | ||
1881 | 2011 | ||
@@ -1945,7 +2075,10 @@ Special care should be taken with macros like | |||
1945 | \verb'lua_getindexed' and \verb'lua_getfield'. | 2075 | \verb'lua_getindexed' and \verb'lua_getfield'. |
1946 | \end{itemize} | 2076 | \end{itemize} |
1947 | 2077 | ||
2078 | \pagebreak | ||
2079 | \tableofcontents | ||
1948 | \newcommand{\indexentry}[2]{\item {#1} #2} | 2080 | \newcommand{\indexentry}[2]{\item {#1} #2} |
2081 | %\catcode`\_=12 | ||
1949 | \begin{theindex} | 2082 | \begin{theindex} |
1950 | \input{manual.idx} | 2083 | \input{manual.idx} |
1951 | \end{theindex} | 2084 | \end{theindex} |