1This is nana.info, produced by makeinfo version 5.2 from nana.texi. 2 3START-INFO-DIR-ENTRY 4* Nana: (nana). The GNU Nana library (assertions, logging, forall,etc) 5END-INFO-DIR-ENTRY 6 7 This file documents the features and implementation of the GNU Nana 8library. 9 10 Copyright (C) 1996, 1997, 1998, 1999 P.J.Maker, Quoll Systems Pty 11Ltd. 12 13 Permission is granted to make and distribute verbatim copies of this 14manual provided the copyright notice and this permission notice are 15preserved on all copies. 16 17 18File: nana.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir) 19 20This manual documents how to install and use the Nana library which 21provides improved support for assertion checking and logging in C and 22C++. 23 24* Menu: 25 26* Introduction:: Overview 27* Installation:: Installing nana 28* Invoking:: Compiling, linking and generating gdb commands 29* Interface:: Interface to nana library functions 30* Shortform:: Nana shortform generation 31* Performance:: 32* Tracing:: 33* Usage:: Some examples 34* FAQ:: Frequently Asked Questions 35* Future:: Future work/projects 36* Index:: 37 38 -- The Detailed Node Listing -- 39 40Introduction 41 42* Related work:: 43* Assert:: 44* Scope:: 45 46Installing the Nana library 47 48* Required Software:: 49* Optional Software :: 50* Configure:: 51* Variables:: 52* Supported Platforms:: 53* Supported Debuggers:: 54* Known Problems:: 55* Bug Reports:: 56* New Versions:: 57 58Interface 59 60* nana.h:: 61* WITHOUT_NANA:: 62* I.h:: 63* DI.h:: 64* L.h:: 65* L_buffer.h:: 66* L_times.h:: 67* DL.h:: 68* GDB.h:: 69* Q.h:: 70* Qstl.h:: 71* now.h:: 72* cycles.h:: 73* eiffel.h:: 74* assert.h:: 75* calls.h:: 76 77cycles.h: access to CPU cycle counting registers. 78 79* RDTSC:: 80 81eiffel.h: eiffel type assertions 82 83* EIFFEL_CHECK:: 84* DOEND:: 85* REQUIRE...:: 86 87Tracing tools 88 89* Statement:: 90* Library:: 91 92Using Nana 93 94* Simplest:: 95* Syslog:: 96* GNU:: 97* Embedded Systems:: 98* Realtime:: 99* A database:: 100* Visualisation:: 101 102 103 104File: nana.info, Node: Introduction, Next: Installation, Prev: Top, Up: Top 105 1061 Introduction 107************** 108 109Nana is a library that provides support for assertion checking and 110logging in a space and time efficient manner. The aim is to put common 111good practice(1) into a library that can be reused rather than writing 112this stuff every time you begin a new project. 113 114 In addition assertion checking and logging code can be implemented 115using a debugger rather than as inline code with a large saving in code 116space. 117 118 Nana aims to solve the following problems: 119 120 1. Avoid the two executables problem (one with asserts in and another 121 without any). 122 123 The code space and time costs of having assertion checking and 124 detailed logging code in a program can be high. Normally people 125 construct two versions of the program, one with checking code for 126 testing and one without checking code for production use. 127 128 With nana one version of the executable can be built for both 129 testing and release since debugger based checking has negligible 130 space and time impact. 131 132 2. Configurable: the nana library is designed to be reconfigured by 133 the user according to their needs. For example we can: 134 * Modify the behavior on assertion failure, e.g. to attempt a 135 system restart rather than just shutting down. 136 * Selectively enable and disable assertion checking and logging 137 both at compile and run time. 138 * Send the logging information off to various locations, e.g. 139 - Users terminal 140 - A file for later checking. 141 - Another process, e.g. a plotting program or a program 142 that verifies that the system is behaving itself. 143 - A circular buffer in memory. 144 145 This is an old embedded systems trick and is very useful 146 for production systems. The time cost of logging into 147 memory is not large and when your production system in 148 the field has problems you can then see what was 149 happening in the minutes before its unfortunate demise 150 rather than asking some user what was happening before it 151 died. 152 153 3. Time and space efficient. 154 155 For example the GNU 'assert.h' implementation uses 53 bytes for 156 'assert(i>=0)' on a i386. The nana version using the i386 'stp' 157 instruction on assert fail uses 10 bytes. If you're willing to 158 accept the time penalty this can be reduced to 0 or 1 byte by using 159 debugger based assertions. 160 161 4. Support for formal methods. 162 163 * Before and after state (e.g. x,x' in the Z notation). 164 165 Specifications are often written in terms of the state of 166 variables before and after an operation. For example the 167 'isempty' operation on a stack should leave the stack 168 unchanged. To verify this in nana we could use: 169 170 bool isempty(){ /* true iff stack is empty */ 171 DS($s = s); /* copy s into $s in the debugger */ 172 ...; /* code to do the operation */ 173 DI($s == s); /* verify that s hasn't been changed */ 174 } 175 176 These '$..' variables are called convenience variables and are 177 implemented by gdb. They have a global scope and are 178 dynamically typed and initialised automatically to 0. 179 180 In addition a C only version of before and after state is 181 provided. For example: 182 183 bool isempty() { /* true iff stack is empty */ 184 ID(int olds); /* declare variable to hold old value */ 185 IS(olds = s); /* copy s into $s in the debugger */ 186 ...; /* code to do the operation */ 187 I(olds == s); /* verify that s hasn't been changed */ 188 } 189 190 * Support for Predicate Calculus. 191 192 Nana provides some support for universal (forall) and 193 existential (exists one or more) quantification. For example 194 to specify that the string v contains only lower case letters 195 we could use: 196 197 I(A(char *p = v, *p != '\0', p++, islower(*p))); 198 199 These macros can be nested and used as normal boolean values 200 in control constructs as well as assertions. Unfortunately 201 they depend on the GNU CC statement value extensions and so 202 are not portable. The following macros are defined in 'Q.h': 203 204 'A' 205 For all values the expression must be true. 206 'E' 207 There exists one or more values for which the expression 208 is true. 209 'E1' 210 There exists a single value for which the expression is 211 true. 212 'C' 213 Returns the number of times the expression is true. 214 'S' 215 Returns the sum of the expressions. 216 'P' 217 Returns the product of the expressions. 218 219 * A C/C++ based shortform generator similar to Eiffel which can 220 produce a HTML summary of your code. 221 222 The shortform of a program consists of the function headers 223 together with their preconditions(2) and postconditions(3) 224 225 * Performance measurement. 226 227 A small package which measures the time and space overhead of 228 code fragments is provided. This is used to analyse the 229 space/time requirements of the nana library and could be used 230 for other types of measurement. 231 232 * Verifying timing. 233 234 As well as using nana to verify timings with assertions using 235 a hardware supported timer you can also a simulator (e.g. the 236 PSIM power pc simulator by Cagney) with gdb. These simulators 237 can model time and provide a register called '$cycles' which 238 represents the current cycle count of the program. This can 239 be used to check that timing constraints are being meet. 240 241 void process_events() { 242 for(;;){ 243 DS($start = $cycles); 244 switch(get_event()){ 245 case TOO_HOT: 246 ...; 247 DI($cycles - $start <= 120); 248 break; 249 case TOO_COLD: 250 ...; 251 DI($cycles - $start <= 240); 252 break; 253 } 254 } 255 } 256 257 The intended audience for Nana includes: 258 259 * Software Engineers. 260 * Formal methods community. 261 * Real time programmers. 262 * System testers. 263 * People teaching programming. 264 265* Menu: 266 267* Related work:: 268* Assert:: 269* Scope:: 270 271 ---------- Footnotes ---------- 272 273 (1) Which is unfortunately quite uncommon in the authors experience. 274 275 (2) Precondition: a boolean expression which must be true if the 276operation is to succeed. For example the 'sort(int *v, int n)' might 277have have precondition that 'v != NULL && n >= 0'. 278 279 (3) Postcondition: a boolean expression that must be true if the 280operation is correct (and the precondition was true on entry). 281 282 283File: nana.info, Node: Related work, Next: Assert, Prev: Introduction, Up: Introduction 284 2851.1 Related work 286================ 287 288The Nana project was inspired by some other projects, in particular: 289 290 * Anna - Anna stands for "Annotated Ada" where the programmer inserts 291 various assertions into the code which can be automatically 292 validated. To quote from the WWW Virtual Library entry on Anna: 293 294 Anna is a language for formally specifying the intended 295 behaviour of Ada programs. It extends Ada with various 296 different kinds of specification constructs from ones as 297 simple as assertions, to as complex as algebraic 298 specifications. A tool set has been implemented at Stanford 299 for Anna, including: 300 301 1. standard DIANA extension packages, parsers, 302 pretty-printers; 303 2. a semantic checker; 304 3. a specification analyser; 305 4. an annotation transformer; and 306 5. a special debugger that allows program debugging based on 307 formal specifications 308 309 All tools have been developed in Ada and are therefore 310 extremely portable. Anna has thus been ported to many 311 platforms. For more information send e-mail to 312 "anna-request@anna.stanford.edu". Before down loading the 313 huge Anna release, you may wish to copy and read some Anna 314 LaTeX reports. 315 316 Anna is available from: 'ftp://anna.stanford.edu/pub/anna' 317 318 * Eiffel - the Eiffel programming language provides support in the 319 language flexible assertion checking. To quote from the Eiffel 320 page in WWW Virtual library: 321 322 Eiffel is a pure object-oriented language featuring multiple 323 inheritance, polymorphism, static typing and dynamic binding, 324 genericity (constrained and unconstrained), a disciplined 325 exception mechanism, systematic use of assertions to promote 326 programming by contract, and deferred classes for high-level 327 design and analysis. 328 329 * APP - Annotation PreProcessor. The APP was written by David S. 330 Rosenblum and provides assertion checking functions for C and C++. 331 It is implemented using a preprocessor wrapper around the C 332 preprocessor and supports quantifiers and before/after state. 333 334 See "A Practical Approach to Programming with Assertions" in Vol 335 21, No. 1, January 1995 of IEEE Transactions on Software 336 Engineering for an interesting paper describing APP. Unfortunately 337 the APP tool doesn't seem to be freely available (I'm willing to be 338 corrected on this). Note that any similarity between my examples 339 and David's are due to morphic resonance. 340 341 * ADL - the Assertion Definition Language. 342 343 To quote from 'http://www.sunlabs.com/research/adl/': 344 345 ADL (Assertion Definition Language) is a specification 346 language for programming interfaces. It can be used to 347 describe the programmer's interface to any C-callable 348 function, library or system call. 349 350 The Practical Specification Language. 351 352 ADL is the world's most practical specification language 353 because: 354 355 * Even partial specifications are useful 356 * Test programs can be automatically generated from ADL 357 specifications 358 * Specifications are external to the implementation of the 359 interface, so that they are vendor-independent. 360 361 An Automated Test Generator. 362 363 An ADL specification is not just a paper document. It can be 364 compiled by ADLT (the ADL translator). ADLT generates: 365 366 * Header files, that can be used in an implementation 367 * Test programs, that ensure that any implementation meets 368 the specification 369 * Natural-language documentation, derived directly from the 370 specification 371 372 ADLT can be used: 373 374 As a test generator, to create tests for existing software or 375 for existing standards As a development tool, to ensure that 376 documentation, software, and tests are aligned, and to enable 377 concurrent work on all three aspects of software production. 378 379 Nana is essentially a poor mans implementation of some of these ideas 380which works for C and C++. Ideally in the best of all possible worlds 381you might want to look at Eiffel or in the military world Ada and Anna. 382If you use TCL/TK you might also be interested in Jon Cook's 'AsserTCL' 383package. 384 385 386File: nana.info, Node: Assert, Next: Scope, Prev: Related work, Up: Introduction 387 3881.2 Assert.h considered harmful 389=============================== 390 391Most C programmers become familiar with assertions from the the 392'assert.h' header. As such its a very good thing and has a nice simple 393implementation. However it is also inefficient and leads some people to 394the conclusion that assertion checking is an expensive luxury. 395 396 The implementation of 'assert.h' as distributed with 'gcc' looks like 397the following (after a bit of editing): 398 399 # ifndef NDEBUG 400 # define _assert(ex) {if (!(ex)) \ 401 {(void)fprintf(stderr, \ 402 "Assertion failed: file \"%s\", line %d\n", \ 403 __FILE__, __LINE__);exit(1);}} 404 # define assert(ex) _assert(ex) 405 # else 406 # define _assert(ex) 407 # define assert(ex) 408 # endif 409 410 There are are two main problems with this: 411 412 1. Code space overhead: each call to 'assert' generates 2 function 413 calls with 4 and 1 arguments plus strings for error messages. If 414 'assert.h' had library code support we could make the 415 implementation much more space efficient, e.g. by calling a single 416 function on error detection. 417 2. The default behaviour simply prints a message and dies, ideally you 418 like to be able to use a debugger to determine why the assertion 419 failed. Even if you run this under the debugger you can't observe 420 the failures of variables are an assert failure because the process 421 exits rather than aborting back to the debugger. 422 423 Of course everyone merely rewrites their own 'assert' macro so these 424are not significant objections. The only problem is if the author uses 425the libraries without modification. 426 427 428File: nana.info, Node: Scope, Prev: Assert, Up: Introduction 429 4301.3 Scope of this document 431========================== 432 433This document aims to both describe the library and provide a tutorial 434in its use. Further work is required, particularly on the tutorial 435sections. If anyone has any suggestions please send them to me. 436 437 438File: nana.info, Node: Installation, Next: Invoking, Prev: Introduction, Up: Top 439 4402 Installing the Nana library 441***************************** 442 443Nana uses the normal GNU install method in the same way as 'gcc' and 444'gdb'. To install nana in the default location 445'/usr/local/{bin,lib,include}' you would use: 446 447 % gzcat nana-1.10.tar.gz | tar xvf - 448 % cd nana-1.10 449 % ./configure 450 % make 451 % make install 452 % make check 453 % make check-mail 454 % make subscribe 455 456 If you wish to produce space and time efficient code then replace the 457'./configure' with: 458 459 % I_DEFAULT=fast ./configure 460 461 If you are using a Pentium compatiable CPU which supports the 'RDTSC' 462instruction you may wish to enable cycle level timing in 'cycles.h' by 463using: 464 465 % ./configure --enable-rdtsc 466 467 The CHECK-MAIL and SUBSCRIBE targets both send e-mail. If you need 468to change the mailer used try something like: 469 470 % make MAILER=elm subscribe 471 472 *Note:* we need to install nana before running the 'make check' 473target. The 'check-mail' target sends the test report via e-mail to the 474'gnuware@cs.ntu.edu.au'. 475 476 Of course things are never that simple. If you want to install Nana 477in a different location or change the behaviour on error detection see 478*note Configure::. 479 480 Each of the sub-directories nana can be compiled and installed 481separately, e.g. if you don't need the documentation you can just 482compile and install from the 'src' sub-directory after doing the 483configure statement. 484 485 Note that some of the subdirectories contain code that you may wish 486to install, improve or inspect. In particular: 487 488 * 'emacs' - a protype emacs mode for browsing log files. 489 * 'examples' - some small examples. 490 * 'gdb' - some tools for use with gdb, in particular a statement 491 level trace utility and some gdb patches. 492 * 'perf' - a tool for measuring space/time of code fragments. 493 * 'shortform' - a shortform generator which produces a HTML summary 494 of your codes interface. 495 * 'tcl' - a prototype TCL driver. We actually have a few more TCL 496 tools in the works so if you're interested contact the author. 497 498* Menu: 499 500* Required Software:: 501* Optional Software :: 502* Configure:: 503* Variables:: 504* Supported Platforms:: 505* Supported Debuggers:: 506* Known Problems:: 507* Bug Reports:: 508* New Versions:: 509 510 511File: nana.info, Node: Required Software, Next: Optional Software, Prev: Installation, Up: Installation 512 5132.1 Required Software 514===================== 515 516The following software is possibly required to run nana. 517 518'gcc-2.7.2' 519 Nana makes use of two GNU extensions in its library so you really 520 should be using 'gcc'. Some of the code can be used with any C 521 compiler, though it may not be worth the bother. The dependencies 522 on gcc are in 'Q.h' which uses the statement value extension and in 523 'L.h' which uses the variable number of arguments extension to 524 'cpp'. 525'gdb-4.16+' 526 A recent version of 'gdb' is worthwhile, some early 4.?? versions 527 had problems setting a large number of breakpoints. Note that 528 'gdb-4.17' is available and has a few improvement which are useful 529 for some parts of this package including the tools in 'gdb'. 530'gmake' 531 The 'configure' script and 'Makefiles' are generated using the 532 'automake' and 'autoconf' programs. They should be reasonably 533 portable but if you have problems try using GNU make. For example 534 on some old DEC boxes we have had strange behaviour using the 535 system make. 536 537 For a listing of porting results including software versions see: 538 539 'http://www.cs.ntu.edu.au/homepages/pjm/nana-bug/' 540 541 542File: nana.info, Node: Optional Software, Next: Configure, Prev: Required Software, Up: Installation 543 5442.2 Optional Software 545===================== 546 547In addition to the required software you might also be interested in: 548 549 * 'http://www.cs.tu-bs.de/softech/ddd/' - a smart frontend for gdb 550 which can display dynamic data structures such as linked lists, 551 etc. 552 * 'ftp://ftp.ci.com.au/pub/psim/' - a cycle level simulator for the 553 PowerPC. A fine piece of work. 554 555 556File: nana.info, Node: Configure, Next: Variables, Prev: Optional Software, Up: Installation 557 5582.3 Configure 559============= 560 561Nana uses a standard GNU 'autoconf' generated 'configure' script. The 562'configure' script checks the setup on your machine and then generates 563the appropriate Makefiles. Some of the things checked by configure 564include: 565 566 1. Which compiler, compiler flags and libraries to use, e.g. you 567 might need to include a '-lposix' flag to the linker to build 568 programs on your machine. 569 2. Which header (.h) files are available on this machine, e.g. is 570 'unistd.h' available on this machine. 571 3. Where to install programs, header file, man pages, etc. 572 573 In addition 'configure' uses the host architecture and operating 574system to generate the 'nana-config.h' file. This file contains some 575macro definitions which define how nana works on particular operating 576systems and hardware architectures. 577 578 For example on 'i386' machines we would use the 'asm("hlt")' 579instruction whenever an assertion fails, on a 'sparc' we would use 580'asm("unimp")'. Otherwise we would default to a plain C call to 581'abort()' If 'configure' does not recognise your machine it uses plain C 582code. 583 584 You may wish to change these defaults on installation, one method is 585to edit a local copy of the 'nana-config.h' file. Alternately you can 586define the code yourself in the call to 'configure'. For example to 587redefine the action we take when an error is detected by the 'I' macro 588we can use: 589 590 I_DEFAULT_HANDLER="restart_system()" ./configure 591 592 As well as simple calls to routines various other bits of information 593are passed off to the 'I_DEFAULT_HANDLER' such as the expression that 594failure and a failure code. For example: 595 596 % I_DEFAULT_HANDLER="restart(line,file,param)" ./configure 597 598 The default for 'I_DEFAULT_HANDLER' calls a function which prints a 599message and then dumps core. Different behaviour on failure can be 600organised by setting the 'I_DEFAULT' to 'fast', i.e. plain core dump or 601'verbose' which prints an error messsage and then does the core dump. 602 603 % I_DEFAULT=fast ./configure 604 605 For nana the following examples may be useful: 606 607 1. './configure' 608 609 Accept the default values for everything. In particular the files 610 will be installed in: 611 612 '/usr/local/{bin,include,lib,man,info}' 613 614 2. './configure --prefix=~project/tools' 615 616 Install the files into: 617 618 '~project/tools/{bin,include,lib,man,info}' 619 620 3. './configure --bindir=~project/bin --libdir=~/project/lib \ 621 --includedir=~/project/headers --infodir=/usr/local/info \ 622 --mandir=~/project/doc' 623 624 The install directory for program ('bin'), etc can all be set with 625 command line arguments to 'configure'. 626 627 4. 'CC=xacc LIBS=-lposix ./configure sun3' 628 629 If the defaults chosen by 'configure' are not correct you can 630 override them by setting variables such as 'CC' before calling 631 'configure'. The 'sun3' argument is used to identify the machine 632 we are running on and may be necessary on some machines. 633 634 5. './configure --help' 635 636 And of course when in doubt ask for help. 637 638 For even more details see the file 'INSTALL.con' which contains the 639generic instructions for use with 'autoconf' generated 'configure' 640scripts. 641 642 643File: nana.info, Node: Variables, Next: Supported Platforms, Prev: Configure, Up: Installation 644 6452.4 Variables for ./configure 646============================= 647 648The configure program uses the following shell variables to change 649various defaults. Another method is simply to edit the 'nana-config.h' 650file. Most of these values should be auto detected, so you can ignore 651this section until your need to save a few bytes of store by using 652'asm("hlt")' instead of a call to 'abort()'. 653 654'DI_MAKE_VALID_BREAKPOINT' 655 This text is inserted when the 'DI.h' library needs to set a 656 breakpoint in the generated code. It should ideally update all 657 variables which being kept in registers etc so that gdb gets the 658 correct values for each variable. 659 660 Possible values include: 661 662 1. 'asm("nop")' - a single 'nop' instruction to set the 663 breakpoint at. 664 665 This is the default. 666 667 2. '_vi = 0' - where '_vi' is a global volatile int. 668 3. '_vi = (exprn)' - where EXPRN is the expression we are 669 checking for this assertion. 670 4. '/* nothing */' - nothing at all, this means the breakpoint 671 will be set at the start of the next statement which works 672 most of the time. However for some examples this will do the 673 wrong thing. 674'DL_MAKE_VALID_BREAKPOINT' 675 Used for the same purpose as 'DI_MAKE_VALID_BREAKPOINT' for 'DL.h'. 676 It also defaults to 'asm("nop")'. 677'I_DEFAULT_HANDLER' 678 The code called when 'I.h' detects an error. 679 'asm("hlt")' 680 Some machines use a 'hlt' instruction. 681 'asm("unimp")' 682 And other machines use a 'unimp' instruction. 683 'abort()' 684 Or we could use a call to 'abort' which is at least standard 685 C. On some machines this is significantly larger than a 686 single 'hlt' instruction. 687 'restart()' 688 Or a call to a function which attempts to restart the system. 689'ALWAYS_INCLUDE_MALLOC' 690 This is a dodgey for some versions of Linux which don't seem to 691 include 'malloc' when you include 'stdio.h' and use 'print'. This 692 causes problems for 'gdb' since it uses 'malloc' in the executable 693 to implement parts of its functionality. 694 695 This kludge should be removed! 696'GDB' 697 This is the pathname of the version of GDB you wish to use. For 698 example on my FreeBSD box we have gdb-4.16 installed in '/usr/bin' 699 and gdb-4.17 in '/usr/local/bin/' to optimise confusion. If you 700 wish nana to use 4.17 try something like: 701 702 GDB=/usr/local/bin/gdb ./configure 703 704 705File: nana.info, Node: Supported Platforms, Next: Supported Debuggers, Prev: Variables, Up: Installation 706 7072.5 Supported Platforms 708======================= 709 710Nana has been tested on the following platforms: 711 712 1. i386-unknown-linux, gcc-2.7.0, gdb-4.16 713 2. sparc-sun-sunos4.1.4, gcc-2.7.2.f.1, gdb-4.16 714 3. sparc-sun-solaris2.3, gcc-2.7.2, gdb-4.16 715 4. alpha-dec-osf3.2, gcc-2.7.2, gdb-4.16 716 5. mips-sgi-irix5.3, gcc-2.7.0, gdb-4.16 717 6. powerpc-ibm-aix3.2.5, gcc-2.6.3, gdb-4.16 718 719 The 'alpha-dec-osf3.2', 'mips-sgi-irix5.3' and 'powerpc-ibm-aix3.2.5' 720implementations have problems when you compile with '-O2' or '-O3' 721optimisation. This causes some errors in the the debugger based 722assertion and logging code since variables can be removed or changed by 723optimisation. At '-O' everything passes. Regardless of optimisation 724the C based checking code passes all tests on these platforms. 725 726 If you use nana on a new platform please send the report file to me 727via the 'make check-mail' command. A machine generated list of this 728information is available at: 729 730 'http://www.cs.ntu.edu.au/homepages/gnuware/nana' 731 732 (Warning this page is out of date and may be fixed shortly) 733 734 735File: nana.info, Node: Supported Debuggers, Next: Known Problems, Prev: Supported Platforms, Up: Installation 736 7372.6 Supported Debuggers 738======================= 739 740Currently Nana works with the GNU GDB debugger which is available on a 741wide range of platforms including embedded systems and even provides 742support for remote debugging. Porting to any reasonable debugger with 743conditional breakpoints and commands is not very difficult. 744 745 As an example of an unreasonable debugger, Nana has been ported to 746work with the Microsoft CodeView debugger. The port is small (60 lines 747of code) but suffers from a problem with variable scoping in CodeView. 748If a breakpoint is set at a point in the code the expressions are not 749evaluated from that particular scope. For example setting a breakpoint 750in the function 'f' cannot access a variable local to 'f' directly. 751CodeView has a unique (expletive deleted) scope operator which you must 752use to set the scope '{...}'. This makes the interface somewhat less 753than beautiful. 754 755 Another good thing about CodeView is to try a debug command which 756prints a message which contains a single open '{'. This of course 757causes it to hang and was the main problem during the porting to 758CodeView which took a whole day.(1) 759 760 If anyone is interested I may release the CodeView implementation, 761please contact me if you are interested. Of course a better bet is 762probably to move to the 'gdbserver' system. I think 'gdb' has been 763released as a native even for some Microsoft operating systems. 764 765 Other debuggers like DBX don't seem to be worth the trouble since gdb 766works on those machines. A redesign of the nana internals may also be 767useful if we decide portability between debuggers is actually useful. 768 769 ---------- Footnotes ---------- 770 771 (1) And about 60 reset cycles where the machine went off into 772hyperspace. 773 774 775File: nana.info, Node: Known Problems, Next: Bug Reports, Prev: Supported Debuggers, Up: Installation 776 7772.7 Known Problems 778================== 779 780Nana has the following known features (or perhaps problems): 781 782 1. Nana macros which use the debugger such as 'DI' or 'DL' should be 783 on lines by themselves. If you mix code and nana macros on the 784 same line you will get errors, e.g: 785 786 main(){ 787 int x; 788 x = 5; x--; DI(x == 4); 789 } 790 791 This doesn't work since breakpoints are set at line boundaries 792 rather than statement ones. Of course anyone who writes code like 793 this deserves whatever happens to them. 794 795 2. Optimisation can remove variables so that debugger based assertions 796 ('DI.h') do not work correctly. As usual the interaction between 797 the debugger and the compiler is rather complicated. This may not 798 be a problem if the appropriate compile-time flags are selected, 799 e.g. '-O0 and -O1' work on most platforms. 800 801 3. The 'Q.h' macros depend on the statement value extension to GNU CC 802 so if you wish to use them you must use GCC. This can be fixed for 803 C++ in a possibly useful manner, I can't see any solution for C. 804 805 4. The logging macros depend on the Var Args extension provided by the 806 GNU C Preprocessor.(1) We could (probably will) implement a fix 807 for this based on the tricks in the C FAQ. Unfortunately these 808 tricks are not pretty. For now interested users could simply 809 replace their CPP with the GNU CPP if they wished to stay with 810 non-standard compilers. 811 812 5. The 'Q.h' macros do not work in the debugger since 'gdb' does 813 support the statement expression extension. 814 815 6. Multiline expressions do not work as expected in the debugger since 816 you need to use a blackslash as an escape at the end of the line. 817 For example: 818 819 DI(x + 820 10 > 30); 821 A few backslashes may solve this particular problem. 822 823 7. Problems with the 'configure' script. 824 825 The 'configure' script automatically detects the target operating 826 system and architecture and then generates 'nana-config.h'. If the 827 options selected in 'nana-config.h' are incorrect they can be 828 edited by hand and installed in the usual include directory. The 829 easiest method is simply to delete all macros in 'nana-config.h' 830 since the system defaults to more portable (and less efficient) 831 implementations. If you wish to do this from the configure script 832 you can try giving a unsupported machine type, e.g. 833 834 % ./configure pdp11-dec-ultrix 835 836 8. Some users have reported problems with the 'configure' script 837 detecting 'vsnprintf'. If 'configure' doesn't find it and it does 838 exist then simply define it in 'nana-config.h' as per the previous 839 question. 840 841 If 'vsnprintf' really doesn't exist then get a new C library, 842 possibly the GNU libc. 843 844 9. The use of 'vsprintf' opens a security hole since no bounds 845 checking is done by it. Nana attempts to use 'vsnprintf' which is 846 safe when it exists but it will resort to 'vsprintf' if it can't 847 find 'vsnprintf'. All careful people should make sure that they 848 have a library with 'vsnprintf'. 849 850 10. 'Qstl.h' doesn't work since the STL library has not been installed 851 along with C++. This can of course be fixed by installing STL. 852 See: 853 854 'http://www.stl.org' 855 856 11. 'STL' header file errors due to nana. 857 858 The C++ 'STL' header files for version 3.0 at least must be 859 included before the 'Q.h' file. 860 861 The problem is caused by the STL files using 'S' as a template 862 argument. Of course 'Q.h' uses 'S' for summing a series. As usual 863 namespace pollution strikes again. 864 865 (Thanks to Han Holl for this particular problem). 866 867 12. If you try to use the debugger based macros such as 'DI.h' or 868 'DL.h' on code that has not been compiled with '-g' then misery 869 follows. 870 871 (Thanks to Eugen Dedu for this one) 872 873 ---------- Footnotes ---------- 874 875 (1) This allows a variable number of arguments to C preprocessor 876macros. 877 878 879File: nana.info, Node: Bug Reports, Next: New Versions, Prev: Known Problems, Up: Installation 880 8812.8 Bug Reports 882=============== 883 884If you think you have found a bug in the Nana library, please 885investigate it and report it. 886 887 * Please make sure that the bug is really in the Nana library. 888 * You have to send us a test case that makes it possible for us to 889 reproduce the bug. 890 * You also have to explain what is wrong; if you get a crash, or if 891 the results printed are not good and in that case, in what way. 892 Make sure that the bug report includes all information you would 893 need to fix this kind of bug for someone else. 894 895 If your bug report is good, we will do our best to help you to get a 896corrected version of the library; if the bug report is poor, we won't do 897anything about it (apart from asking you to send better bug reports). 898 899 Send your bug report to: 900 901 'nana-bug@cs.ntu.edu.au' 902 903 Copies of bug reports will be kept at: 904 905 'http://www.cs.ntu.edu.au/homepages/pjm/nana-bug/' 906 907 908File: nana.info, Node: New Versions, Prev: Bug Reports, Up: Installation 909 9102.9 New Versions 911================ 912 913New versions of nana will be made available at: 914 915 'ftp://ftp.cs.ntu.edu.au/pub/nana/' 916 917 If you wish to be informed about new releases of nana then subscribe 918to the nana mailing list. Send a message containing 'subscribe' <your 919e-mail address> to: 920 921 'mailto:nana-request@it.ntu.edu.au'. 922 923 A hypermail archive of this list is kept at: 924 925 'http://www.cs.ntu.edu.au/hypermail/nana-archive' 926 927 If you wish to send a message to the list send it to 928'mailto:nana@it.ntu.edu.au'. 929 930 931File: nana.info, Node: Invoking, Next: Interface, Prev: Installation, Up: Top 932 9333 Invoking Nana 934*************** 935 936The functions defined by Nana are implemented either as pure C code or 937as a set of commands which are generated for the debugger. To use the C 938based support for assertion checking you would use something like: 939 940 #include <nana.h> /* this file includes the other nana .h files */ 941 942 int floor_sqrt(int i) { /* returns floor(sqrt(i) */ 943 int answer; 944 I(i >= 0); /* assert(i >= 0) if i -ve then exit */ 945 ...; /* code to calculate sqrt(i) */ 946 L("floor_sqrt(%d) == %d\n", 947 i, answer); /* logs a printf style message */ 948 } 949 950 To compile and link the previous code you may need to use the 951'-Ipath' or '-lnana' flags with the compiler. For example: 952 953 % gcc toy.c -lnana 954 955 If the nana headers have been installed in a strange location you may 956need to do something like: 957 958 % gcc -I<strange location>/include toy.c -L<strange location>/lib -lnana 959 960 The next example uses the debugger versions of 'L' and 'I'. If the 961code is run under the debugger these checks will occur, otherwise they 962take up a negligible amount of space and time. 963 964 #include <nana.h> /* this includes the other nana .h files */ 965 966 int floor_sqrt(int i){ 967 int answer; 968 DI(i >= 0); /* assert(i >= 0) if i -ve then exit */ 969 ...; /* code to calculate sqrt(i) */ 970 DL("floor_sqrt(%d) == %d\n", i, answer); /* logs a printf style message */ 971 } 972 973 To generate the debugger commands from the C source we just run the 974'nana' filter over the program and then execute the commands under gdb 975using the 'source' command. You also need to compile the program with 976the '-g' option so the debugger works. So something like: 977 978 % gcc -g sqrt.c 979 % nana sqrt.c >sqrt.gdb 980 % gdb a.out 981 (gdb) source sqrt.gdb 982 breakpoint insert: ... 983 (gdb) run 984 ... 985 (gdb) quit 986 987 Note that any C preprocessor flags which you use must be passed off 988to the 'nana' command. The best way to do this of course is in a 989Makefile. Something like the following works for GNU Make: 990 991 %.nana: %.c 992 nana $(CFLAGS) $< >$@ 993 994 The 'nana' filter can also be run over multiple source files in a 995single run if thats more convenient. 996 997 For convenience a number of other simple scripts are provided, in 998particular to: 999 1000'nana-run' 1001 Run a program under the debugger without prompting, etc. For 1002 example: 1003 1004 % nana-run a.out -x main.gdb 1005 output from program 1006 1007'nana-clg' 1008 Compiles the program, generates the debugger commands and the runs 1009 the program using 'nana-run'. For example: 1010 1011 % nana-clg -O3 main.c 1012 output from program 1013 1014 You can change the compiler invoked by 'nana-clg' by redefining the 1015 'NANACC' environment variable. For example: 1016 1017 % NANACC=g++ nana-clg -O3 main.cc 1018 1019 The installation also 'nana-c++lg' which compiles your code using a 1020 GNU C++ compiler. 1021 1022'nana-trace' 1023 Generates a line by line trace of the execution of a program using 1024 GDB. For example: 1025 1026 % nana-trace a.out 1027 54 printf("main()\n"); 1028 55 x = distance(5,-5); 1029 distance (i=5, j=-5) at test.c:47 1030 47 i = -i; 1031 48 j = -j; 1032 ... 1033 1034 The arguments to 'nana-trace' are passed directly to GDB. If you 1035 wish display variables or call procedures on each line then could 1036 use something like: 1037 1038 % nana-trace -x mycommands.gdb a.out 1039 1040 Where the 'mycommands.gdb' contains the GDB commands such as 1041 'display x' which causes 'x' to be printed every time the debugger 1042 gets control of the program. 1043 1044 1045File: nana.info, Node: Interface, Next: Shortform, Prev: Invoking, Up: Top 1046 10474 Interface 1048*********** 1049 1050This section describes the details of the interface to nana library. 1051 1052 All of the files can be included multiple times without ill-effect 1053since they use the C preprocessor to make sure the header declarations 1054are only seen the first by the compiler. Each of the files can also be 1055included individually. 1056 1057* Menu: 1058 1059* nana.h:: 1060* WITHOUT_NANA:: 1061* I.h:: 1062* DI.h:: 1063* L.h:: 1064* L_buffer.h:: 1065* L_times.h:: 1066* DL.h:: 1067* GDB.h:: 1068* Q.h:: 1069* Qstl.h:: 1070* now.h:: 1071* cycles.h:: 1072* eiffel.h:: 1073* assert.h:: 1074* calls.h:: 1075 1076 If any of the following routines have an internal problem (e.g. 1077malloc fails due to lack of memory) they will call the 'nana_error' 1078function defined in 'nana_error.c'. By default this function prints a 1079message and dumps core using 'abort'. If you wish to override this 1080behaviour you should define your own handler before linking in the nana 1081library. 1082 1083 1084File: nana.info, Node: nana.h, Next: WITHOUT_NANA, Prev: Interface, Up: Interface 1085 10864.1 nana.h: the main header file 1087================================ 1088 1089The 'nana.h' file includes most of the other files in the library. In 1090particular it '#include's' the following files: 1091 1092'I.h' 1093'DI.h' 1094'L.h' 1095'DL.h' 1096'Q.h' 1097'GDB.h' 1098 1099 1100File: nana.info, Node: WITHOUT_NANA, Next: I.h, Prev: nana.h, Up: Interface 1101 11024.2 WITHOUT_NANA: disabling all nana code for portability. 1103========================================================== 1104 1105If you wish to disable all nana code you can '#define' the 1106'WITHOUT_NANA' macro. This selects versions of the macros defined in 1107'I.h','L.h', etc which map to '/* empty */'. 1108 1109 So if you are using nana for your development but don't wish to force 1110your customers to use it you can add an option to your 'configure' 1111script to define/undefine 'WITHOUT_NANA'. In addition you will need to 1112distribute copies of the nana header files with your package to get the 1113stubs. 1114 1115 Note that the 'L.h' and 'DL.h' macros use the macro variable number 1116of arguments extension provided by GNU C. If you wish your code to be 1117portable you should use the macros 'VL((..))', etc rather than 'L(..)' 1118to avoid problems with non GNU C preprocessors which only take a fixed 1119number of arguments. 1120 1121 1122File: nana.info, Node: I.h, Next: DI.h, Prev: WITHOUT_NANA, Up: Interface 1123 11244.3 I.h: C based invariant checking 1125=================================== 1126 1127This implements the C based invariant checking code and is a replacement 1128for 'assert.h'. The first two macros are the normal user interface; the 1129remainder are used for configuring the behaviour on failure, etc. 1130 1131 -- Macro: void I (bool EXPRN) 1132 The EXPRN should always be true if the program is correct. If the 1133 EXPRN is false a message will be printed, followed by core dump.(1) 1134 1135 Checking can be enabled and disabled by using the I_LEVEL and 1136 I_DEFAULT_GUARD macros. See the definitions below for these macros 1137 for further details. 1138 1139 Note that EXPRN should have no side-effects(2) since disabling 1140 checking shouldn't change your programs behaviour. 1141 1142 I(z != 0); 1143 x = y / z; 1144 1145 -- Macro: void N (bool EXPRN) 1146 The opposite of 'I', i.e. the expression must never ever be true 1147 if the program is working properly. It is equivelant to 'I(!(e))' 1148 and exists as a piece of syntactic sugar which may be helpful for 1149 complicated boolean expressions. 1150 1151 char* strdup(char *s) { 1152 N(s == NULL); 1153 ...; 1154 } 1155 1156 -- Macro: int I_LEVEL 1157 The 'I_LEVEL' macro is used to globally enable and disable checking 1158 by the macros in this file. It can take on one of three values: 1159 1160 '0' 1161 Disable all checking. Regardless of anything else no code 1162 will be generated for 'I', 'N', etc. 1163 '1' 1164 Enable checking only if the corresponding guard condition is 1165 true. The guard condition can be used to enable and disable 1166 checking at compile and run time. 1167 '2' 1168 Enable all checking regardless of guard conditions. 1169 1170 'I_LEVEL' defaults to '1'. 1171 1172 -- Macro: bool I_DEFAULT_GUARD 1173 The 'I_DEFAULT_GUARD' is used to selectively enable or disable 1174 checking at compile or run time. 1175 1176 'I_DEFAULT_GUARD' defaults to 'TRUE', i.e. always enabled. 1177 1178 A user would typically define 'I_DEFAULT_GUARD' to be global or 1179 local variable which is used to turn checking on or off at 1180 run-time. For example: 1181 1182 #define I_DEFAULT_GUARD i_guard > 0 1183 1184 extern int i_guard; 1185 1186 -- Macro: text I_DEFAULT_PARAMS 1187 This is passed off to the 'I_DEFAULT_HANDLER' and defaults to 1188 nothing, it is just some text and is intended to pass failure codes 1189 (e.g. 'IEH303') or requests (e.g. 'HW_DEAD') information off to 1190 the handler. 1191 1192 'I_DEFAULT_PARAMS' defaults to nothing. 1193 1194 -- Macro: void I_DEFAULT_HANDLER (char *EXPRN, char *FILE, int LINE, 1195 PARAM) 1196 1197 When an error is detected the 'I_DEFAULT_HANDLER' will be called to 1198 handle the error. The arguments are: 1199 1200 'exprn' 1201 A string representation of the expression that has failed, 1202 e.g. '"I(i>=0)"'. 1203 'file' 1204 The file that this error occurred in, i.e. '__FILE__'. 1205 'line' 1206 The line number for the error, i.e. '__LINE__'. 1207 'param' 1208 An optional parameter which can be passed across which 1209 defaults to 'I_DEFAULT_PARAMS'. This can be used to pass 1210 failure codes or other information from the checking code to 1211 the handler. 1212 1213 All of the remaining macros are used to individually override the 1214default values defined above. Normally these macros would be used in a 1215system wide header file to define macros appropriate for the 1216application. For example you might use 'IH' to define different 1217checking macros for hardware and software faults. 1218 1219 -- Macro: void I (bool E) 1220 -- Macro: void IG (bool E, bool G) 1221 -- Macro: void IH (bool E, Macro H) 1222 -- Macro: void IP (bool E, Text P) 1223 -- Macro: void IGH (bool E, bool G, Macro H) 1224 -- Macro: void IGP (bool E, bool G, Text P) 1225 -- Macro: void IHP (bool E, Macro H, Text P) 1226 -- Macro: void IGHP (bool E, bool G, Macro H, Text P) 1227 -- Macro: void N (bool E) 1228 -- Macro: void NG (bool E, bool G) 1229 -- Macro: void NH (bool E, Macro H) 1230 -- Macro: void NP (bool E, Text P) 1231 -- Macro: void NGH (bool E, bool G, Macro H) 1232 -- Macro: void NGP (bool E, bool G, Text P) 1233 -- Macro: void NHP (bool E, Macro H, Text P) 1234 -- Macro: void NGHP (bool E, bool G, Macro H, Text P) 1235 1236 We also provide support for referring to previous values of variables 1237in postconditions. The 'ID' macro is used to create variables to save 1238the old state in. The 'IS' and 'ISG' macros are to set these values. 1239 1240 -- Macro: void ID (Text DECLN) 1241 -- Macro: void IS (Text ASSIGNMENT) 1242 -- Macro: void ISG (Text DECLN, bool G) 1243 1244 For example: 1245 void ex(int &r) { 1246 ID(int oldr = r); /* save parameter */ 1247 g(r); 1248 I(oldr == r); /* check r is unchanged */ 1249 while(more()) { 1250 IS(oldr = r); /* assign r to oldr */ 1251 h(r); 1252 I(oldr == r * r); 1253 } 1254 } 1255 1256 ---------- Footnotes ---------- 1257 1258 (1) If you don't want a core dump then look at stopping the core 1259dumps with 'ulimit' rather than changing the handler. 1260 1261 (2) Side-effects include such operations as input/output or 1262assignments, e.g. 'x++'. 1263 1264 1265File: nana.info, Node: DI.h, Next: L.h, Prev: I.h, Up: Interface 1266 12674.4 DI.h: debugger based invariant checking 1268=========================================== 1269 1270This implements the debugger based invariant checking code. The first 1271two macros are the normal user interface; the remainder are used for 1272configuring the behaviour on failure, etc. Note that these macros have 1273no effect unless you run your program under the debugger and read in the 1274commands generated by the 'nana' command. You also need to compile the 1275program with the '-g' option. 1276 1277 -- Macro: void DI (bool EXPRN) 1278 The EXPRN should always be true if the program is working. If it 1279 is true then nothing happens otherwise the code given by 1280 'DI_DEFAULT_HANDLER' will be called which by default prints a 1281 message and dies just like 'assert.h'. 1282 1283 The checking using DI can be enabled and disabled by using the 1284 DI_LEVEL and DI_DEFAULT_GUARD macros. See the definitions below 1285 for these macros for further details. 1286 1287 Note that EXPRN should have no side-effects(1) since disabling the 1288 checking shouldn't change your programs behaviour. 1289 1290 -- Macro: void DN (bool EXPRN) 1291 The opposite of 'DI', i.e. the expression must never ever be true 1292 if the program is working properly. It is equivelant to 'I(!(e))' 1293 and exists as piece of syntactic sugar which is helpful for 1294 complicated boolean expressions. 1295 1296 -- Macro: int DI_LEVEL 1297 The 'DI_LEVEL' macro is used to globally enable and disable 1298 checking, in particular it can take on one of three values: 1299 1300 '0' 1301 Disable all checking. Regardless of anything else no code 1302 will be generated for 'DI', 'DN', etc. 1303 '1' 1304 Enable checking only if the corresponding guard condition is 1305 true. The guard condition can be used to enable and disable 1306 checking at compile and run time. 1307 '2' 1308 Enable all checking regardless of guard conditions, etc. 1309 1310 'DI_LEVEL' defaults to '1'. 1311 1312 -- Macro: bool DI_DEFAULT_GUARD 1313 The 'DI_DEFAULT_GUARD' is used to selectively enable or disable 1314 checking at compile or run time. 1315 1316 'DI_DEFAULT_GUARD' defaults to 'TRUE', i.e. always enabled. 1317 1318 A user would typically define 'DI_DEFAULT_GUARD' to be global or 1319 local variable which is used to turn checking on or off at 1320 run-time. For example: 1321 1322 #define DI_DEFAULT_GUARD (i_guard) 1323 1324 extern int i_guard; 1325 1326 -- Macro: text DI_DEFAULT_PARAMS 1327 This is passed off to the 'DI_DEFAULT_HANDLER' and defaults to 1328 nothing, it is just some text and is intended to pass failure codes 1329 (e.g. 'IEH303') or requests (e.g. 'HW_DEAD') information off to 1330 the handler. 1331 1332 'DI_DEFAULT_PARAMS' defaults to nothing. 1333 1334 -- Macro: void DI_DEFAULT_HANDLER (char *EXPRN, char *FILE, int LINE, 1335 PARAM) 1336 1337 When an error is detected the 'DI_DEFAULT_HANDLER' will be called 1338 to handle the error. The arguments are: 1339 1340 'exprn' 1341 A string representation of the expression that has failed, 1342 e.g. '"I(i>=0)"'. 1343 'file' 1344 The file that this error occurred in, i.e. '__FILE__'. 1345 'line' 1346 The line number for the error, i.e. '__LINE__'. 1347 'param' 1348 An optional parameter which can be passed across which 1349 defaults to 'DI_DEFAULT_PARAMS'. This can be used to pass 1350 failure codes or other information from the checking code to 1351 the handler. 1352 1353 -- Macro: void DI_MAKE_VALID_BREAKPOINT (exprn E) 1354 This macro is used to ensure that a breakpoint can be set at the 1355 location we are checking using 'DI', etc. It defaults to 1356 'asm("nop")' and can be redefined by the user. 1357 1358 -- Macro: void DI (bool E) 1359 -- Macro: void DIG (bool E, bool G) 1360 -- Macro: void DIH (bool E, Macro H) 1361 -- Macro: void DIP (bool E, Text P) 1362 -- Macro: void DIGH (bool E, bool G, Macro H) 1363 -- Macro: void DIGP (bool E, bool G, Text P) 1364 -- Macro: void DIHP (bool E, Macro H, Text P) 1365 -- Macro: void DIGHP (bool E, bool G, Macro H, Text P) 1366 -- Macro: void DN (bool E) 1367 -- Macro: void DNG (bool E, bool G) 1368 -- Macro: void DNH (bool E, Macro H) 1369 -- Macro: void DNP (bool E, Text P) 1370 -- Macro: void DNGH (bool E, bool G, Macro H) 1371 -- Macro: void DNGP (bool E, bool G, Text P) 1372 -- Macro: void DNHP (bool E, Macro H, Text P) 1373 -- Macro: void DNGHP (bool E, bool G, Macro H, Text P) 1374 1375 All of these macros are used to individually override the default 1376 values defined above. Normally these macros would be used in a 1377 system wide header file to define macros appropriate for the 1378 application. 1379 1380 -- Macro: void DS (E) 1381 -- Macro: void DSG (E, G) 1382 These macros are used to assign values to convenience variables in 1383 the debugger. Convenience variables are dynamically typed, global 1384 in scope and initialised to 0. They start with a single '$' and 1385 can be used be used for saving the state of program or for counting 1386 events. The 'DS' macro executes E under the same rules as 'DI'. 1387 The 'DSG' macro executes E only if the the expression G is true. 1388 1389 Note that 'DS' and 'DSG' can also be used for modifying C variables 1390 and calling functions. 1391 1392 ---------- Footnotes ---------- 1393 1394 (1) Side-effects include operations like input/output or assignments. 1395 1396 1397File: nana.info, Node: L.h, Next: L_buffer.h, Prev: DI.h, Up: Interface 1398 13994.5 L.h: support for printf style logging 1400========================================= 1401 1402These routines are used to provide logging functions. Messages can be 1403divided into classes and separately enabled and disabled. 1404 1405 -- Macro: void L (ARGS...) 1406 Used to log a message in a similar way to printf. 1407 1408 Defaults to a using 'fprintf' on 'stderr'. 1409 1410 -- Macro: void LG (bool GUARD, ARGS...) 1411 -- Macro: void LH (function HANDLER, ARGS...) 1412 -- Macro: void LP (text PARAM, ARGS...) 1413 -- Macro: void LGP (bool GUARD, text PARAM, ARGS...) 1414 -- Macro: void LHP (function HANDLER, text PARAM, ARGS...) 1415 -- Macro: void LGHP (bool GUARD, function HANDLER, text PARAM, ARGS...) 1416 And all of the special functions. 1417 1418 The macros such as 'L' depend on the GNU CC variable number of 1419arguments to macros extension. If you wish to compile your code on 1420other systems you might wish to use the following variations on 'L', 1421etc. 1422 1423 -- Macro: void VL ((ARGS...)) 1424 -- Macro: void VLG ((bool GUARD, ARGS...)) 1425 -- Macro: void VLH ((function HANDLER, ARGS...)) 1426 -- Macro: void VLP ((text PARAM, ARGS...)) 1427 -- Macro: void VLGP ((bool GUARD, TEXT PARAM, ARGS...)) 1428 -- Macro: void VLHP ((function HANDLER, text PARAM, ARGS...)) 1429 -- Macro: void VLGHP ((bool GUARD, function HANDLER, text PARAM, 1430 ARGS...)) 1431 Each of these macros calls the corresponding function from the 1432 previous group, i.e. by default 'VLG' is the same as a call to 1433 'LG'. If you define 'WITHOUT_NANA' all these macros are translated 1434 to '/* empty */'. 1435 1436 Thus you can have nana under GCC whilst the code is still portable 1437 to other compilers. However debugging information will not be 1438 available on other platforms. 1439 1440 *Note:* the argument list is surrounded by *two* sets of brackets. 1441 For example: 1442 1443 VL(("haze in darwin = %d\n", 3.4)); 1444 1445 -- Macro: void L_LEVEL 1446 Used to enable and disable logging independently of guard 1447 expressions. 1448 1449 '2' 1450 Always print message 1451 '1' 1452 Print message only if the guard expression is true. 1453 '0' 1454 Never print any messages. 1455 1456 Defaults to '1'. 1457 1458 -- Macro: text L_DEFAULT_HANDLER 1459 The default handler for printing which is simply the name of the 1460 logging function or macro. 1461 1462 Defaults to 'fprintf' 1463 1464 -- Macro: bool L_DEFAULT_GUARD 1465 The default guard condition for logging. 1466 1467 Defaults to 'TRUE'. 1468 1469 -- Macro: text L_DEFAULT_PARAMS 1470 The default parameter passed off to the logging function or macro. 1471 1472 Defaults to 'stderr' 1473 1474 -- Macro: void L_SHOW_TIME 1475 If defined then display the time in front of each message. 1476 1477 -- Macro: char* L_SHOW_TIME_FORMAT 1478 A format string for the time stamp in the log. By default it 1479 prints the time out in seconds. 1480 1481 -- Macro: value L_SHOW_TIME_NOW 1482 The name of a function that returns the time for the time stamp. 1483 This defaults to the 'now' function from 'now.h'. 1484 1485 1486File: nana.info, Node: L_buffer.h, Next: L_times.h, Prev: L.h, Up: Interface 1487 14884.6 L_buffer.h: a circular buffer for logging. 1489============================================== 1490 1491A traditional embedded systems trick is to log messages to a circular 1492buffer in core. This has the following benefits: 1493 1494 1. Speed - writing to a in core buffer is much faster than spitting 1495 out messages to a file on disk. It is often fast enough to leave 1496 at least most of the messages in the final product. 1497 2. Field debugging - what the ... was the user doing before the 1498 system crashed. Oh lets ask them, I'm sure they'll give us a good 1499 problem report. 1500 1501 -- Type: struct L_BUFFER 1502 Used to define buffer variables, it is similar to 'FILE*' type in 1503 'stdio.h'. To create an instance use 'L_buffer_create'. 1504 1505 -- Function: L_BUFFER* L_buffer_create (size_t SIZE) 1506 -- Function: L_BUFFER* L_buffer_delete (L_BUFFER *B) 1507 These are used to create or delete a buffer which can contain SIZE 1508 characters. 1509 1510 L_BUFFER *lbuffer; 1511 1512 lbuffer = L_buffer_create(32*1024); /* create a 32K buffer */ 1513 ...; 1514 L_buffer_delete(lbuffer); /* and delete it after use */ 1515 1516 -- Function: void L_buffer_wraparound (L_BUFFER *B, int W) 1517 A buffer created by 'L_buffer_create' is set up so that the new 1518 messages will overwrite the older messages in the buffer. If you 1519 wish to disable this overwriting, e.g. to keep the first 32K bytes 1520 of your system startup messages you should use 1521 'L_buffer_wraparound'. For example: 1522 1523 L_BUFFER *lb = L_buffer_create(32*1024); 1524 L_buffer_wraparound(lb, 0); /* disable wraparound */ 1525 1526 -- Function: void L_buffer_printf (L_BUFFER *B, const char *FMT, ...) 1527 -- Function: void L_buffer_puts (L_BUFFER *B, const char *STR) 1528 -- Function: void L_buffer_putchar (L_BUFFER *B, char CH) 1529 These are the routines which do that actual printing to the buffer. 1530 1531 L_buffer_printf(lbuffer, "U: user input %c\n", c); 1532 L_buffer_puts(lbuffer, "warning: its too hot"); 1533 L_buffer_putchar(lbuffer, '*'); 1534 1535 Note: a null pointer passed to the 'L_buffer_puts' function prints 1536 as '(null)'. (1) 1537 1538 -- Function: void L_buffer_clear (L_BUFFER *B) 1539 Clear the log, i.e. remove all messages and start again. 1540 1541 -- Function: void L_buffer_dump (L_BUFFER *B, FILE *FP) 1542 Dump the contents of the log *B to the file descriptor *FP. 1543 Typically *FP would be 'stderr'. 1544 1545 Note that this does not change the contents of the buffer. This is 1546 important since we may have a hardware or software problem part of 1547 the way through the dump operation and you don't want to loose 1548 anything. 1549 1550 To reset the buffer after a successful dump use 'L_buffer_clear'. 1551 1552 The output of 'L_buffer_dump' consists of a starting message 1553 followed by the contents of the log. If a character in the log is 1554 not printable we print it out in hex on a line by itself. 1555 1556 * L_buffer_dump = 1557 log message 1558 and another 1559 * non-printable character 0x1 1560 more log messages 1561 * end of dump 1562 1563 You also need to be able to integrate these functions into your 1564design. See 'examples/ott.c' for a complicated example. Here we will 1565provide a simplified version which implements a new logging macro called 1566'LFAST' which does a 'printf' to the 'log_buffer'. If you want to have 1567all messages going to a 'L_BUFFER' then you can redefine 1568'L_DEFAULT_HANDLER'. 1569 1570 /* project.h - the project wide include file */ 1571 1572 #include <nana.h> 1573 #include <L_buffer.h> 1574 1575 /* LFAST(char *, ...) - log a message to the log_buffer */ 1576 /* ##f translates as the rest of the arguments to LFAST */ 1577 1578 #define LFAST(f...) LHP(L_buffer_printf,log_buffer,##f) 1579 1580 extern L_BUFFER *log_buffer; /* the log buffer */ 1581 1582 The main program merely creates the LOG_BUFFER and eventually calls 1583'L_buffer_dump' to print out the buffer when the system dies. 1584 /* main.c - initialise the system and start things */ 1585 1586 #include <project.h> 1587 1588 L_BUFFER *log_buffer; 1589 1590 main() { 1591 log_buffer = L_buffer_create(16000); 1592 if(log_buffer == NULL) { /* not enough store */ 1593 ... 1594 } 1595 LFAST("system starting at %f\n", now()); 1596 ...; 1597 } 1598 1599 void fatal_error() { /* called on fatal errors */ 1600 FILE *f = fopen("project.errors","w"); 1601 L_buffer_dump(b, stderr); /* print log to stderr */ 1602 L_buffer_dump(b, f); /* print log to file */ 1603 } 1604 1605 ---------- Footnotes ---------- 1606 1607 (1) This was suggested by Phil Blecker. 1608 1609 1610File: nana.info, Node: L_times.h, Next: DL.h, Prev: L_buffer.h, Up: Interface 1611 16124.7 L_times.h: recording events and times. 1613========================================== 1614 1615This component is used to record events and times with a lower time and 1616space overhead than the 'L_buffer.h' component. Instead of using a 1617'printf' style string we simply record the time and a pointer to a 1618string identifying the event in a circular buffer. 1619 1620 *Note 0:* the string arguments should not be modified after a call 1621since we record pointers to the strings rather than the strings 1622themselves. 1623 1624 *Note 1:* there is no PRINTF style formatting, e.g. '%d' in this 1625package. 1626 1627 -- Type: struct L_TIMES 1628 Used to define buffers, it is similar to 'FILE*' type in 'stdio.h'. 1629 To create an instance use 'L_times_create'. 1630 1631 -- Function: L_TIMES* L_times_create (int SIZE) 1632 -- Function: L_TIMES* L_times_delete (L_BUFFER *B) 1633 These are used to create or delete a buffer which can contain SIZE 1634 messages. 1635 1636 -- Function: void L_times_wraparound (L_TIMES *B, int W) 1637 A buffer created by 'L_times_create' is set up so that the new 1638 messages will overwrite the oldest messages in the buffer. If you 1639 wish to disable this overwriting, e.g. to keep the first few 1640 messages messages you could use 'L_times_wraparound(b,0)'. 1641 1642 -- Function: void L_times_add (L_BUFFER *B, char *M, NANA_TIME T) 1643 Add an event identified by message M at time T to B. The type 1644 NANA_TIME defaults to 'double'. 1645 1646 -- Function: void L_times_dump (L_TIMES *B, FILE *FD) 1647 Dump the contents of the buffer out. 1648 1649 -- Function: void L_times_clear (L_TIMES *B) 1650 Clear all the messages from B. 1651 1652 1653File: nana.info, Node: DL.h, Next: GDB.h, Prev: L_times.h, Up: Interface 1654 16554.8 DL.h: support for printf style logging 1656========================================== 1657 1658These routines are used to provide logging functions. Messages can be 1659divided into classes and separately enabled and disabled. Note that 1660these macros have no effect unless you run your program under the 1661debugger and read in the commands generated by the 'nana' command. You 1662also need to compile the program with the '-g' option. 1663 1664 -- Macro: void DL (ARGS...) 1665 Used to log a message. 1666 1667 Defaults to a using 'fprintf' on 'stderr'. 1668 1669 -- Macro: void DLG (bool GUARD, ARGS...) 1670 -- Macro: void DLH (function HANDLER, ARGS...) 1671 -- Macro: void DLP (text PARAM, ARGS...) 1672 -- Macro: void DLGP (bool GUARD, text PARAM, ARGS...) 1673 -- Macro: void DLHP (function HANDLER, ARGS...) 1674 -- Macro: void DLGHP (bool GUARD, function HANDLER, ARGS...) 1675 And all of the special functions. 1676 1677 The macros such as 'DL' depend on the GNU CC variable number of 1678arguments to macros extension. If you wish to compile your code on 1679other systems you might wish to use the following variations on 'DL', 1680etc. 1681 1682 -- Macro: void VDL ((ARGS...)) 1683 -- Macro: void VDLG ((bool GUARD, ARGS...)) 1684 -- Macro: void VDLH ((function HANDLER, ARGS...)) 1685 -- Macro: void VDLP ((text PARAM, ARGS...)) 1686 -- Macro: void VDLGP ((bool GUARD, TEXT PARAM, ARGS...)) 1687 -- Macro: void VDLHP ((function HANDLER, ARGS...)) 1688 -- Macro: void VDLGHP ((bool GUARD, function HANDLER, ARGS...)) 1689 Each of these macros calls the corresponding function from the 1690 previous group, i.e. by default 'VDL' is equivelant to a call to 1691 'DL'. If 'WITHOUT_NANA' is defined then the call too 'VDL' is 1692 equivelant to '/* empty */'. 1693 1694 Thus you can have debugging under GCC whilst the code is still 1695 portable to other compilers. However debugging information will 1696 not be available on other platforms. 1697 1698 *Note:* the argument list is surrounded by *two* sets of brackets. 1699 For example: 1700 1701 VDL(("haze in darwin = %d\n", 3.4)); 1702 1703 -- Macro: int DL_LEVEL 1704 Used to enable and disable logging independently of guard 1705 expressions. 1706 1707 '2' 1708 Always print message 1709 '1' 1710 Print message only if the guard expression is true. 1711 '0' 1712 Never print any messages. 1713 1714 Defaults to '1'. 1715 1716 -- Macro: text DL_DEFAULT_HANDLER 1717 The default handler for printing which is simply the name of the 1718 printing function. 1719 1720 Defaults to 'printf' 1721 1722 -- Macro: bool DL_DEFAULT_GUARD 1723 1724 Defaults to 'TRUE'. 1725 1726 -- Macro: text DL_DEFAULT_PARAMS 1727 Defaults to 'stderr' 1728 1729 -- Macro: flag DL_SHOW_TIME 1730 Each message can be given an individual time stamp by defining 1731 'DL_SHOW_TIME'. This causes the '_L_gettime' routine to be called 1732 before each message which generates the timestamp. A default 1733 version is provided by the nana library. 1734 1735 1736File: nana.info, Node: GDB.h, Next: Q.h, Prev: DL.h, Up: Interface 1737 17384.9 GDB.h: sending plain gdb commands to the debugger 1739===================================================== 1740 1741'GDB.h' provides macros for generating user specified commands in the 1742output of the 'nana' command. They are not included by default in the 1743'nana.h' file. Note that these macros have no effect unless you run 1744your program under the debugger and read in the commands generated by 1745the 'nana' command. You also need to compile the program with the '-g' 1746option. 1747 1748 -- Macro: void GDB (COMMAND) 1749 Emit a single line command when running this file through 'nana'. 1750 Note that each line must be passed off separately to the 'GDB' 1751 macro. 1752 1753 This could be used to set debugger options or to define procedures 1754 inside 'gdb', e.g. 1755 1756 GDB(define checkstack); 1757 GDB( if 0 <= n && n <= 10); 1758 GDB( print "stack ok"); 1759 GDB( else); 1760 GDB( print "stack corrupted"); 1761 GDB( end); 1762 GDB(end); 1763 1764 -- Macro: void GDBCALL (COMMAND) 1765 Causes a single gdb COMMAND to be executed whenever control passes 1766 through this line of code. After the user's command is executed 1767 control automatically returns to the program. 1768 1769 GDBCALL(set memory_check = 1) 1770 1771 These macros could used for instrumenting code or setting up test 1772harnesses, e.g. 1773 1774 1775 GDB(set $siocall = 0); 1776 GDB(set $sioerr = 0); 1777 1778 void sio_driver() { 1779 GDBCALL(set $siocall++) 1780 if(SIO_REQ & 0x010) { 1781 GDBCALL(set $sioerr++); 1782 ... 1783 } 1784 } 1785 1786 1787File: nana.info, Node: Q.h, Next: Qstl.h, Prev: GDB.h, Up: Interface 1788 17894.10 Q.h: support for quantifiers 1790================================= 1791 1792'Q.h' provides support for the quantifiers of predicate logic. For 1793example to check that all elements in a data structure have some 1794property we would use universal (forall, upside down A) quantification. 1795To check that one or more values in a data structure have some property 1796we would use existential (exists, back the front E) quantification. For 1797example: 1798 1799 /* all values in a[] must be between 0 and 10 */ 1800 I(A(int i = 0, i < n_array, i++, 0 <= a[i] && a[i] <= 10)); 1801 1802 /* there exists a value in linked list l which is smaller than 10 */ 1803 I(E(node *p = l, p != NULL, p = p->next, p->data <= 10)); 1804 1805 The first three arguments to 'A' and 'E' are similar to a C 'for' 1806loop which iterates over the values we wish to check. The final 1807argument is the expression that must be true. 1808 1809 The only minor difference from the C 'for' loop is that variables may 1810be declared at the start of the loop, even if you are using C rather 1811than C++ which already supports this.(1) 1812 1813 The 'Q.h' macros can also be nested and used anywhere a boolean value 1814is required. For example: 1815 1816 if(A(int i = 0, i < MAXX, i++, 1817 A(int j = 0, j < MAXY, j++, 1818 m[i][j] == (i == j ? 1 : 0)))) { 1819 /* identity matrix, i.e. all 0's except for 1's on */ 1820 /* the diagonal */ 1821 ... 1822 } else { 1823 /* not an identity matrix */ 1824 ... 1825 } 1826 1827 The results from these macros can also be combined using boolean 1828operations, e.g. 1829 1830 /* the values in a[i] are either ALL positive or ALL negative */ 1831 I(A(int i = 0, i < MAX, i++, a[i] >= 0) 1832 || 1833 A(int i = 0, i < MAX, i++, a[i] < 0)); 1834 1835 *Portability:* note the macros in this file require the GNU CC/C++ 1836statement expression extension of GCC to work. If you're not using GNU 1837CC then for now you are out of luck. At some time in the future we may 1838implement a method which will work for standard C++, standard C is a bit 1839of a challenge. 1840 1841 *Portability:* unfortunately these macros do not work for the 'DI' 1842and 'DL' macros since the statement expression extension has not been 1843implemented in GDB. 1844 1845 -- Macro: bool A (INIT,CONDITION,NEXT,EXPRN) 1846 For all values generated by 'for(INT;CONDITION;NEXT)' the EXPRN 1847 must be true. 1848 I(A(int i = 0, i < MAX, i++, a[i] >= 0)); /* all a[i] are +ve */ 1849 1850 -- Macro: bool E (INIT,CONDITION,NEXT,EXPRN) 1851 There exists at least one value for EXPRN generated by 'for 1852 (INT;CONDITION;NEXT)' which is true. 1853 1854 /* one or more a[i] >= 0 */ 1855 I(E(int i = 0, i < MAX, i++, a[i] >= 0)); 1856 1857 -- Macro: long C (INIT,CONDITION,NEXT,EXPRN) 1858 Returns the number of times the EXPRN is true over the values 1859 generated by 'for(INT;CONDITION;NEXT)'. 1860 1861 /* 3 elements of a[] are +ve */ 1862 I(C(int i = 0, i < MAX, i++, a[i] >= 0) == 3); 1863 1864 -- Macro: bool E1 (INIT,CONDITION,NEXT,EXPRN) 1865 There exists only one value generated by 'for(INT;CONDITION;NEXT)' 1866 for which the EXPRN is true. 1867 1868 /* a single elements of a[] is +ve */ 1869 I(E1(int i = 0, i < MAX, i++, a[i] >= 0)); 1870 1871 -- Macro: typeof( EXPRN ) S (INIT,CONDITION,NEXT,EXPRN) 1872 Sum the values generated by EXPRN for all values given by 1873 'for(INT;CONDITION;NEXT)'. The type of the value returned is given 1874 by the type of the EXPRN.(2) 1875 1876 /* sum of a[] is 10 */ 1877 I(S(int i = 0, i < MAX, i++, a[i]) == 10); 1878 1879 /* sum of all +ve numbers in a[] is 10 */ 1880 I(S(int i = 0, i < MAX, i++, a[i] >= 0 ? a[i] : 0) == 10); 1881 1882 -- Macro: typeof( EXPRN ) P (INIT,CONDITION,NEXT,EXPRN) 1883 Returns the product of the values generated by EXPRN for all values 1884 given by 'for(INT;CONDITION;NEXT)'. The type returned is the type 1885 of the expression. 1886 1887 /* product of all the values in a[] is 10 */ 1888 I(P(int i = 0, i < MAX, i++, a[i]) == 10); 1889 1890 /* a = x^y i.e. x*x..*x y times */ 1891 I(P(int i = 0, i < y, i++, x) == a); 1892 1893 ---------- Footnotes ---------- 1894 1895 (1) ANSI C does not allow variable declarations at the beginning of 1896loops unlike C++. The 'Q.h' macros get around this by starting each 1897loop with a new scope. 1898 1899 (2) This uses yet another GNU CC extension, however since we are 1900already using statement expressions we might as well use 'typeof' as 1901well. 1902 1903 1904File: nana.info, Node: Qstl.h, Next: now.h, Prev: Q.h, Up: Interface 1905 19064.11 Qstl.h: quantifiers for STL containers. 1907============================================ 1908 1909The Standard Template Library (STL) is a library for C++ that makes 1910extensive use of templates to implement the standard container classes 1911and much more. Each of the container classes provides an interface to 1912iterate over all the objects in the container, e.g. 1913 1914 // MAP is an associate array from location(lat,long) onto the name. 1915 typedef map<location,string,locationlt> MAP; 1916 1917 void print_map_names(MAP& m) { // print out all the names in the map 1918 for(MAP::iterator i = m.begin(); i != m.end(); ++i) { 1919 cout << (*i).second << "\n"; 1920 } 1921 } 1922 1923 'Qstl.h' provides the same facilities as 'Q.h' but uses the standard 1924STL iterator protocol shown above. The names in 'Qstl.h' are generated 1925by appending a 'O' (O not zero!) to the names in 'Q.h'. In particular: 1926 1927 -- Macro: bool AO (NAME,CONTAINER,PREDICATE) 1928 For all values in the CONTAINER class the PREDICATE must be true. 1929 The PREDICATE refers to individual values using NAME. See the STL 1930 documentation for more details. Another way of putting this is 1931 forall NAME in CONTAINER the PREDICATE must be true. 1932 1933 map<int,char *,ltint> m; 1934 // all keys (or indexes) into m are positive 1935 I(AO(i, m, (*i).first >= 0)); 1936 1937 -- Macro: bool EO (NAME,CONTAINER,PREDICATE) 1938 There exists one or more values in the CONTAINER class for which 1939 the PREDICATE is true. 1940 1941 map<int,char,ltint> m; 1942 1943 // one or more characters in m are '$' 1944 I(EO(i, m, (*i).second == '$')); 1945 1946 -- Macro: bool E1O (NAME,CONTAINER,PREDICATE) 1947 There exists one value in the CONTAINER for which the PREDICATE is 1948 true. 1949 1950 map<int,char,ltint> m; 1951 1952 // one characters in m is a '$' 1953 I(E1O(i, m, (*i).second == '$')); 1954 1955 -- Macro: int CO (NAME,CONTAINER,PREDICATE) 1956 Returns the number of times the PREDICATE was true for all values 1957 in the CONTAINER. 1958 1959 map<int,char,ltint> m; 1960 int nalpha; 1961 // count the number of alphabetic chars in the map 1962 nalpha = CO(i, m, isalpha((*i).second)); 1963 1964 -- Macro: typeof( EXPRN ) SO (NAME,CONTAINER,EXPRN) 1965 Sum the EXPRN for all values in the CONTAINER. 1966 1967 map<int,float,ltint> m; 1968 float sum; 1969 // sum all the values in m 1970 sum = SO(i, m, (*i).second); 1971 1972 -- Macro: typeof( EXPRN ) PO (NAME,CONTAINER,EXPRN) 1973 Take the product of the EXPRN for all values in the CONTAINER. 1974 1975 map<int,float,ltint> m; 1976 float product; 1977 // multiply all the values in m 1978 product = PO(i, m, (*i).second); 1979 1980 1981File: nana.info, Node: now.h, Next: cycles.h, Prev: Qstl.h, Up: Interface 1982 19834.12 now.h: measuring time 1984========================== 1985 1986The 'now.h' file provides some simple time measurement routines. It is 1987_not_ included in 'nana.h' so you must include this file separately. 1988 1989 It uses the 'gettimeofday' system call and has an accuracy of between 19901us and 10ms depending on the operating system and hardware 1991configuration. 1992 1993 See the IPM package if you require better measurement tools.(1) 1994 1995 -- Function: double now () 1996 Returns the time in seconds since the beginning of time as defined 1997 by your system. If you call 'now_reset' the time will start again 1998 at 0. 1999 2000 -- Function: double now_reset () 2001 Reset the times returned by 'now' to 0. 2002 2003 -- Function: double now_delta (double *DP) 2004 Returns the elapsed time between *DP and NOW(). It then sets *DP 2005 to NOW, thus giving a delta time between particular events. 2006 2007 t = now(); 2008 for(;;) { 2009 ...; /* code that must finish in 50ms */ 2010 I(now_delta(&t) <= 0.050); 2011 } 2012 2013 ---------- Footnotes ---------- 2014 2015 (1) In the fullness of time, we may integrate these routines in here. 2016 2017 2018File: nana.info, Node: cycles.h, Next: eiffel.h, Prev: now.h, Up: Interface 2019 20204.13 cycles.h: access to CPU cycle counting registers. 2021====================================================== 2022 2023Some modern CPU's provide user accessible registers or special 2024intstructions which can access a counter driven directly by the CPU 2025clock. The 'cycles.h' library provides access to these instructions 2026together with some calibration and utility routines. 2027 2028 Currently we only provide support for Pentium/Cyrix machines using 2029the 'RDTSC' instruction. If you want to use these routines you need to 2030run the 'configure' script with the '--enable-rdtsc' option. Other 2031machine architectures will be supported as time goes on. 2032 2033 -- typedef: CYCLES long long 2034 The CPU cycle measurement type, typically a 64 bit unsigned 2035 integer. 2036 2037 -- Macro: CYCLES cycles () 2038 Returns the current value for the cycle counter. 2039 2040 -- Function: CYCLES cycles_per_second (double T, int N) 2041 Returns an estimate of the number of cycles per second using the 2042 'now.h' library. The measurement is taken N times using a 2043 measurement period of T seconds for each measurement. The minimum 2044 and maximum values for the measurement are set by each call to 2045 'cycles_per_second' and are available from the next two functions. 2046 2047 -- Function: CYCLES cycles_per_second_min () 2048 -- Function: CYCLES cycles_per_second_max () 2049 Return the minimum or maximum of the measurements carried out by 2050 the previous call to 'cycles_per_second'. 2051 2052 -- Function: double cycles_diff (CYCLES START, CYCLES STOP) 2053 Returns the time difference between START and STOP cycles in 2054 seconds as a double. As usual there are a few requirements: 2055 2056 * 'cycles_per_second' must be called before hand to calibrate 2057 the cycle time with the real time clock. 2058 * START must be less than or equal to STOP. Note we do not 2059 handle wraparound currently since the counters start at 0 and 2060 are 64 bits long and so will not overflow in a reasonable 2061 period. (1) 2062 2063 * The difference between the START and STOP times should be able 2064 to be represented in a 'double', lest overflow and misery 2065 follow. 2066 * CPU clocks tend to vary a bit with temperature etc, trust this 2067 and die. 2068 2069* Menu: 2070 2071* RDTSC:: 2072 2073 ---------- Footnotes ---------- 2074 2075 (1) Famous last words I know but: (2^64)/(1e9*60*60*24*365) = 584 2076yrs. 2077 2078 2079File: nana.info, Node: RDTSC, Prev: cycles.h, Up: cycles.h 2080 20814.13.1 RDTSC: cycle timing for Pentium, Cyrix, etc 2082-------------------------------------------------- 2083 2084The RDTSC instruction is used for cycle timing on Pentiums and other 2085compatible CPUs such as the Cyrix chip set. Note that this instruction 2086does _not_ exist on earlier CPUs in the series. 2087 2088 We could of course try to discover the CPU type at compile or run 2089time and then use the appropriate instruction. This has all sorts of 2090problems, e.g. if we compile on a i586 does that mean it will be run on 2091the same CPU (no of course not....). 2092 2093 For now we use the '--enable-rdtsc' option for './configure'. 2094 2095 2096File: nana.info, Node: eiffel.h, Next: assert.h, Prev: cycles.h, Up: Interface 2097 20984.14 eiffel.h: eiffel type assertions 2099===================================== 2100 2101Eiffel is a very nice language which provides the assertion checking 2102facilities of nana inside the language itself. The 'eiffel.h' library 2103is intended to provide a similar setup to Eiffel in the C++ language. 2104 2105* Menu: 2106 2107* EIFFEL_CHECK:: 2108* DOEND:: 2109* REQUIRE...:: 2110 2111 2112File: nana.info, Node: EIFFEL_CHECK, Next: DOEND, Prev: eiffel.h, Up: eiffel.h 2113 21144.14.1 EIFFEL_CHECK: enabling and disabling checking. 2115----------------------------------------------------- 2116 2117Assertion checking is controlled by the EIFFEL_CHECK macro which can 2118take on any of the following values: 2119 2120'CHECK_NO' 2121 Disable all checking. 2122'CHECK_REQUIRE' 2123 Check the preconditions for each method. 2124'CHECK_ENSURE' 2125 And also check the postconditions. 2126'CHECK_INVARIANT' 2127 And also check the class invariant before and after each method is 2128 called. The programmer should provide a class method called 2129 'invariant' which returns 'true' if the object is consistent, 2130 'false' otherwise. 2131'CHECK_LOOP' 2132 And also check the loop invariants. 2133'CHECK_ALL' 2134 And also check any assertions using the 'CHECK' instruction. 2135 2136 Note that the default value for 'EIFFEL_CHECK' is 'CHECK_REQUIRE', 2137i.e. check preconditions only. 2138 2139 A typical compile flag to the compile might be: 2140 2141 % g++ -c -DEIFFEL_CHECK=CHECK_ALL play.cc 2142 2143 2144File: nana.info, Node: DOEND, Next: REQUIRE..., Prev: EIFFEL_CHECK, Up: eiffel.h 2145 21464.14.2 DOEND: adding DO ... END 2147------------------------------- 2148 2149At the suggestion of Bertrand Meyer (Eiffel's author) the 'DO' and 'END' 2150macros have been added to 'eiffel.h'. Note that these are only 2151available if you define the 'EIFFEL_DOEND' macro. To use these macros 2152each of your methods should use 'DO' ... 'END' as their outermost 2153brackets. For example: 2154 2155 // compiled with EIFFEL_DOEND defined 2156 void Stack::push(int n) 2157 DO // checks the class invariant + { 2158 ... 2159 END // check the class invariant + } 2160 2161 If you do _not_ define the 'EIFFEL_DOEND' macro then 'eiffel.h' 2162reverts to its old behaviour where 'REQUIRE' and 'ENSURE' also check the 2163class invariant. Thus to check the class invariant when you are not 2164using 'DO' and 'END' you would need to call 'REQUIRE' and 'ENSURE', for 2165example: 2166 2167 // compile with EIFFEL_DOEND undefined (i.e. old behaviour) 2168 void Stack::push(int n) 2169 { 2170 REQUIRE(true); // checks the invariant as well as the precondition 2171 2172 ENSURE(true); // checks the invariant as well as the postcondition 2173 } 2174 2175 As for which one to option to pick, Bertrand Meyer is in favour of 2176the 'DO' ... 'END' solution. 2177 2178 2179File: nana.info, Node: REQUIRE..., Prev: DOEND, Up: eiffel.h 2180 21814.14.3 REQUIRE, ENSURE, CHECK, etc. 2182----------------------------------- 2183 2184Here are the individual checking macros: 2185 2186 -- Macro: void REQUIRE (EXPRN) 2187 Called at the beginning of each method to check its precondition 2188 (requirements). For example: 2189 2190 void Stack::push(int n) { 2191 REQUIRE(!full()); // stack has space for push 2192 ... 2193 } 2194 2195 If 'EIFFEL_DOEND' is not defined this also checks the class 2196 invariant. 2197 2198 -- Macro: void ENSURE (EXPRN) 2199 Called at the end of each method. This checks the postcondition 2200 for a method and the class invariant. 2201 2202 void Stack::push(int n) { 2203 ... 2204 ENSURE(!empty()); // it can't be empty after a push! 2205 } 2206 2207 If 'EIFFEL_DOEND' is not defined this also checks the class 2208 invariant. 2209 2210 -- Macro: void INVARIANT (EXPRN) 2211 Used to check a loop invariant. 2212 2213 -- Macro: void CHECK (EXPRN) 2214 Used for any other inline assertions. For example: 2215 2216 CHECK(z != 0); 2217 x = y / z; 2218 2219 And finally a small example: 2220 2221 #include <eiffel.h> 2222 2223 class example { 2224 int nobjects; 2225 map<location,string,locationlt> layer; 2226 public: 2227 bool invariant(); // is this object consistent 2228 void changeit(location l); 2229 }; 2230 2231 bool example::invariant() { 2232 return AO(i,layer,valid_location((*i).first)) && 2233 nobjects >= 0; 2234 } 2235 2236 void example::changeit(string n, location l) { 2237 REQUIRE(E1O(i,layer,(*i).second == n)); 2238 ...; 2239 while(..) { 2240 INVARIANT(...); 2241 ... 2242 INVARIANT(...); 2243 } 2244 ... 2245 CHECK(x == 5); 2246 ... 2247 ENSURE(layer[l] == n); 2248 } 2249 2250 Note that the invariant checking macro 'example::invariant' is called 2251automatically on function entry/exit using the 'REQUIRE' and 'ENSURE' 2252macros if 'EIFFEL_CHECK' is not defined. 2253 2254 2255File: nana.info, Node: assert.h, Next: calls.h, Prev: eiffel.h, Up: Interface 2256 22574.15 assert.h: a drop in replacement for assert.h 2258================================================= 2259 2260A drop in replacement for 'assert.h' is provided in the 'src' directory. 2261It is *not* installed by default. If you wish to use it then you need 2262to copy it to your include directory by hand. 2263 2264 This might be of use if you are already using 'assert.h' and wish to 2265save some code space since the nana implementation is more space 2266efficient. 2267 2268 Calls to 'assert' are translated to calls to 'I' and can be disabled 2269by defining 'NDEBUG'. 2270 2271 2272File: nana.info, Node: calls.h, Prev: assert.h, Up: Interface 2273 22744.16 calls.h: checking/printing many objects/facts. 2275=================================================== 2276 2277The 'calls' module implements a simple list of functions which can be 2278modified and executed at run-time. It is similar in spirit to the ANSI 2279C 'atexit' function. It is intended to be used for: 2280 2281 * Checking the consistency of the components in your system. 2282 2283 For example each module could register a self checking function 2284 which uses the rest of the nana library. All of these functions 2285 would then be called using 'calls.h' to check that the entire 2286 system is consistent. 2287 2288 * Printing out the state of your program in a readable format. 2289 2290 -- Type: typedef FUNC 2291 A pointer to a 'void' function which takes a single 'void*' 2292 argument. The 'void *' argument is intended to be used to pass 2293 information such as arguments or pointers to objects (e.g. 'this' 2294 in C++). All of the checking/printing functions must be of this 2295 type, e.g. 2296 2297 void print_object(void *f) { 2298 ...; 2299 } 2300 2301 -- Type: struct CALL 2302 This structure represents a single call to a function, i.e. a 2303 function pointer ('FUNC') and the 'void*' argument. 2304 2305 CALL *head = 0; 2306 2307 -- Function: void calls_add (CALL **head, FUNC fp, *arg) 2308 Adds a call to function 'fp' with argument 'arg' to the list 2309 pointed to by 'head'. 2310 2311 CALL *global_checks = 0; 2312 2313 calls_add(&global_checks,complex_ok,(void *)x); 2314 2315 -- Function: void calls_exec (CALL **head, FUNC fp, void *arg) 2316 Execute all/some of the calls in the list given by 'head'. The 2317 arguments 'fp' and 'arg' must both match for each individual call. 2318 The null pointer ('0') matches anything whilst any other value 2319 requires an exact match between the 'CALL' and the arguments to 2320 'calls_exec'. For example: 2321 2322 calls_exec(&l,0,0); /* execute all functions in l */ 2323 calls_exec(&l,complex_print,0); /* calls complex_print(*) in l */ 2324 calls_exec(&l,0,(void*) &b); /* calls *(&b) in l */ 2325 calls_exec(&l,f,(void*) &b); /* calls f(&b) in l */ 2326 2327 -- Function: void calls_delete (CALL **head, FUNC fp, void *arg) 2328 Delete all/some of the calls in the list given by 'head'. The 2329 arguments 'fp' and 'arg' must both match for each individual call. 2330 The null pointer ('0') matches anything whilst any other value 2331 requires an exact match between the 'CALL' and the arguments to 2332 'calls_delete'. For example: 2333 2334 calls_delete(&l,0,0); /* delete all functions in l */ 2335 calls_delete(&l,complex_print,0); /* delete complex_print(*) in l */ 2336 calls_delete(&l,0,(void*) &b); /* delete *(&b) in l */ 2337 calls_delete(&l,f,(void*) &b); /* delete f(&b) in l */ 2338 2339 *Note:* that calls are added to the head of the list rather than the 2340tail. This means that the most recently added call will be executed 2341first (as in a stack). 2342 2343 2344File: nana.info, Node: Shortform, Next: Performance, Prev: Interface, Up: Top 2345 23465 Nana Shortform Generator. 2347*************************** 2348 2349The Eiffel language provides a shortform of a class which consists of 2350the exported methods and their pre and post conditions. The private 2351part of the class such as the code is hidden in this form leaving only: 2352 2353 1. Arguments and return values for methods. 2354 2. 'REQUIRE' and 'ENSURE' calls which specify the precondition and 2355 postconditions of each method. 2356 2357 This is useful to provide a summary of what the code does and how to 2358use it rather than how it works. 2359 2360 Nana provides a similar service which can be used to generated a HTML 2361version of the short form of your program automatically. The code for 2362this is kept in 'shortform'. Do a 'make example' to build an example 2363document.(1) 2364 2365 Consider the following program: 2366 2367 /* smallex.c - a small example */ 2368 2369 #include <stdio.h> 2370 #include <math.h> 2371 #include <eiffel.h> 2372 2373 void sort(int *v, int n) { 2374 int i; 2375 REQUIRE(v != NULL && 2376 0 <= n); 2377 2378 for(i = 0; < n; i++) { /* at last, an O(n) sort! */ 2379 v[i] = i; 2380 } 2381 /* And no, this isn't what most people think of as sorting */ 2382 2383 ENSURE(A(int i = 0, i < n - 1, i++, 2384 v[i] <= v[i+1])); 2385 } 2386 2387 Its short form can be generated by using the 'nana-sfg'(2) program 2388which generates: 2389 2390 % nana-sfg smallex.c 2391 ... 2392 #include <stdio.h> 2393 #include <math.h> 2394 #include <eiffel.h> 2395 ... 2396 void sort(int *v, int n) { 2397 ... 2398 REQUIRE(v != NULL && 2399 n >= 0); 2400 ... 2401 ENSURE(A(int i = 0, i < n, i++, 2402 v[i] <= v[i+1])); 2403 } 2404 % 2405 2406 The 'nana-sfg' program is a small AWK program which processes its 2407arguments into shortform and always writes to the standard output. If 2408it is passed no arguments it works as a normal UNIX filter reading from 2409the standard input. 2410 2411 It is suggested that a copy of 'nana-sfg' be kept in each projects 2412'bin' directory so that it can be modified for local taste. The user 2413will probably wish to modify the rules for short form generation. For 2414example you might add rules such as: 2415 2416 /^\/\// { emit(); } # print out C++ comments in column 1 2417 /^\/\*\+/,/\*\// { emit(); } # print out multi-line /*+ ... */ comments 2418 2419 Of course for a real project you need to run 'nana-sfg' over the 2420entire source tree. To do this you can use the 'nana-sfdir' program. 2421 2422 % nana-sfdir 2423 2424 This command simply creates a copy of the source tree in the current 2425directory under 'NANASF' using the 'nana-sfg' program. You can then run 2426a source code to HTML translator over the 'NANASF' directory. Currently 2427we are using the GLOBAL package which was written by Shigio Yamaguchi 2428which available from: 2429 2430 * 'http://wafu.netgate.net/tama/unix/global.html' - the GLOBAL 2431 homepage. 2432 * 'ftp://ftp.cs.ntu.edu/pub/nana/global-2.24.tar.gz' - a local (well 2433 for Darwin at least) copy of the GLOBAL package. 2434 2435 The alert reader will perhaps be asking themselves why we did not 2436simply modify GLOBAL. Well that was the original idea, however after a 2437bit of thinking it seemed better to separate the generation of the short 2438form of the code from the generation of the HTML. This gives us the 2439ability to use other translators and other tools. It also simplifies 2440the interaction between nana and GLOBAL. For information on other 2441translators see: 2442 2443 * 'http://www.zib.de/Visual/software/doc++/index.html' - DOC++ 2444 homepage. 2445 * 'http://www.webnz.com/webnz/robert/cpp_site.html#Documentation' - 2446 an index of other translation tools (e.g. to LaTeX). 2447 2448 ---------- Footnotes ---------- 2449 2450 (1) Note you need to install the GLOBAL package first. This is 2451installed by default on FreeBSD systems. If you do not have the GLOBAL 2452package read on. 2453 2454 (2) The name 'nana-sfg' stands for either Nana Short Form Generator 2455or Nana Science Fiction Generator. Personally I prefer the later 2456derivation. 2457 2458 2459File: nana.info, Node: Performance, Next: Tracing, Prev: Shortform, Up: Top 2460 24616 Nana Performance Measurement 2462****************************** 2463 2464A tool is provided for measuring code/time requirements for arbitrary 2465code fragments. This is kept in the 'perf' directory and is *not* built 2466by default. If you wish to use this tool use the following targets: 2467 2468 % cd perf 2469 % make perf 2470 2471 The output is 'perf.tex', 'perf.dvi' and 'perf/index.html'. 2472 2473 Note that the measurement requires the following: 2474 2475 * GNU CC - it uses the GNU address of label extension to calculate 2476 the size in bytes of a code fragment. 2477 * Time is measured using the nana 'now()' function. 2478 * LaTeX - to generate the document from 'perf.tex'. 2479 * LaTeX2HTML - to generate a HTML version of 'perf.tex'. 2480 2481 As an indication of the values you can expect here is part of the 2482results for 'make perf' on a 200Mhz Cyrix MMX (i386) chip which runs at 2483about 200 BogoMips under FreeBSD 2.2.6 with '-O'. 2484 2485 'assert(i >= 2);' 28 bytes, 19ns. 2486 2487 'TRAD_assert(i >= 2);' 47 bytes, 20ns.(1) 2488 2489 'I(i >= 2);' 9 bytes, 18ns. 2490 2491 'DI(i >= 2);' 1 byte, 147.4us. 2492 2493 'I(A(int i=0, i!=10, i++, a[i]>=0));' 28 bytes, 287ns. 2494 2495 'd = now();' 8 bytes, 3.1us. 2496 2497 'printf("helloworld\n");' 13 bytes, 9.1us. 2498 2499 'L("helloworld\n");' 18 bytes, 8.9us. 2500 2501 'DL("helloworld\n");' 1 byte, 26.4us. 2502 2503 Note that these measurements were on a system that was configured 2504with 'I_DEFAULT=fast ./configure'. The default output of './configure' 2505produces nice error messages at the cost of increased code space. 2506 2507 ---------- Footnotes ---------- 2508 2509 (1) This is the traditional assert which uses 'fprintf' and 'exit' in 2510a macro. The BSD 'assert' macro used in FreeBSD is a bit smarter and 2511calls a function to do the message printing and exiting. Note that the 2512real cost of this function is even higher since we are only measuring 2513the code space requirements, not the space required for the message 2514strings. 2515 2516 2517File: nana.info, Node: Tracing, Next: Usage, Prev: Performance, Up: Top 2518 25197 Tracing tools 2520*************** 2521 2522A few tools for execution tracing and logging are available in the 'gdb' 2523directory and are installed by default. They are simple shell scripts 2524and may be of some use in testing/development. Note that 'gdb-4.17' may 2525be required on some machines for this stuff to work properly. 2526 2527* Menu: 2528 2529* Statement:: 2530* Library:: 2531 2532 2533File: nana.info, Node: Statement, Next: Library, Prev: Tracing, Up: Tracing 2534 25357.1 Statement level tracing 2536=========================== 2537 2538The 'nana-trace' executes a program and generates a message for each 2539line of code executed (a statement trace). The statement level trace is 2540useful for things such as: 2541 2542 * Understanding code. 2543 * Measuring test coverage. 2544 * Comparing runs of the code when regression testing, e.g. verifying 2545 that change X only changes the behaviour of program P for test case 2546 Z. 2547 2548 For example the 'make ex-trace' command in 'gdb' generates: 2549 2550 % make ex-trace 2551 gcc -g test.c 2552 sh ./nana-trace a.out 2553 47 setbuf(stdout, NULL); /* disable buffering */ 2554 49 printf("** main()\n"); 2555 ** main() 2556 50 printf("** 1: %d\n", distance(1,-5)); 2557 distance (i=1, j=-5) at test.c:43 2558 43 return abs(i - j); 2559 abs (i=6) at test.c:35 2560 35 if(i >= 0) { 2561 36 return i; 2562 40 } 2563 distance (i=1, j=-5) at test.c:44 2564 44 } 2565 ** 1: 6 2566 main () at test.c:51 2567 51 printf("** 2: %d\n", distance(twice(1),-5)); 2568 twice (i=1) at test.c:29 2569 29 i = i * 2; 2570 31 return i ; 2571 32 } 2572 distance (i=2, j=-5) at test.c:43 2573 43 return abs(i - j); 2574 abs (i=7) at test.c:35 2575 35 if(i >= 0) { 2576 36 return i; 2577 40 } 2578 distance (i=2, j=-5) at test.c:44 2579 44 } 2580 ** 2: 7 2581 main () at test.c:52 2582 52 printf("** 3: %d\n", distance(3,-5)); 2583 distance (i=3, j=-5) at test.c:43 2584 43 return abs(i - j); 2585 abs (i=8) at test.c:35 2586 35 if(i >= 0) { 2587 36 return i; 2588 40 } 2589 distance (i=3, j=-5) at test.c:44 2590 44 } 2591 ** 3: 8 2592 main () at test.c:53 2593 53 } 2594 2595 2596File: nana.info, Node: Library, Prev: Statement, Up: Tracing 2597 25987.2 Library tracing 2599=================== 2600 2601On most UNIX machines there exists a tool for tracing the system calls 2602executed by a program. If you haven't used one of these tools (e.g. 2603ktrace) then now is a good time to learn. Being able to trace the 2604User/Kernel interface is an excellent way to understand complex 2605applications. 2606 2607 The 'nana-libtrace' facility generalises this idea to provide a GDB 2608based function call tracer for all functions in which are defined in a 2609particular library. For example the 'make ex-libtrace' command in the 2610'gdb' directory produces a trace of all calls to 'libc'. 2611 2612 * 'nana-libtrace a.out' - reads a list of function names one per line 2613 from the standard input and writes the corresponding gdb(1) script 2614 to the standard output. Note that all functions specified in the 2615 input must exist in the executable for them to be passed through to 2616 the output. This may require compilation of your program with 2617 '-static' linking.(1) For example: 2618 2619 % gcc -g -static test.c -lm 2620 % ./nana-libtrace a.out >test.gdb 2621 printf 2622 write 2623 % cat test.gdb 2624 break printf 2625 command $bpnum 2626 silent 2627 where 1 2628 cont 2629 end 2630 break write 2631 command $bpnum 2632 silent 2633 where 1 2634 cont 2635 end 2636 % nana-run a.out -x test.gdb 2637 Breakpoint 1 at 0x1483: file /usr/.../libc/stdio/printf.c, line 65. 2638 Breakpoint 2 at 0x8c78 2639 #0 printf (fmt=0x1116 "** main()\n") 2640 at /usr/src/lib/libc/../libc/stdio/printf.c:65 2641 #0 0x8c78 in write () 2642 ** main() 2643 #0 printf (fmt=0x1121 "** 1: %d\n") 2644 at /usr/src/lib/libc/../libc/stdio/printf.c:65 2645 #0 0x8c78 in write () 2646 ** 1: 6 2647 #0 printf (fmt=0x112b "** 2: %d\n") 2648 at /usr/src/lib/libc/../libc/stdio/printf.c:65 2649 #0 0x8c78 in write () 2650 ** 2: 7 2651 #0 printf (fmt=0x1135 "** 3: %d\n") 2652 at /usr/src/lib/libc/../libc/stdio/printf.c:65 2653 #0 0x8c78 in write () 2654 ** 3: 8 2655 2656 Program exited with code 010. 2657 2658 * 'nana-libtrace a.out /usr/lib/libc.a' - trace all calls to 'libc' 2659 from 'a.out'. A subset of the output is given below: 2660 2661 #0 printf (fmt=0x1135 "** 3: %d\n") 2662 at /usr/src/lib/libc/../libc/stdio/printf.c:65 2663 #0 vfprintf (fp=0xa1d4, fmt0=0x1135 "** 3: %d\n", ap=0xefbfd888 "\b") 2664 at /usr/src/lib/libc/../libc/stdio/vfprintf.c:428 2665 #0 vfprintf (fp=0xefbfd5b8, fmt0=0x1135 "** 3: %d\n", ap=0xefbfd888 "\b") 2666 at /usr/src/lib/libc/../libc/stdio/vfprintf.c:428 2667 #0 __sfvwrite (fp=0xefbfd5b8, uio=0xefbfd184) 2668 at /usr/src/lib/libc/../libc/stdio/fvwrite.c:68 2669 #0 0x8ff8 in memcpy () 2670 #0 0x8ff8 in memcpy () 2671 #0 __sfvwrite (fp=0xefbfd5b8, uio=0xefbfd184) 2672 at /usr/src/lib/libc/../libc/stdio/fvwrite.c:68 2673 #0 0x8ff8 in memcpy () 2674 #0 fflush (fp=0xefbfd5b8) at /usr/src/lib/libc/../libc/stdio/fflush.c:60 2675 #0 __sflush (fp=0xefbfd5b8) at /usr/src/lib/libc/../libc/stdio/fflush.c:84 2676 #0 __swrite (cookie=0xa1d4, buf=0xefbfd1b8 "** 3: 8\nain()\n", n=8) 2677 at /usr/src/lib/libc/../libc/stdio/stdio.c:78 2678 #0 0x8c78 in write () 2679 ** 3: 8 2680 2681 Note that your library code must be compiled with '-g' to put in 2682 the debugging information if you wish to have the arguments 2683 displayed symbolically. This is fairly easy on most systems, e.g 2684 on FreeBSD you edit the global compile options '/etc/make.conf' and 2685 rebuild/install the entire system. 2686 2687 Ideally we would also like to be able to trace only entry calls and 2688 not internal calls to libc. This can be done by inserting a 2689 counter which is incremented on call and decremented on return. We 2690 print the trace messages iff the counter is 1. This extension 2691 (perhaps to GNU libc) may be written if people are interested. 2692 2693 ---------- Footnotes ---------- 2694 2695 (1) If possible we should replace the call to 'nm' with a call to 2696something which can print all the symbols for a dynamically linked 2697library. Unfortunately GDB gets upset if you try to set a breakpoint 2698for a function that does not exist. I suppose we could use gdb to print 2699the symbol list out. 2700 2701 2702File: nana.info, Node: Usage, Next: FAQ, Prev: Tracing, Up: Top 2703 27048 Using Nana 2705************ 2706 2707This chapter is intended to provide some hopefully useful examples of 2708Nana. If any of the users of this library would be so kind as to 2709contribute a section on their usage I would be obliged. 2710 2711 *This section is under development* 2712 2713* Menu: 2714 2715* Simplest:: 2716* Syslog:: 2717* GNU:: 2718* Embedded Systems:: 2719* Realtime:: 2720* A database:: 2721* Visualisation:: 2722 2723 2724File: nana.info, Node: Simplest, Next: Syslog, Prev: Usage, Up: Usage 2725 27268.1 Simplest example 2727==================== 2728 2729As a nice simple, indeed trivial example consider the following: 2730 2731 1. Include 'nana.h' in your project wide include file. 2732 2. Use 'I' to check invariants in your code. In particular all 2733 functions or methods should be required to check their pre and post 2734 conditions. 2735 3. Use 'DL' to print debugging messages in your code. This means that 2736 debugging messages only occur when you run the program under the 2737 debugger.(1) 2738 2739 ---------- Footnotes ---------- 2740 2741 (1) Of course you also need to use the gdb commands generated by the 2742'nana' command, perhaps using 'nana-clg'. 2743 2744 2745File: nana.info, Node: Syslog, Next: GNU, Prev: Simplest, Up: Usage 2746 27478.2 Syslog 2748========== 2749 2750'syslog' is a comprehensive logging system written by Eric Allman which 2751is available on most UNIX systems. It provides facilities for sorting 2752messages by source or severity and mechanisms for sending messages off 2753to files, terminals or other machines. See chapter 12 of the "UNIX 2754System Administration Handbook (2nd Edition)" by Nemeth, Snyder, Seebass 2755and Hein for an excellent tutorial on 'syslog'. 2756 2757 The rules used by 'syslog' are normally defined in 2758'/etc/syslog.conf'. Each line specifies the destination for messages 2759with particular sources and priorities. For example: 2760 2761 # log mail messages at priority info to mailinfo 2762 mail.info /var/log/mailinfo 2763 # log all ftp information to syslog on a remote machine 2764 ftp.* @remote-machine.cs.ntu.edu.au 2765 # all critical errors to console 2766 *.crit /dev/console 2767 2768 To use 'syslog' we merely redefine the default handlers and parameter 2769values for 'nana'. For example: 2770 2771 #include <syslog.h> 2772 #define L_DEFAULT_HANDLER syslog /* replaces fprintf(3) by syslog(3) */ 2773 #define L_DEFAULT_PARAMS LOG_USER /* default priority for syslog info */ 2774 #include <nana.h> 2775 2776 int main() { 2777 openlog("nana", /* identifier for these log messages */ 2778 LOG_PID, /* also log the process id */ 2779 LOG_DAEMON /* facility */ 2780 ); 2781 2782 L("temperature is falling to %d", 35); /* logged at LOG_USER priority */ 2783 LP(LOG_CRIT, "the snow is falling in Darwin"); /* LOG_CRIT message */ 2784 2785 closelog(); 2786 return 0; 2787 } 2788 2789 This program results in the following addition to 2790'/var/adm/messages': 2791 2792 Jun 12 16:04:46 chingiz nana[2671]: the temperature is falling to 35 2793 Jun 12 16:04:46 chingiz nana[2671]: the snow is falling in Darwin 2794 2795 In addition the 'LOG_CRIT' message about snow falling in Darwin is 2796sent to the console for immediate action. 2797 2798 *Note:* 'syslog' normally uses 'UDP' for its network communications 2799and that 'UDP' does not guarantee delivery. 2800 2801 2802File: nana.info, Node: GNU, Next: Embedded Systems, Prev: Syslog, Up: Usage 2803 28048.3 GNU Programs: how to avoid nana sometimes 2805============================================= 2806 2807Imagine you(1) are building a GNU program. Ideally it should run on 2808systems without any other software including GCC, GDB and even Nana. 2809 2810 To achieve this noble goal you can provide your configure script with 2811a '--without-nana'(2) flag which then '#define's' 'WITHOUT_NANA'. You 2812should also use the 'VL' macro rather than 'L' macro since 'L' takes a 2813variable number of arguments and will break non GNU C preprocessors. 2814 2815 int half(int n) { 2816 /* Don't use L(...) since it takes a variable number of args! */ 2817 VL(("hello world = %d\n", 10)); /* Note the doubled (( */ 2818 ...; 2819 } 2820 2821 ---------- Footnotes ---------- 2822 2823 (1) Gordon Matzigkeit contributed some of the ideas presented here 2824and raised this problem. 2825 2826 (2) Or add a '--with-nana' flag to configure that does the opposite. 2827 2828 2829File: nana.info, Node: Embedded Systems, Next: Realtime, Prev: GNU, Up: Usage 2830 28318.4 Embedded Systems: testing non-deterministic systems. 2832======================================================== 2833 2834One of the uses (in fact the original use) for nana is in the testing of 2835non-deterministic systems. These systems behave in a different way each 2836time they are run because of timing or measurement noise. Thus you 2837can't just 'diff' the results against a known good run since they run 2838differently each time. 2839 2840 Instead you can use a series of programs which execute over a the 2841results of the program and check that the behaviour meets the 2842specification automatically. 2843 2844 2845File: nana.info, Node: Realtime, Next: A database, Prev: Embedded Systems, Up: Usage 2846 28478.5 Realtime Systems 2848==================== 2849 2850Assertions for the timing behaviour of you system can be done in nana 2851nicely. You might also consider using an instruction level simulator 2852such as PSIM and use the 'cycles' variable to test realtime constraints. 2853 2854 2855File: nana.info, Node: A database, Next: Visualisation, Prev: Realtime, Up: Usage 2856 28578.6 A database 2858============== 2859 2860Ah databases, business boffins using assertions. It would nice to get a 2861real example for this one! 2862 2863 2864File: nana.info, Node: Visualisation, Prev: A database, Up: Usage 2865 28668.7 Program Visualisation: pretty pictures 2867========================================== 2868 2869One nice way to use 'L', 'DL', etc is to punt off the messages off to a 2870program which displays the information. You would use the 2871'L_DEFAULT_PARAM' argument to send commands off to a pipe with gnuplot 2872or TCL/TK interpreter running at the end. 2873 2874 For a small example of this, see 'tcl/status.c'. 2875 2876 2877File: nana.info, Node: FAQ, Next: Future, Prev: Usage, Up: Top 2878 28799 FAQ 2880***** 2881 2882This chapter is intended to answer some general questions about the 2883usage of Nana. Most of the material (perhaps) has been presented 2884elsewhere, my apologies for repeating myself. 2885 2886 1. Can I use GNU Nana in a commercial product? 2887 2888 See the license details in the file 'COPYING'. In summary GNU Nana 2889 is Free software which has been released under a license that 2890 allows commercial use provided the copyright is maintained. It is 2891 not under the GPL. 2892 2893 2. How do you completely disable assertion checking? 2894 2895 Set the 'I_LEVEL' command to '0' using '-D' or '#define' before 2896 including 'nana.h'. 2897 2898 3. How do I find out about future releases? 2899 2900 Subscribe to the nana mailing list by using the 'make subscribe' 2901 target in the nana root directory. 2902 2903 4. Where do I send bug reports, suggestions, etc? 2904 2905 'nana-bug@it.ntu.edu.au' - bug archive 2906 2907 'nana@it.ntu.edu.au' - mailing list 2908 2909 'pjm@gnu.org' - the author 2910 2911 2912File: nana.info, Node: Future, Next: Index, Prev: FAQ, Up: Top 2913 291410 Future work 2915************** 2916 2917As usual, there is more work to be done than time to do it in. Workers 2918or test pilots are invited to apply for the following tasks which may 2919get down in the fullness of time minister. I'm particularly interested 2920in which projects people think are worthwhile (or rubbish). 2921 2922 1. Ada support - pre 1.00 versions of nana provided support for Ada. 2923 Adding a full version of nana into the GNATS compiler would 2924 probably be useful, particularly the real time part. 2925 2. FORTRAN support. 2926 3. Message browsing ('emacs/fess.el') - a very prototypical mode for 2927 browsing message logs is distributed with nana. Features include 2928 hiding/showing lines by regular expression or predicate. If 2929 someone who knows what they are doing rewrites this that would be a 2930 good thing. Or perhaps a modified version of 'less' would be 2931 useful. 2932 4. Program Visualisation ('tcl/status.c') - another prototype which 2933 uses a small TCL/TK library and nana logging to generate some nice 2934 pictures showing the behaviour of the program. For example the 2935 history of variables over time can be recorded, graphs drawn and 2936 long message logs kept. A new version of this has been created and 2937 may be released shortly. 2938 5. Automated Testing - combine nana logs with a workbench that lets 2939 you verify properties of the logs using programs 'gawk' or the 2940 'PRECC' (LL infinity version of YACC). This technique has been used 2941 on industrial strength products quite successfully. 2942 6. GNU standards - perhaps it would be worthwhile to set up a 2943 prototype for checking and logging in GNU tools. 2944 7. Extend GDB(1) so that we can support 'Q.h' style quantifiers in 2945 'DI.h' expressions. Basically we need to add a restricted form of 2946 the statement value expression to GDB. 2947 8. Support for other (particularly ANSI only) compilers. 2948 2949 2950File: nana.info, Node: Index, Prev: Future, Up: Top 2951 2952Appendix A Index 2953**************** 2954 2955[index] 2956* Menu: 2957 2958* 0: I.h. (line 39) 2959* 0 <1>: DI.h. (line 36) 2960* 0 <2>: L.h. (line 57) 2961* 0 <3>: DL.h. (line 59) 2962* 1: I.h. (line 42) 2963* 1 <1>: DI.h. (line 39) 2964* 1 <2>: L.h. (line 55) 2965* 1 <3>: DL.h. (line 57) 2966* 2: I.h. (line 46) 2967* 2 <1>: DI.h. (line 43) 2968* 2 <2>: L.h. (line 53) 2969* 2 <3>: DL.h. (line 55) 2970* A: Introduction. (line 101) 2971* A <1>: Q.h. (line 59) 2972* abort(): Variables. (line 41) 2973* ALWAYS_INCLUDE_MALLOC: Variables. (line 47) 2974* AO: Qstl.h. (line 24) 2975* asm("hlt"): Variables. (line 37) 2976* asm("unimp"): Variables. (line 39) 2977* C: Introduction. (line 109) 2978* C <1>: Q.h. (line 71) 2979* calls_add: calls.h. (line 36) 2980* calls_delete: calls.h. (line 56) 2981* calls_exec: calls.h. (line 44) 2982* CHECK: REQUIRE.... (line 35) 2983* CHECK_ALL: EIFFEL_CHECK. (line 22) 2984* CHECK_ENSURE: EIFFEL_CHECK. (line 13) 2985* CHECK_INVARIANT: EIFFEL_CHECK. (line 15) 2986* CHECK_LOOP: EIFFEL_CHECK. (line 20) 2987* CHECK_NO: EIFFEL_CHECK. (line 9) 2988* CHECK_REQUIRE: EIFFEL_CHECK. (line 11) 2989* CO: Qstl.h. (line 52) 2990* CYCLES: cycles.h. (line 16) 2991* cycles: cycles.h. (line 20) 2992* cycles_diff: cycles.h. (line 35) 2993* cycles_per_second: cycles.h. (line 23) 2994* cycles_per_second_max: cycles.h. (line 31) 2995* cycles_per_second_min: cycles.h. (line 30) 2996* DI: DI.h. (line 13) 2997* DI <1>: DI.h. (line 94) 2998* DI.h: nana.h. (line 10) 2999* DIG: DI.h. (line 95) 3000* DIGH: DI.h. (line 98) 3001* DIGHP: DI.h. (line 101) 3002* DIGP: DI.h. (line 99) 3003* DIH: DI.h. (line 96) 3004* DIHP: DI.h. (line 100) 3005* DIP: DI.h. (line 97) 3006* DI_DEFAULT_GUARD: DI.h. (line 48) 3007* DI_DEFAULT_HANDLER: DI.h. (line 70) 3008* DI_DEFAULT_PARAMS: DI.h. (line 62) 3009* DI_LEVEL: DI.h. (line 32) 3010* DI_MAKE_VALID_BREAKPOINT: Variables. (line 12) 3011* DI_MAKE_VALID_BREAKPOINT <1>: DI.h. (line 89) 3012* DL: DL.h. (line 12) 3013* DL.h: nana.h. (line 12) 3014* DLG: DL.h. (line 17) 3015* DLGHP: DL.h. (line 22) 3016* DLGP: DL.h. (line 20) 3017* DLH: DL.h. (line 18) 3018* DLHP: DL.h. (line 21) 3019* DLP: DL.h. (line 19) 3020* DL_DEFAULT_GUARD: DL.h. (line 70) 3021* DL_DEFAULT_HANDLER: DL.h. (line 64) 3022* DL_DEFAULT_PARAMS: DL.h. (line 74) 3023* DL_LEVEL: DL.h. (line 51) 3024* DL_MAKE_VALID_BREAKPOINT: Variables. (line 32) 3025* DL_SHOW_TIME: DL.h. (line 77) 3026* DN: DI.h. (line 26) 3027* DN <1>: DI.h. (line 102) 3028* DNG: DI.h. (line 103) 3029* DNGH: DI.h. (line 106) 3030* DNGHP: DI.h. (line 109) 3031* DNGP: DI.h. (line 107) 3032* DNH: DI.h. (line 104) 3033* DNHP: DI.h. (line 108) 3034* DNP: DI.h. (line 105) 3035* DS: DI.h. (line 116) 3036* DSG: DI.h. (line 117) 3037* E: Introduction. (line 103) 3038* E <1>: Q.h. (line 64) 3039* E1: Introduction. (line 106) 3040* E1 <1>: Q.h. (line 78) 3041* E1O: Qstl.h. (line 43) 3042* ENSURE: REQUIRE.... (line 20) 3043* EO: Qstl.h. (line 34) 3044* exprn: I.h. (line 79) 3045* exprn <1>: DI.h. (line 76) 3046* EXPRN: Q.h. (line 85) 3047* EXPRN <1>: Q.h. (line 96) 3048* EXPRN <2>: Qstl.h. (line 61) 3049* EXPRN <3>: Qstl.h. (line 69) 3050* file: I.h. (line 82) 3051* file <1>: DI.h. (line 79) 3052* gcc-2.7.2: Required Software. (line 8) 3053* GDB: Variables. (line 54) 3054* GDB <1>: GDB.h. (line 13) 3055* gdb-4.16+: Required Software. (line 15) 3056* GDB.h: nana.h. (line 14) 3057* GDBCALL: GDB.h. (line 29) 3058* gmake: Required Software. (line 20) 3059* I: I.h. (line 10) 3060* I <1>: I.h. (line 98) 3061* I.h: nana.h. (line 9) 3062* ID: I.h. (line 119) 3063* IG: I.h. (line 99) 3064* IGH: I.h. (line 102) 3065* IGHP: I.h. (line 105) 3066* IGP: I.h. (line 103) 3067* IH: I.h. (line 100) 3068* IHP: I.h. (line 104) 3069* INVARIANT: REQUIRE.... (line 32) 3070* IP: I.h. (line 101) 3071* IS: I.h. (line 120) 3072* ISG: I.h. (line 121) 3073* I_DEFAULT_GUARD: I.h. (line 51) 3074* I_DEFAULT_HANDLER: Variables. (line 35) 3075* I_DEFAULT_HANDLER <1>: I.h. (line 73) 3076* I_DEFAULT_PARAMS: I.h. (line 65) 3077* I_LEVEL: I.h. (line 35) 3078* L: L.h. (line 9) 3079* L.h: nana.h. (line 11) 3080* LG: L.h. (line 14) 3081* LGHP: L.h. (line 19) 3082* LGP: L.h. (line 17) 3083* LH: L.h. (line 15) 3084* LHP: L.h. (line 18) 3085* line: I.h. (line 84) 3086* line <1>: DI.h. (line 81) 3087* LP: L.h. (line 16) 3088* L_BUFFER: L_buffer.h. (line 16) 3089* L_buffer_clear: L_buffer.h. (line 53) 3090* L_buffer_create: L_buffer.h. (line 20) 3091* L_buffer_delete: L_buffer.h. (line 21) 3092* L_buffer_dump: L_buffer.h. (line 56) 3093* L_buffer_printf: L_buffer.h. (line 41) 3094* L_buffer_putchar: L_buffer.h. (line 43) 3095* L_buffer_puts: L_buffer.h. (line 42) 3096* L_buffer_wraparound: L_buffer.h. (line 31) 3097* L_DEFAULT_GUARD: L.h. (line 68) 3098* L_DEFAULT_HANDLER: L.h. (line 62) 3099* L_DEFAULT_PARAMS: L.h. (line 73) 3100* L_LEVEL: L.h. (line 49) 3101* L_SHOW_TIME: L.h. (line 78) 3102* L_SHOW_TIME_FORMAT: L.h. (line 81) 3103* L_SHOW_TIME_NOW: L.h. (line 85) 3104* L_TIMES: L_times.h. (line 18) 3105* L_times_add: L_times.h. (line 33) 3106* L_times_clear: L_times.h. (line 40) 3107* L_times_create: L_times.h. (line 22) 3108* L_times_delete: L_times.h. (line 23) 3109* L_times_dump: L_times.h. (line 37) 3110* L_times_wraparound: L_times.h. (line 27) 3111* N: I.h. (line 24) 3112* N <1>: I.h. (line 106) 3113* nana-clg: Invoking. (line 77) 3114* nana-run: Invoking. (line 70) 3115* nana-trace: Invoking. (line 92) 3116* NG: I.h. (line 107) 3117* NGH: I.h. (line 110) 3118* NGHP: I.h. (line 113) 3119* NGP: I.h. (line 111) 3120* NH: I.h. (line 108) 3121* NHP: I.h. (line 112) 3122* now: now.h. (line 15) 3123* now_delta: now.h. (line 23) 3124* now_reset: now.h. (line 20) 3125* NP: I.h. (line 109) 3126* P: Introduction. (line 113) 3127* param: I.h. (line 86) 3128* param <1>: DI.h. (line 83) 3129* Q.h: nana.h. (line 13) 3130* REQUIRE: REQUIRE.... (line 8) 3131* restart(): Variables. (line 45) 3132* S: Introduction. (line 111) 3133* struct: calls.h. (line 30) 3134* typedef: calls.h. (line 19) 3135* VDL: DL.h. (line 30) 3136* VDLG: DL.h. (line 31) 3137* VDLGHP: DL.h. (line 36) 3138* VDLGP: DL.h. (line 34) 3139* VDLH: DL.h. (line 32) 3140* VDLHP: DL.h. (line 35) 3141* VDLP: DL.h. (line 33) 3142* VL: L.h. (line 27) 3143* VLG: L.h. (line 28) 3144* VLGHP: L.h. (line 33) 3145* VLGP: L.h. (line 31) 3146* VLH: L.h. (line 29) 3147* VLHP: L.h. (line 32) 3148* VLP: L.h. (line 30) 3149 3150 3151 3152Tag Table: 3153Node: Top512 3154Node: Introduction2856 3155Ref: Introduction-Footnote-19678 3156Ref: Introduction-Footnote-29751 3157Ref: Introduction-Footnote-39941 3158Node: Related work10073 3159Node: Assert14655 3160Node: Scope16443 3161Node: Installation16770 3162Node: Required Software19223 3163Node: Optional Software20538 3164Node: Configure21020 3165Node: Variables24325 3166Node: Supported Platforms26895 3167Node: Supported Debuggers28090 3168Ref: Supported Debuggers-Footnote-129870 3169Node: Known Problems29948 3170Ref: Known Problems-Footnote-134025 3171Node: Bug Reports34102 3172Node: New Versions35152 3173Node: Invoking35786 3174Node: Interface39539 3175Node: nana.h40847 3176Node: WITHOUT_NANA41164 3177Node: I.h42139 3178Ref: I.h-Footnote-147060 3179Ref: I.h-Footnote-247183 3180Node: DI.h47273 3181Ref: DI.h-Footnote-152499 3182Node: L.h52573 3183Node: L_buffer.h55573 3184Ref: L_buffer.h-Footnote-160167 3185Node: L_times.h60211 3186Node: DL.h61889 3187Node: GDB.h64806 3188Node: Q.h66425 3189Ref: Q.h-Footnote-170622 3190Ref: Q.h-Footnote-270786 3191Node: Qstl.h70930 3192Node: now.h73719 3193Ref: now.h-Footnote-174851 3194Node: cycles.h74925 3195Ref: cycles.h-Footnote-177326 3196Node: RDTSC77401 3197Node: eiffel.h78075 3198Node: EIFFEL_CHECK78558 3199Node: DOEND79598 3200Node: REQUIRE...80871 3201Node: assert.h82816 3202Node: calls.h83437 3203Node: Shortform86442 3204Ref: Shortform-Footnote-190144 3205Ref: Shortform-Footnote-290301 3206Node: Performance90451 3207Ref: Performance-Footnote-192055 3208Node: Tracing92418 3209Node: Statement92886 3210Node: Library94779 3211Ref: Library-Footnote-198976 3212Node: Usage99277 3213Node: Simplest99849 3214Ref: Simplest-Footnote-1100446 3215Node: Syslog100561 3216Node: GNU102661 3217Ref: GNU-Footnote-1103477 3218Ref: GNU-Footnote-2103573 3219Node: Embedded Systems103646 3220Node: Realtime104315 3221Node: A database104664 3222Node: Visualisation104885 3223Node: FAQ105342 3224Node: Future106375 3225Node: Index108343 3226 3227End Tag Table 3228