home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0912.lha / Enforcer / Enforcer.doc < prev    next >
Text File  |  1993-10-03  |  52KB  |  1,221 lines

  1. TABLE OF CONTENTS
  2.  
  3. Enforcer
  4. FindHit
  5. LawBreaker
  6. Move4K
  7. SegTracker
  8. Enforcer                                                             Enforcer
  9.  
  10.    NAME
  11.     Enforcer V37 - An advanced version of Enforcer - Requires V37
  12.  
  13.    SYNOPSIS
  14.     Enforcer - A tool to watch for illegal memory accesses
  15.  
  16.    FUNCTION
  17.     Enforcer will use the MMU in the advanced 680x0 processors
  18.     to set up MMU tables to watch for illegal accesses to memory
  19.     such as the low-page and non-existent pages.
  20.  
  21.     To use, run Enforcer (plus any options you may wish)
  22.     If you wish to detach, just use RUN >NIL: <NIL: to start it.
  23.     You can also start it from the Workbench.  When started from Workbench,
  24.     Enforcer will read the tooltypes of its icon or selected project icon
  25.     for its options.  (See the sample project icons)
  26.  
  27.     Enforcer should only be run *after* SetPatch.
  28.  
  29.     If SegTracker is running in the system when Enforcer is started,
  30.     Enforcer will use the public SegTracker seglist tracking for
  31.     identifying the hits.
  32.  
  33.    INPUTS
  34.     The options for Enforcer are as follows:
  35.  
  36.     QUIET/S        - This tells Enforcer to not complain about any invalid
  37.                      access and to just build MMU tables for cache setting
  38.                      reasons -- mainly used in conjunction with an
  39.                      Amiga BridgeBoard in a 68030 environment so that
  40.                      the system can run with the data cache turned on.
  41.                      In this case,
  42.                                     RUN >NIL: Enforcer QUIET
  43.                      should be placed into the startup-sequence right
  44.                      after SetPatch.
  45.  
  46.     TINY/S         - This tells Enforcer to output a minimal hit.  The
  47.                      output is basically the first line of the Enforcer
  48.                      hit.  (see below)
  49.  
  50.     SMALL/S        - This tells Enforcer to output the hit line, the
  51.                      USP: line, and the Name: line.  (This means that
  52.                      no register or stack display will be output)
  53.  
  54.     SHOWPC/S       - This tells Enforcer to also output the two lines
  55.                      that contain the memory area around the PC where
  56.                      the hit happened.  Useful for disassembly.
  57.                      This option will not do anything if QUIET, SMALL or
  58.                      TINY output modes are selected.
  59.  
  60.     STACKLINES/K/N - This lets you pick the number of lines of stack
  61.                      backtrace to display.  The default is 2.  If set
  62.                      to 0, no stack backtrace will be displayed.  There
  63.                      is NO ENFORCED LIMIT on the number of lines.
  64.  
  65.     STACKCHECK/S   - This option tells Enforcer that you wish all of
  66.                      the long words displayed in the stack to be checked
  67.                      against the global seglists via SegTracker.
  68.                      This will tell you what seglist various return
  69.                      addresses are on the stack.  If you are not
  70.                      displaying stack information in the Enforcer hit
  71.                      then STACKCHECK will have nothing to check.
  72.                      If you are displaying stack information, then
  73.                      each long word will be check and only those which
  74.                      are in one of the tracked seglists will be
  75.                      displayed in a SegTracker line.
  76.                      The output will show the PC address first and
  77.                      then work its way back on the stack such that you
  78.                      can read it from bottom up as the order of calling
  79.                      or from top down as the stack-frame backtrace.
  80.  
  81.     AREGCHECK/S    - This option tells Enforcer that you wish all of
  82.                      the values in the Address Registers checked via
  83.                      SegTracker, much like STACKCHECK.
  84.  
  85.     DREGCHECK/S    - This option tells Enforcer that you wish all of
  86.                      the values in the Data Registers checked via
  87.                      SegTracker, much like STACKCHECK.
  88.  
  89.     DATESTAMP/S    - This makes Enforcer output a date and time with each
  90.                      hit.  Due to the nature of the way Enforcer must
  91.                      work, the time can not be read during the Enforcer
  92.                      hit itself so the time output will be the last time
  93.                      value the main Enforcer task set up.  Enforcer will
  94.                      update this value every second as to try to not
  95.                      use any real CPU time.  The time displayed in the
  96.                      hit will thus be exact.
  97.                      (Assuming the system clock is correct.)
  98.                      The date is output before anything from the hit
  99.                      other than the optional introduction string.
  100.  
  101.     DEADLY/S       - This makes Enforcer be a bit more nasty.  Normally,
  102.                      when an illegal read happens, Enforcer returns 0
  103.                      as the result of this read.  With this option,
  104.                      Enforcer will return $ABADFEED as the read data.
  105.                      This option can make programs with Enforcer hits
  106.                      cause even more hits.
  107.  
  108.     FSPACE/S       - This option will make the special $00F00000 address
  109.                      space available for writing to.  This is useful for
  110.                      those people with $00F00000 boards.  Mainly Commodore
  111.                      internal development work -- should only be used
  112.                      in that enviroment.
  113.  
  114.     NORELOADKICK/S - In A3000 SuperKickstart machines, Enforcer would
  115.                      force the Kickstart to be reloaded on a reboot.
  116.                      This option will disable this feature of Enforcer.
  117.                      Note that on some systems, reboots may not be
  118.                      reliable in this case.  Also note that if software
  119.                      tries to reboot the machine and it does not use the
  120.                      2.x ColdReboot() function, the system will not
  121.                      correctly reboot.  (It may, in fact, hang)
  122.  
  123.     VERBOSE/S      - This option will make Enforcer display information
  124.                      as to the mapping of the I/O boards and other
  125.                      technical information.  This information maybe useful
  126.                      in specialized debugging.
  127.  
  128.     LED/K/N        - This option lets you specify the speed at which
  129.                      the LED will be toggled for each Enforcer hit.
  130.                      The default is 1 (which is like it always was)
  131.                      Setting it to 0 will make Enforcer not touch
  132.                      the LED.  Using a larger value will make the
  133.                      flash take longer (such that it can be noticed
  134.                      when doing I/O models other than the default
  135.                      serial output)  The time that the flash will
  136.                      take is a bit more than 1.3 microseconds times
  137.                      the number.  So 1000 will be a bit more than
  138.                      1.3 milliseconds.  (Or 1000000 is a bit more than
  139.                      1.3 seconds.)
  140.  
  141.     PARALLEL/S     - This option will make Enforcer use the parallel port
  142.                      hardware rather than the serial port for output.
  143.  
  144.     RAWIO/S        - This option will make Enforcer stuff the hit report
  145.     (special IO)     into an internal buffer and then from the main
  146.                      Enforcer process output the results via the
  147.                      RawPutChar() EXEC debugging LVO.  Since the output
  148.                      happens on the Enforcer task it is possible for a
  149.                      hit that ends in a system crash to not be able to
  150.                      be reported.  This option is here such that tools
  151.                      which can redirect debugging output can redirect
  152.                      the Enforcer output too.
  153.  
  154.     FILE/K         - This option will make Enforcer output the hit report
  155.     (special IO)     but to a file insted of sending it to the hardware
  156.                      directly or using the RAWIO LVO.  A good example of
  157.                      such a file is CON:0/0/640/100/HIT/AUTO/WAIT.
  158.                      Another thing that can be done is to have a program
  159.                      sit on a named pipe and have Enforcer output to it.
  160.                      This program can then do whatever it feels like with
  161.                      the Enforcer hits.  (Such as decode them, etc.)
  162.                      *NOTE*  It is not a good idea to have Enforcer hits
  163.                      go to a file on a disk as if the system crashes
  164.                      during/after the Enforcer hit, the disk may
  165.                      become corrupt.
  166.  
  167.     STDIO/S        - This option will make Enforcer output the hit report
  168.     (special IO)     to STDOUT.  This option only works from the CLI as it
  169.                      requires STDOUT.  It is best used with redirection or
  170.                      pipes.
  171.  
  172.     BUFFERSIZE/K/N - This lets you set Enforcer's internal output buffer
  173.                      for the special I/O options.  This option is only
  174.                      valid with the RAWIO, FILE, or STDIO options.
  175.                      The minimum setting is 8000.  The default is 8000.
  176.                      Having the right amount of buffer is rather
  177.                      important for the special I/O modes.  The reason
  178.                      is due to the fact that no operating system calls
  179.                      can be made from a bus error.  Thus, in the
  180.                      special I/O mode, Enforcer must store the output
  181.                      in this buffer and, via some special magic,
  182.                      wake up the Enforcer task to read the buffer and
  183.                      write it out as needed.  However, if a task is
  184.                      in Forbid() or Disable() when the Enforcer hit
  185.                      happens, the Enforcer task will not be able to
  186.                      output the results of the hit.  This buffer lets
  187.                      a number of hits happen even if the Enforcer task
  188.                      was unable to do the I/O.  If the number of
  189.                      hits that happen before the I/O was able to
  190.                      run gets too large, the last few hits will either
  191.                      be cut off completely or contain only partial
  192.                      information.
  193.  
  194.     INTRO/K        - This optional introduction string will be output
  195.                      at the start of every Enforcer hit.  For example:
  196.                      INTRO="*NBad Program!"   The default is no string.
  197.  
  198.     PRIORITY/K/N   - This lets you set Enforcer's I/O task priority.
  199.                      The default for this priority is 99.  In some
  200.                      special cases, you may wish to adjust this.
  201.                      It is, however, recommended that if you are using
  202.                      one of the special I/O options (RAWIO, FILE, or
  203.                      STDIO) that you keep the priority rather high.
  204.                      If the priority you supply is outside of the
  205.                      valid task priority range (-127 to 127) Enforcer
  206.                      will use the default priority.
  207.  
  208.     NOALERTPATCH/S - This option disables the patching of the EXEC
  209.                      Alert() function.  Normally Enforcer will patch
  210.                      this function to provide information as to what
  211.                      called Alert() and to prevent the Enforcer hits
  212.                      that a call to Alert() would cause.
  213.  
  214.     ON/S           - Mainly for completeness.  If not specified, it
  215.                      is assumed you want to turn ON Enforcer.
  216.  
  217.     QUIT=OFF/S     - Tells Enforcer to turn off.  Enforcer can also be
  218.                      stopped by sending a CTRL-C to its process.
  219.  
  220.    RESULTS
  221.     When running, a set of MMU tables that map addresses that are not
  222.     in the system's address map as invalid such that any access to them
  223.     will cause an access fault.  Enforcer will then display this fact
  224.     and generate a diagnostic message as to what the illegal access
  225.     was.  The first memory page (the one starting at location 0) is
  226.     also marked as invalid as many programming errors cause invalid
  227.     access to these addresses.  These addresses are completely off
  228.     limits to applications.
  229.  
  230.     When an    access violation happens, a report such as the following
  231.     is output.
  232.  
  233. 03-Apr-93  21:26:18
  234. WORD-WRITE to  00000000        data=4444       PC: 07895CA4
  235. USP:  078D692C SR: 0000 SW: 0729  (U0)(-)(-)  TCB: 078A2690
  236. Data: DDDD0000 DDDD1111 DDDD2222 DDDD3333 DDDD4444 DDDD5555 DDDD6666 DDDD7777
  237. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 AAAA5555 07800804 --------
  238. Stck: 00000000 07848E1C 00009C40 078A30B4 BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB
  239. Stck: BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB 078E9048 00011DA8 DEADBEEF
  240. ----> 07895CA4 - "lawbreaker"  Hunk 0000 Offset 0000007C
  241. PC-8: AAAA1111 247CAAAA 2222267C AAAA3333 287CAAAA 44442A7C AAAA5555 31C40000
  242. PC *: 522E0127 201433FC 400000DF F09A522E 012611C7 00CE4EAE FF7642B8 0324532E
  243. Name: "New_Shell"  CLI: "lawbreaker"  Hunk 0000 Offset 0000007C
  244.  
  245. LONG-READ from AAAA4444                        PC: 07895CA8
  246. USP:  078D692C SR: 0015 SW: 0749  (U0)(F)(-)  TCB: 078A2690
  247. Data: DDDD0000 DDDD1111 DDDD2222 DDDD3333 DDDD4444 DDDD5555 DDDD6666 DDDD7777
  248. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 AAAA5555 07800804 --------
  249. Stck: 00000000 07848E1C 00009C40 078A30B4 BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB
  250. Stck: BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB 078E9048 00011DA8 DEADBEEF
  251. ----> 07895CA8 - "lawbreaker"  Hunk 0000 Offset 00000080
  252. PC-8: 247CAAAA 2222267C AAAA3333 287CAAAA 44442A7C AAAA5555 31C40000 522E0127
  253. PC *: 201433FC 400000DF F09A522E 012611C7 00CE4EAE FF7642B8 0324532E 01266C08
  254. Name: "New_Shell"  CLI: "lawbreaker"  Hunk 0000 Offset 00000080
  255.  
  256. 25-Jul-93  17:15:06
  257. Alert !! Alert 35000000     TCB: 07642F70     USP: 07657C10
  258. Data: 00000000 DDDD1111 DDDD2222 DDDD3333 0763852A DDDD5555 DDDD6666 35000000
  259. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 0763852A 07400810 --------
  260. Stck: 076385A0 00000000 0752EE9A 00002800 07643994 00000000 0762F710 076305F0
  261. ----> 076385A0 - "lawbreaker"  Hunk 0000 Offset 00000098
  262.  
  263.     Here is a breakdown of what these reports are saying:
  264.  
  265.     In the first report, the first line is the date stamp.
  266.  
  267.     The first line of each report describes the access violation
  268.     and where it happened from.  In the case of a WRITE, the data
  269.     that was being written will be displayed as well.  If an instruction
  270.     mode access caused the fault, there will be an (INST) in the line.
  271.  
  272.     The first line may also contain the BUS ERROR message.  This will
  273.     be displayed when an address that is valid in the system lists
  274.     causes a physical bus fault during the access.  This usually
  275.     will happen with plug-in cards or when a hardware problem causes
  276.     some form of system fault.  Watch out, if this does show up, your
  277.     system may be unstable and/or unreliable.
  278.  
  279.     The second line (starts USP:) displays the USER stack pointer (USP),
  280.     the status register (SR:), the special status word (SW:).  It then
  281.     displays the supervisor/user state and the interrupt level.  This
  282.     will be from (U0) to (U7) or (S0) to (S7)  (S=Supervisor)  Next
  283.     is the forbid state (F=forbid, -=not) and the disable state (D or -)
  284.     of the task that was running when the access fault took place.
  285.     Finally, the task control block address is displayed (TCB:)
  286.  
  287.     The next two lines contain the data and address register dumps from
  288.     when the access fault happened.  Note that A7 is not listed here.
  289.     It is the stack pointer and is listed as USP: in the line above.
  290.  
  291.     Then come the lines of stack backtrace.  These lines show the
  292.     data on the stack.  If the stack is in invalid memory, Enforcer will
  293.     display a message to that fact.
  294.  
  295.     If SegTracker was installed before Enforcer, the "---->" lines
  296.     will display in which seglist the given addresses are in based on the
  297.     global tracking that SegTracker does.  (See docs on SegTracker)
  298.     If no seglist match is found, no lines will be displayed.
  299.     One line will be displayed for each of the stack longwords asked
  300.     for (see the STACKCHECK option) and one line for the PC address of
  301.     the Enforcer hit.  (The PC line is always checked for is SegTracker
  302.     is installed.)  The lines are in order: hit, first stack find,
  303.     second stack find, etc.  This is useful for tracking down who
  304.     called the routine that caused the Enforcer hit.
  305.  
  306.     Next, optionally, comes the data around the program counter when the
  307.     access fault happened.  The first line (PC-8:) is the 8 long-words
  308.     before the program counter.  The second line starts at the program
  309.     counter and goes for 8 long words.
  310.  
  311.     The last line displays the name of the task that was running when
  312.     the access fault took place.  If the task was a CLI, it will display
  313.     the name of the CLI command that was running.  If the access fault
  314.     was found to have happened within the seglist of a loaded program,
  315.     the segment number and the offset from the start of the segment will
  316.     be displayed.  (Note that this works for any LoadSeg()'ed process)
  317.  
  318.     Note that the name will display as "Processor Interrupt Level x"
  319.     if the access happened in an interrupt.
  320.  
  321.     The other output that could happen is when a program or the OS
  322.     calls the EXEC Alert function.  Enforcer catches these calls
  323.     and will display the alert information as seen above.  (With the
  324.     data and time if needed)
  325.  
  326.    WARNING
  327.     Enforcer is for software testing.  In this role it is vital.
  328.     Software that causes Enforcer hits may well not be able to run on
  329.     newer hardware.  (Enforcer hits of high addresses on systems not
  330.     running Enforcer but with a 68040 will most likely crash the system)
  331.     Future systems and hardware will make this even more important.  The
  332.     system can NOT survive software that causes Enforcer hits.
  333.  
  334.     However, Enforcer is NOT a system protector.  As a side effect, it
  335.     may well keep a system from crashing when Enforcer hits happen, but
  336.     it may just as well make the software crash earlier.  Enforcer is
  337.     mainly a development and testing tool.
  338.  
  339.     Enforcer causes    no ill effects with correctly working software.
  340.     If a program fails to work while Enforcer is active, you should
  341.     contact the developer of that program.
  342.  
  343.    NOTES
  344.     This is Enforcer V37.  Bryce Nesbitt came up with the original
  345.     "Enforcer" that has been instrumental to the improvement in the
  346.     quality of software on the Amiga.  The Amiga users and developers
  347.     owe him a great deal for this.  Thank you Bryce!  Enforcer V37,
  348.     however, is a greatly enhanced and more advanced tool.
  349.  
  350.     Enforcer V37 came about due to a number of needs.  These included
  351.     the need for more output options and better performance.  It also
  352.     marks the removal of all kludges that were in the older versions.
  353.     Also, some future plans required some of these changes...
  354.  
  355.     In addition, the complete redesign was needed in order to also
  356.     support the 68040.  The internal design of Enforcer is now set up
  357.     such that CPU/MMU specific code can be cleanly accessed from the
  358.     general house keeping aspect of the code.  The MMU bus error
  359.     handling is, however, 100% CPU specific.
  360.  
  361.     Since AbsExecBase is in low memory, reads of this address are slower
  362.     with Enforcer running.  Caching AbsExecBase locally is highly
  363.     recommended since it is in CHIP memory and on systems with FAST
  364.     memory, it will be faster to access the local cached value. (In
  365.     addition to the performance increase when running Enforcer) Note
  366.     that doing many reads of location 4 will hurt interrupt performance.
  367.  
  368.     When the Amiga produces an ALERT, EXEC places some magic numbers
  369.     into some special locations in low memory.  The exact pattern
  370.     changes between versions of the operating system.
  371.  
  372.     Enforcer will patch the EXEC function ColdReboot() in an attempt to
  373.     "get out of the way" when someone tries to reboot the system.
  374.     Enforcer will clean up as much as possible the MMU tables and then
  375.     call the original LVO.  When Enforcer is asked to quit, it will
  376.     check to make sure it can remove itself from this LVO. If it can
  377.     not, it will not quit at that time.  If run from the shell, it will
  378.     display a message saying that it tried but could not exit.  Enforcer
  379.     will continue to be active and you can try later to deactivate it.
  380.  
  381.     Enforcer will also patch the EXEC function Alert() in an attempt to
  382.     provide better tracking of other events in the system.  It is also
  383.     patched such that dead-end alerts will correctly reset the system
  384.     and be displayed.  With this patch in place, the normal alerts will
  385.     not be seen but will be replaced by the Enforcer output shown
  386.     above.  See LawBreaker for a more complete example of this.
  387.  
  388.    68020 NOTES
  389.     The 68020 does not have a built-in MMU but has a co-processor
  390.     feature that lets an external MMU be connected.  Enforcer MMU code
  391.     is designed for use with 68851 MMU.  This is the some-what 68030
  392.     compatible MMU by Motorola.  Enforcer uses the same code for both
  393.     the 68030 and the 68020/68851.  For this reason, 68020/68851 users
  394.     should see the 68030 NOTES section.
  395.  
  396.    68030 NOTES
  397.     The 68030 uses cycle/instruction continuation and will
  398.     supply the data on reads and ignore writes during an access
  399.     fault rather than let the real bus cycle happen.  This means
  400.     that on a fault caused by MMU tables, no bus cycle to the
  401.     fault address will be generated.  (For those of you with analyzers)
  402.  
  403.     In some cases, the 68030 will have advanced the Program Counter
  404.     past the instruction by the time the access fault happens.
  405.     This is usually only on WRITE faults.  For this reason, the PC
  406.     may either point at the instruction that caused the fault or
  407.     just after the instruction that caused the fault.  (Which could
  408.     mean that it is pointing to the middle of the instruction
  409.     that caused the fault.)
  410.  
  411.     Note that there is a processor called 68EC030.  This processor
  412.     has a disabled or defective MMU.  However, it may function well
  413.     enough for Enforcer to think it has a fully functional MMU and
  414.     thus Enforcer will attempt to run.  However, even if it looks like
  415.     the MMU is functioning, it is not fully operational and thus may
  416.     cause strange system activity and even crashes.  Do not assume
  417.     that Enforcer is safe to use on 68EC030 systems.
  418.  
  419.    68040 NOTES
  420.     Enforcer, on the 68040, *requires* that the 68040.library be
  421.     installed and it requires an MMU 68040 CPU.  The 68EC040 does not
  422.     have a MMU.  The 68LC040 does have an MMU and is supported. Enforcer
  423.     will work best in a system with the 68040.library 37.10 or better
  424.     but it does know how to deal with systems that do not have that
  425.     version.
  426.  
  427.     Due to the design of the 68040, Enforcer is required to do a number
  428.     of things differently.  For example, the MMU page size can only be
  429.     either 8K or 4K.  This means that to protect the low 1K of memory,
  430.     Enforcer will end up having to mark the first 4K of memory as
  431.     invalid and emulate the access to the 3K of that memory that is
  432.     valid. For this reason Enforcer moves a number of possible
  433.     structures from the first 4K of memory to higher addresses.  This
  434.     means that the system will continue to run at a reasonable speed.
  435.     The first time Enforcer is run it may need to allocate memory for
  436.     these structures that it will move.  Enforcer can never return this
  437.     memory to the system.
  438.  
  439.     In addition to the fact that the 68040 MMU table size is different,
  440.     the address fault handling is also different.  Namely, the 68040 can
  441.     only rerun the cycle and not continue it like the 68030. This means
  442.     that on a 68040, the page must be made available first and then made
  443.     unavailable.  Make this work, Enforcer will switch the instruction
  444.     that caused the error into trace mode and let it run with a special
  445.     MMU setup.  When the trace exception comes in, the MMU is set up
  446.     back to the way it was.  Enforcer does its best to keep debuggers
  447.     working.  Note, however, that the interrupt level during a trace of
  448.     a READ will end up being set to 7.  This is to prevent interrupts
  449.     from changing the order of trace/MMU table execution.  The level
  450.     will be restored to the original state before continuing.  Since T0
  451.     mode tracing is also supported, there are also some changes in the
  452.     way it operates.  T0 mode tracing is defined, on the 68040, to cause
  453.     a trace whenever the instruction pipeline needed to be reloaded.
  454.     While on the 68020/030 processors this was normally only for the
  455.     branch instructions, in the 68040 this includes a large number of
  456.     other instructions.  (Including NOP!)  Anyway, if an Enforcer hit
  457.     happens while in T0 tracing mode, the trace will happen even on
  458.     instructions that normally would not cause a T0 mode trace.  Since
  459.     this may actually help in debugging and because it was not possible
  460.     to do anything else, this method of operation is deemed acceptable.
  461.  
  462.     Another issue with the 68040 is that WRITE faults happen *after* the
  463.     instruction has executed.  (Except for MOVEM)  In fact, it is common
  464.     for the 68040 to execute one or more extra instructions before the
  465.     WRITE fault is executed.  This design makes the 68040 much faster,
  466.     but it also makes the Program Counter value that Enforcer can report
  467.     for the fault much less likely to be pointing to the instruction
  468.     that caused it.  The worst cases are sequences such as a write fault
  469.     followed by a branch instruction.  In these cases, the branch is
  470.     usually already executed before the write fault happens and thus the
  471.     PC will be pointing to the target of the branch.  There is nothing
  472.     that can be done within Enforcer to help out here.  You will just
  473.     need to be aware of this and deal with it as best as possible.
  474.  
  475.     Along with the above issue, is the fact that since a write fault may
  476.     be delayed, a read fault may happen before the write fault shows up.
  477.     Internally, enforcer does not do special processing for these and
  478.     they will not show up.  Since another hit was happening anyway, it
  479.     is felt that it is best to just not report the hit.  Along the same
  480.     lines, the hit generated from a MOVEM instruction may only show as a
  481.     single hit rather than 1 for each register moved.
  482.  
  483.     On the Amiga, MOVE16 is not supported 100%.  Causing an Enforcer hit
  484.     with a MOVE16 will cause major problems and maybe cause Enforcer or
  485.     your task to lock.  Since MOVE16 is not supported, this is not a
  486.     major issue.  Just watch out if you are using this 68040
  487.     instruction.  (Also, watch out for the 68040 CPU bug with MOVE16)
  488.  
  489.     The functions CachePreDMA(), CachePostDMA(), and CacheControl() are
  490.     patched when the 68040 MMU is turned on by Enforcer.  These
  491.     functions are patched such the issues with DMA and the 68040
  492.     COPYBACK data caches are addressed.  The 68040.library normally
  493.     deals with this, however since Enforcer turns on the MMU, the method
  494.     of dealing with it in the 68040.library will not work. For this
  495.     reason, Enforcer will patch these and implement the required fix for
  496.     when the MMU is on.  When Enforcer is asked to exit, it will check
  497.     if it can remove itself from these functions.  If it can not, it
  498.     will ignore the request to exit.  If Enforcer was run from the CLI,
  499.     it will print a message saying that it can not exit when the attempt
  500.     is made.
  501.  
  502.    WRITING DEBUGGERS
  503.     If you wish to make a debugger that works with Enforcer to help
  504.     pinpoint Enforcer hits in the application and not cause Enforcer
  505.     hits itself, here are some simple tips and a bit of code.
  506.  
  507.    DEBUGGERS:  TRAPPING A HIT
  508.     To trap a hit requires a number of things to work.
  509.  
  510.     First, the debugger itself must never cause an Enforcer hit.
  511.     For help on that, see the "DEBUGGERS: NOT CAUSING A HIT"
  512.  
  513.     Second, the debugger must be global.  That is, you must be
  514.     able to deal with a task getting a hit that is not the task
  515.     under test.  There are a number of simple ways to deal with
  516.     this, and I will leave this up to the debugger writer.
  517.     (One method will be shown below)
  518.  
  519.     Third, the debugger must start *AFTER* Enforcer starts.
  520.     If it is started before Enforcer, the hits will not be
  521.     trapped.  (Note that this is not a problem)
  522.  
  523.     A very important point:  The code needs to be fast for
  524.     the special case of location 4.  This is shown in the
  525.     code below.  It is very important that this be fast.
  526.  
  527.     Note that it is much prefered that debuggers use the
  528.     method described below for trapping hits.  It should
  529.     be much more supportable this way as any of the tricky
  530.     work that may need to be done in the hit processing
  531.     will be handled by Enforcer itself.  If you wish the
  532.     hit decoded, you can capture the Enforcer output via a
  533.     pipe or some other method (such as RAWIO) or you can
  534.     leave that issue up to the user.
  535.  
  536.     Now, given the above, the following bits of code can be
  537.     used to get the debugger to switch into single-step mode
  538.     at the point of the Enforcer hit.  You can also set some
  539.     data value here to tell your debugger about this.
  540.  
  541.     ;
  542.     ; The following code is inserted into the bus error vector.
  543.     ; Make sure you follow the VBR to find the vector.
  544.     ; Store the old vector in the address OldVector
  545.     ; Make sure you already have the single-step trap vector
  546.     ; installed before you install this.  Note that any extra
  547.     ; code you add in the comment area *MUST NOT* cause a bus
  548.     ; fault of any kind, including reading of location 4.
  549.     ;
  550.     ; This is the 68020 and 68030 version...
  551.     ;
  552.     EnforcerHit:    ds.l    1                       ; Some private flag
  553.     MyTask:         ds.l    1                       ; Task under test
  554.     MyExecBase:     ds.l    1                       ; The local copy
  555.     OldVector:      ds.l    1                       ; One long word
  556.     NewVector:      cmp.l   #4,$10(sp)              ; 68020 and 68030
  557.                     beq.s   TraceSkip               ; If AbsExecBase, OK
  558.                     ;
  559.                     ; Now, if you wish to only trap a specific task,
  560.                     ; do the check at this point.  For example, a
  561.                     ; simple single-task debugger would do something
  562.                     ; like this:
  563.                     move.l  a0,-(sp)                ; Save this...
  564.                     move.l  MyExecBase(pc),a0       ; Get ExecBase...
  565.                     move.l  ThisTask(a0),a0         ; Get ThisTask
  566.                     cmp.l   MyTask(pc),a0           ; Are they the same?
  567.                     move.l  (sp)+,a0                ; Restore A0 (no flags)
  568.                     bne.s   TraceSkip               ; If not my task, skip
  569.                     ;
  570.                     bset.b  #7,(sp)                 ; Set trace bit...
  571.                     ; If you have any other data to set, do it now...
  572.                     ; Set as setting the EnforcerHit bit in your data...
  573.                     addq.l    #1,EnforcerHit          : Count the hit...
  574.                     ;
  575.     TraceSkip:      move.l  OldVector(pc),-(sp)     ; Ready to return
  576.                     rts
  577.     ;
  578.     ; This is the 68040 version...
  579.     ;
  580.     NewVector040:   cmp.l   #4,$14(sp)              ; 68040
  581.                     beq.s   TraceSkip040            ; If AbsExecBase, OK
  582.                     ;
  583.                     ; Now, if you wish to only trap a specific task,
  584.                     ; do the check at this point.  For example, a
  585.                     ; simple single-task debugger would do something
  586.                     ; like this:
  587.                     move.l  a0,-(sp)                ; Save this...
  588.                     move.l  MyExecBase(pc),a0       ; Get ExecBase...
  589.                     move.l  ThisTask(a0),a0         ; Get ThisTask
  590.                     cmp.l   MyTask(pc),a0           ; Are they the same?
  591.                     move.l  (sp)+,a0                ; Restore A0 (no flags)
  592.                     bne.s   TraceSkip               ; If not my task, skip
  593.                     ;
  594.                     bset.b  #7,(sp)                 ; Set trace bit...
  595.                     ; If you have any other data to set, do it now...
  596.                     ; Set as setting the EnforcerHit bit in your data...
  597.                     addq.l    #1,EnforcerHit          : Count the hit...
  598.                     ;
  599.     TraceSkip040:   move.l  OldVector(pc),-(sp)     ; Ready to return
  600.                     rts
  601.  
  602.    DEBUGGERS:  NOT CAUSING A HIT
  603.     In order not to cause Enforcer hits, you can do a number
  604.     of things.  The easiest is to test the address with the TypeOfMem()
  605.     EXEC function.  If TypeOfMem() returns 0, the address is not
  606.     in the memory lists.  However, this does not mean it is not a
  607.     valid address in all cases.  (ROM, chip registers, I/O boards)
  608.     For those cases, you can build a "valid memory access table"
  609.     much like Enforcer does.  Here is the code from Enforcer for
  610.     the base memory tables:
  611.  
  612.     /*
  613.      * Mark_Address(mmu,start address,length,type)
  614.      */
  615.  
  616.     /*
  617.      * Special case the first page of CHIP RAM
  618.      */
  619.     mmu=Mark_Address(mmu,0,0x1000,INVALID | NONCACHEABLE);
  620.  
  621.     /*
  622.      * Map in the free memory
  623.      */
  624.     Forbid();
  625.     mem=(struct MemHeader *)SysBase->MemList.lh_Head;
  626.     while (mem->mh_Node.ln_Succ)
  627.     {
  628.       mmu=Mark_Address(mmu,
  629.                        (ULONG)(mem->mh_Lower),
  630.                        (ULONG)(mem->mh_Upper)-(ULONG)(mem->mh_Lower),
  631.                        ((MEMF_CHIP & TypeOfMem(mem->mh_Lower)) ?
  632.                          (NONCACHEABLE | VALID) : (CACHEABLE | VALID)));
  633.       mem=(struct MemHeader *)(mem->mh_Node.ln_Succ);
  634.     }
  635.     Permit();
  636.  
  637.     /*
  638.      * Map in the autoconfig boards
  639.      */
  640.     if (ExpansionBase=OpenLibrary("expansion.library",0))
  641.     {
  642.     struct    ConfigDev    *cd=NULL;
  643.  
  644.       while (cd=FindConfigDev(cd,-1L,-1L))
  645.       {
  646.         /* Skip memory boards... */
  647.         if (!(cd->cd_Rom.er_Type & ERTF_MEMLIST))
  648.         {
  649.           mmu=Mark_Address(mmu,
  650.                            (ULONG)(cd->cd_BoardAddr),
  651.                            cd->cd_BoardSize,
  652.                            VALID | NONCACHEABLE);
  653.         }
  654.       }
  655.       CloseLibrary(ExpansionBase);
  656.     }
  657.  
  658.     /*
  659.      * Now for the control areas...
  660.      */
  661.     mmu=Mark_Address(mmu,0x00BC0000,0x00040000,VALID | NONCACHEABLE);
  662.     mmu=Mark_Address(mmu,0x00D80000,0x00080000,VALID | NONCACHEABLE);
  663.  
  664.     /*
  665.      * and the ROM...
  666.      */
  667.     mmu=Mark_Address(mmu,
  668.                      0x00F80000,
  669.                      0x00080000,
  670.                      VALID | CACHEABLE | WRITEPROTECT);
  671.  
  672.     /*
  673.      * If the credit card resource, make the addresses valid...
  674.      */
  675.     if (OpenResource("card.resource"))
  676.     {
  677.       mmu=Mark_Address(mmu,0x00600000,0x00440002,VALID | NONCACHEABLE);
  678.     }
  679.  
  680.     /*
  681.      * If CD-based Amiga (CDTV, A570, etc.)
  682.      */
  683.     if (FindResident("cdstrap"))
  684.     {
  685.       mmu=Mark_Address(mmu,0x00E00000,0x00080000,VALID | NONCACHEABLE);
  686.       mmu=Mark_Address(mmu,0x00B80000,0x00040000,VALID | NONCACHEABLE);
  687.     }
  688.  
  689.     /*
  690.      * Check for ReKick/ZKick/KickIt
  691.      */
  692.     if ((((ULONG)(SysBase->LibNode.lib_Node.ln_Name)) >> 16) == 0x20)
  693.     {
  694.       mmu=Mark_Address(mmu,
  695.                        0x00200000,
  696.                        0x00080000,
  697.                        VALID | CACHEABLE | WRITEPROTECT);
  698.     }
  699.  
  700.    SEE ALSO
  701.     "A master's secrets are only as good as the
  702.      master's ability to explain them to others."  -  Michael Sinz
  703.  
  704.    BUGS
  705.     None?
  706.  
  707. FindHit                                                               FindHit
  708.  
  709.    NAME
  710.     FindHit - A tool that can locate the source file and line number
  711.               that a SegTracker report happened at.
  712.  
  713.    SYNOPSIS
  714.     FindHit will read the executable file and if there is debugging
  715.     information in it, will try to locate the source file and line
  716.     number that correspond to the Enforcer hit HUNK/OFFSET.
  717.  
  718.    FUNCTION
  719.     FindHit uses the Lattice/SAS/MetaScope standard 'LINE'
  720.     debug hunk to locate the closest line to the hunk/offset given.
  721.     Note that this can only happen if the executable has the
  722.     LINE debugging turned on.  (The LawBreaker program has this
  723.     such that you can test this yourself.)
  724.  
  725.     In SAS/C 6.x, you need to compile with DEBUG=LINE or better
  726.     and do not use the link option of NODEBUG.
  727.  
  728.     In SAS/C 5.x, you need to compile with -d1 or better.
  729.     Note that FindHit works with the old SAS/C 5.x 'SRC '
  730.     debugging information too.  This is required for -d2 or
  731.     higher debugging support.  However, I do not have 'SRC ' hunk
  732.     documentation and thus FindHit may be very specific to the
  733.     SAS/C 5.x version of this hunk.
  734.  
  735.     In DICE (2.07 registered being the one I tried) the -d1
  736.     debug switch also supports the 'LINE' debug hunk and
  737.     works with FindHit.
  738.  
  739.     In HX68 and CAPE, you need to add the DEBUG directive to
  740.     the assembly code program.  (See LawBreaker source)
  741.  
  742.     For other languages, or other versions of the above, please
  743.     see the documentation that comes with the language.
  744.  
  745.    INPUTS
  746.     FILE/A      - The executable file, with debugging information.
  747.  
  748.     OFFSETS/A/M - The HEX offset (with or without leading $)
  749.                   If a hunk number other than the default
  750.                   is needed, it is expressed as hunk:offset.
  751.                   The default hunk is that of the last argument
  752.                   or hunk 0 if no hunk number has been given.
  753.                   For example:  12 $22 $3:12 22 4:$12 32 $0:$32
  754.                   will find information for:
  755.                   hunk $0, offset $12
  756.                   hunk $0, offset $22
  757.                   hunk $3, offset $12
  758.                   hunk $3, offset $22
  759.                   hunk $4, offset $12
  760.                   hunk $4, offset $32
  761.                   hunk $0, offset $32
  762.  
  763.    EXAMPLE
  764.     FindHit FooBar $0342 $1:4F2 3:$1A 2C
  765.     badcode.c : Line 184
  766.     No line number information for Hunk $1, Offset $4F2
  767.     badcode2.c : Line 12
  768.  
  769.     See the Enforcer documentation about issues dealing with the
  770.     exact location of the Enforcer hit.  The line given may
  771.     not be exactly where the hit happened.
  772.  
  773.     The way I use this is to always have line debugging turned on
  774.     when I compile.  This does not change the quality of the code
  775.     and takes only a small amount of extra disk space.  However,
  776.     what I do is to link the program twice:  Once to a file called
  777.     program.ld which contains all of the debugging information.
  778.     Then, I link program.ld to program, stripping debug information.
  779.     The command line for SLINK or BLINK is as follows:
  780.  
  781.         BLINK program.ld TO program NODEBUG
  782.  
  783.     I keep both of these on hand; with program being the one I
  784.     distribute and use.  When a hit happens, I can just use program.ld
  785.     with FindHit to get the line number and source file that it happened
  786.     in.  This way you can distribute your software without the debugging
  787.     information and still be able to use FindHit on the actual code.
  788.     (After all, that link command does nothing but strip symbol and
  789.     debug hunks)
  790.  
  791.    NOTES
  792.     Note that this program does nothing when run from the Workbench
  793.     and thus does not have an icon.
  794.  
  795.    SEE ALSO
  796.     "Quantum Physics:  The Dreams that Stuff is made of." - Michael Sinz
  797.  
  798.    BUGS
  799.  
  800. LawBreaker                                                         LawBreaker
  801.  
  802.    NAME
  803.     LawBreaker - A quicky test of Enforcer
  804.  
  805.    SYNOPSIS
  806.     This is a quick test of Enforcer and its reporting abilities.
  807.  
  808.    FUNCTION
  809.     This program is used to make sure that Enforcer is correctly
  810.     installed and operating.  LawBreaker works from either the CLI
  811.     or Workbench.  It will try to read and write certain memory
  812.     areas that will cause an Enforcer hit or four.
  813.  
  814.     LawBreaker will also do an Alert to show how Enforcer reports
  815.     an Alert.
  816.  
  817.     Note that the LawBreaker executable has debugging information
  818.     in it (standard LINE format debug hunk) such that you can
  819.     try the FindHit program to find the line that causes the hit.
  820.  
  821.    INPUTS
  822.     Just run it...
  823.  
  824.    RESULTS
  825.     When running Enforcer, you will see some output from Enforcer.
  826.     Output on a 68030 machine would look something like this:
  827.  
  828. 25-Jul-93  17:15:04
  829. WORD-WRITE to  00000000        data=0000       PC: 0763857C
  830. USP:  07657C14 SR: 0004 SW: 04C1  (U0)(-)(-)  TCB: 07642F70
  831. Data: DDDD0000 DDDD1111 DDDD2222 DDDD3333 0763852A DDDD5555 DDDD6666 DDDD7777
  832. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 0763852A 07400810 --------
  833. Stck: 00000000 0752EE9A 00002800 07643994 00000000 076786D8 000208B0 2EAC80EE
  834. Stck: 487AFD12 486C82C4 4EBA3D50 4EBAEA28 4FEF0014 52ACE2E4 204D43EC 88BC203C
  835. ----> 0763857C - "lawbreaker"  Hunk 0000 Offset 00000074
  836. Name: "Shell"  CLI: "LawBreaker"  Hunk 0000 Offset 00000074
  837.  
  838. 25-Jul-93  17:15:04
  839. LONG-READ from AAAA4444                        PC: 07638580
  840. USP:  07657C14 SR: 0015 SW: 0501  (U0)(F)(-)  TCB: 07642F70
  841. Data: DDDD0000 DDDD1111 DDDD2222 DDDD3333 0763852A DDDD5555 DDDD6666 DDDD7777
  842. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 0763852A 07400810 --------
  843. Stck: 00000000 0752EE9A 00002800 07643994 00000000 076786D8 000208B0 2EAC80EE
  844. Stck: 487AFD12 486C82C4 4EBA3D50 4EBAEA28 4FEF0014 52ACE2E4 204D43EC 88BC203C
  845. ----> 07638580 - "lawbreaker"  Hunk 0000 Offset 00000078
  846. Name: "Shell"  CLI: "LawBreaker"  Hunk 0000 Offset 00000078
  847.  
  848. 25-Jul-93  17:15:04
  849. BYTE-WRITE to  00000101        data=11         PC: 0763858A
  850. USP:  07657C14 SR: 0010 SW: 04A1  (U0)(F)(D)  TCB: 07642F70
  851. Data: 00000000 DDDD1111 DDDD2222 DDDD3333 0763852A DDDD5555 DDDD6666 DDDD7777
  852. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 0763852A 07400810 --------
  853. Stck: 00000000 0752EE9A 00002800 07643994 00000000 076786D8 000208B0 2EAC80EE
  854. Stck: 487AFD12 486C82C4 4EBA3D50 4EBAEA28 4FEF0014 52ACE2E4 204D43EC 88BC203C
  855. ----> 0763858A - "lawbreaker"  Hunk 0000 Offset 00000082
  856. Name: "Shell"  CLI: "LawBreaker"  Hunk 0000 Offset 00000082
  857.  
  858. 25-Jul-93  17:15:04
  859. LONG-WRITE to  00000102        data=00000000   PC: 07638592
  860. USP:  07657C14 SR: 0014 SW: 0481  (U0)(-)(D)  TCB: 07642F70
  861. Data: 00000000 DDDD1111 DDDD2222 DDDD3333 0763852A DDDD5555 DDDD6666 DDDD7777
  862. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 0763852A 07400810 --------
  863. Stck: 00000000 0752EE9A 00002800 07643994 00000000 076786D8 000208B0 2EAC80EE
  864. Stck: 487AFD12 486C82C4 4EBA3D50 4EBAEA28 4FEF0014 52ACE2E4 204D43EC 88BC203C
  865. ----> 07638592 - "lawbreaker"  Hunk 0000 Offset 0000008A
  866. Name: "Shell"  CLI: "LawBreaker"  Hunk 0000 Offset 0000008A
  867.  
  868. 25-Jul-93  17:15:06
  869. Alert !! Alert 35000000     TCB: 07642F70     USP: 07657C10
  870. Data: 00000000 DDDD1111 DDDD2222 DDDD3333 0763852A DDDD5555 DDDD6666 35000000
  871. Addr: AAAA0000 AAAA1111 AAAA2222 AAAA3333 AAAA4444 0763852A 07400810 --------
  872. Stck: 076385A0 00000000 0752EE9A 00002800 07643994 00000000 0762F710 076305F0
  873. ----> 076385A0 - "lawbreaker"  Hunk 0000 Offset 00000098
  874.  
  875.     Now, using FindHit, you would type:
  876.  
  877.     FindHit LawBreaker 0:82
  878.  
  879.     and it will tell you the source file name and the line number
  880.     where the hit happened.  See the FindHit documentation.
  881.  
  882.    NOTES
  883.     If enforcer is not running, the program should not cause the
  884.     system to crash.  It will, however, write to certain areas
  885.     of low memory.  Also, it will cause read access of some
  886.     addresses that may not exist.  This may cause bus faults.
  887.  
  888.    SEE ALSO
  889.     "Quantum Physics:  The Dreams that Stuff is made of." - Michael Sinz
  890.  
  891.    BUGS
  892.     There are 4 known Enforcer hits in this code and 1 alert, however,
  893.     they will not be fixed.  ;^)
  894.  
  895. Move4K                                                                 Move4K
  896.  
  897.    NAME
  898.     Move4K - Moves as much out of the lower 4K of RAM as possible
  899.  
  900.    SYNOPSIS
  901.     On 68040 systems, as much of the lower 4K of CHIP RAM as possible
  902.     is removed from system use.
  903.  
  904.    FUNCTION
  905.     On 68040 systems the MMU page sizes are 4K and 8K.  Enforcer
  906.     uses the 4K page size.  Since watching for hits of low memory
  907.     is a vital part of Enforcer, this means that the first 4K
  908.     of RAM will be marked invalid.  On current systems, only
  909.     the first 1K of RAM is invalid and thus 3K of RAM in that
  910.     first 4K will end up needing to be emulated in Enforcer.
  911.     In order to reduce the overhead that this causes (and the
  912.     major performance loss) this program will try to move as much
  913.     from that first 4K as possible and make any of the free
  914.     memory within the first 4K inaccessible.
  915.  
  916.     Enforcer itself also has this logic, but it may be useful
  917.     to be able to run this program as the first program in
  918.     the Startup-Sequence (*AFTER* SetPatch) to try to limit
  919.     the number of things that may use the lower 4K of RAM.
  920.  
  921.    INPUTS
  922.     Just run it...  Can be run from CLI or Workbench
  923.  
  924.    RESULTS
  925.     Any available memory in the lower 4K of CHIP RAM is removed
  926.     plus a special graphics buffer is moved if it needs to be.
  927.     After running this program you may have a bit less CHIP RAM
  928.     than before.  You can run this program as many times as you
  929.     wish since it only moves things if it needs to.
  930.  
  931.    NOTES
  932.     This program will do nothing on systems without a 68040.
  933.     It does not, however, check for the MMU and thus it will
  934.     move the lower 4K even if the CPU is not able to run Enforcer.
  935.  
  936.     V39 of the operating system already does have the lowest
  937.     MMU page empty and thus this program will effectively do
  938.     nothing under V39.
  939.  
  940.    SEE ALSO
  941.     "Eloquence is vehement simplicity"
  942.  
  943.    BUGS
  944.     None.
  945.  
  946. SegTracker                                                         SegTracker
  947.  
  948.    NAME
  949.     SegTracker - A global SegList tracking utility
  950.  
  951.    SYNOPSIS
  952.     A global tracking utility for disk loaded files including
  953.     libraries and devices.  If placed in the startup-sequence
  954.     right after SetPatch, it will track all disk loaded segments
  955.     (other than those loaded by SetPatch)
  956.  
  957.    FUNCTION
  958.     SegTracker will patch the DOS LoadSeg(), NewLoadSeg(), and UnLoadSeg()
  959.     functions in order to track the SegLists that are loaded.
  960.     SegTracker keeps these seglist stored in a "safe" manner and
  961.     even handles programs which SegList split.
  962.  
  963.     The first time the program is run, it installs the patches
  964.     and semaphore.  After that point, it just finds the semaphore
  965.     and uses it.
  966.  
  967.     By using SegTracker, it will be possible to better identify
  968.     where Enforcer hits come from when dealing with libraries
  969.     and devices.  Basically, it is a system-global Hunk-o-matic.
  970.  
  971.     External programs can then pass in an address to SegTracker
  972.     either via the command line or via the given function pointer
  973.     in the SegTracker semaphore and get back results as to what
  974.     hunk and offset the address is at.
  975.  
  976.     To work with the function directly, you need to find the
  977.     the semaphore of "SegTracker" using FindSemaphore().
  978.     The structure found will be the following:
  979.  
  980.     struct  SegSem
  981.     {
  982.     struct  SignalSemaphore seg_Semaphore;
  983.             SegTrack        *seg_Find;
  984.     };
  985.  
  986.     The function pointer points to a routine that takes an address
  987.     and two pointers to long words for returning the Segment number
  988.     and Offset within the segment.  The function returns the name
  989.     of the file loaded.  Note that you must call this function
  990.     while in Forbid() and then copy the name as the seglist may
  991.     be UnLoadSeg'ed at any moment and the name string will then
  992.     no longer be in memory.
  993.  
  994.     typedef    char (* __asm SegTrack(register __a0 ULONG Address,
  995.                                    register __a1 ULONG *SegNum,
  996.                                    register __a2 ULONG *Offset));
  997.  
  998.     The above is for use in C code function pointer prototype
  999.     in SAS/C 5 and 6.
  1000.  
  1001.    INPUTS
  1002.     SHOW/S  - Shows all of the segments being tracked.
  1003.  
  1004.     DUMP/S  - Displays all of the segment elements being tracked.
  1005.  
  1006.     FIND/M  - Find the hex (in $xxxxx format) address in
  1007.               the tracked segments.  Multiple addresses
  1008.               can be given.
  1009.  
  1010.     Options are not available from Workbench as they require
  1011.     the CLI.  However, you can run SegTracker from Workbench
  1012.     to install it.
  1013.  
  1014.    EXAMPLE USAGE
  1015.     /*
  1016.      * A simple program that will "find" given addresses in the SegLists
  1017.      * This program has been compiled with SAS/C 6.2 without errors or
  1018.      * warnings.
  1019.      *
  1020.      * Compiler options:
  1021.      * DATA=FARONLY PARAMETERS=REGISTER NOSTACKCHECK
  1022.      * NOMULTIPLEINCLUDES STRINGMERGE STRUCTUREEQUIVALENCE
  1023.      * MULTIPLECHARACTERCONSTANTS DEBUG=LINE NOVERSION
  1024.      * OPTIMIZE OPTIMIZERINLOCAL NOICONS
  1025.      *
  1026.      * Linker options:
  1027.      * FindSeg.o TO FindSeg SMALLCODE SMALLDATA NODEBUG LIB LIB:sc.lib
  1028.      */
  1029.     #include <exec/types.h>
  1030.     #include <exec/execbase.h>
  1031.     #include <exec/libraries.h>
  1032.     #include <exec/semaphores.h>
  1033.     #include <dos/dos.h>
  1034.     #include <dos/dosextens.h>
  1035.     #include <dos/rdargs.h>
  1036.  
  1037.     #include <clib/exec_protos.h>
  1038.     #include <pragmas/exec_sysbase_pragmas.h>
  1039.  
  1040.     #include <clib/dos_protos.h>
  1041.     #include <pragmas/dos_pragmas.h>
  1042.  
  1043.     #include <string.h>
  1044.  
  1045.     #include "FindSeg_rev.h"
  1046.  
  1047.     #define EXECBASE (*(struct ExecBase **)4)
  1048.  
  1049.     typedef char (* __asm SegTrack(register __a0 ULONG,
  1050.                                    register __a1 ULONG *,
  1051.                                    register __a2 ULONG *));
  1052.  
  1053.     struct SegSem
  1054.     {
  1055.     struct SignalSemaphore seg_Semaphore;
  1056.            SegTrack        *seg_Find;
  1057.     };
  1058.  
  1059.     #define SEG_SEM "SegTracker"
  1060.  
  1061.     #define TEMPLATE "FIND/M" VERSTAG
  1062.  
  1063.     #define OPT_FIND  0
  1064.     #define OPT_COUNT 1
  1065.  
  1066.     ULONG cmd(void)
  1067.     {
  1068.     struct ExecBase *SysBase;
  1069.     struct Library  *DOSBase;
  1070.     struct RDArgs   *rdargs;
  1071.            ULONG    rc=RETURN_FAIL;
  1072.     struct SegSem   *segSem;
  1073.            char     **hex;
  1074.            LONG     opts[OPT_COUNT];
  1075.  
  1076.       SysBase = EXECBASE;
  1077.       if (DOSBase = OpenLibrary("dos.library",37))
  1078.       {
  1079.         memset((char *)opts, 0, sizeof(opts));
  1080.  
  1081.         if (!(rdargs = ReadArgs(TEMPLATE, opts, NULL)))
  1082.         {
  1083.           PrintFault(IoErr(),NULL);
  1084.         }
  1085.         else if (CheckSignal(SIGBREAKF_CTRL_C))
  1086.         {
  1087.           PrintFault(ERROR_BREAK,NULL);
  1088.         }
  1089.         else if (segSem=(struct SegSem *)FindSemaphore(SEG_SEM))
  1090.         {
  1091.           rc=RETURN_OK;
  1092.           if (opts[OPT_FIND])
  1093.           {
  1094.             for (hex=(char **)opts[OPT_FIND];(*hex);hex++)
  1095.             {
  1096.             char  *p;
  1097.             ULONG val;
  1098.             ULONG tmp[4];
  1099.             ULONG c;
  1100.  
  1101.               val=0;
  1102.               p=*hex;
  1103.               if (*p=='$') p++; /* Support $hex */
  1104.               while (*p)
  1105.               {
  1106.                 c=(ULONG)*p;
  1107.                 if ((c>='a') && (c<='f')) c-=32;
  1108.                 c-='0';
  1109.                 if (c>9)
  1110.                 {
  1111.                   c-=7;
  1112.                   if (c<10) c=16;
  1113.                 }
  1114.  
  1115.                 if (c<16)
  1116.                 {
  1117.                   val=(val << 4) + c;
  1118.                   p++;
  1119.                 }
  1120.                 else
  1121.                 {
  1122.                   val=0;
  1123.                   p=&p[strlen(p)];
  1124.                 }
  1125.               }
  1126.  
  1127.               /*
  1128.                * Ok, we need to do this within Forbid()
  1129.                * as segments can unload at ANY time, including
  1130.                * during AllocMem(), so we use a stack buffer...
  1131.                *
  1132.                */
  1133.               Forbid();
  1134.               if (p=(*segSem->seg_Find)(tmp[0]=val,&tmp[2],&tmp[3]))
  1135.               {
  1136.               char Buffer[200];
  1137.  
  1138.                 stccpy(Buffer,p,200);
  1139.                 tmp[1]=(ULONG)Buffer;
  1140.                 VPrintf("$%08lx - %s : Hunk %ld, Offset $%08lx",tmp);
  1141.  
  1142.                 /*
  1143.                  * Now get the SegList address by passing the
  1144.                  * same pointer for both hunk & offset.  Note
  1145.                  * that this is only in the newer SegTrackers
  1146.                  * To test if this worked, check if the result
  1147.                  * of this call is either a hunk or an offset.
  1148.                  */
  1149.                 (*segSem->seg_Find)(val,&tmp[0],&tmp[0]);
  1150.                 /*
  1151.                  * This "kludge" is for compatibility reasons
  1152.                  * Check if result is the same as either the hunk
  1153.                  * or the offset.  If so, do not print it...
  1154.                  */
  1155.                 if ((tmp[0]!=tmp[2]) && (tmp[0]!=tmp[3]))
  1156.                 {
  1157.                   VPrintf(", SegList $%08lx",tmp);
  1158.                 }
  1159.  
  1160.                 PutStr("\n");
  1161.               }
  1162.               else VPrintf("$%08lx - Not found\n",tmp);
  1163.               Permit();
  1164.             }
  1165.           }
  1166.         }
  1167.         else PutStr("Could not find SegTracker semaphore.\n");
  1168.  
  1169.         if (rdargs) FreeArgs(rdargs);
  1170.         CloseLibrary(DOSBase);
  1171.       }
  1172.       else if (DOSBase=OpenLibrary("dos.library",0))
  1173.       {
  1174.         Write(Output(),"Requires Kickstart 2.04 (37.175) or later.\n",43);
  1175.         CloseLibrary(DOSBase);
  1176.       }
  1177.  
  1178.       return(rc);
  1179.     }
  1180.  
  1181.    NOTES
  1182.     The earlier this command is run, the better off it will be in
  1183.     tracking disk loaded segments.  Under debug usage, you may
  1184.     wish to run the command right *AFTER* SetPatch.
  1185.  
  1186.     Some things may not call UnLoadSeg() to free their seglists.
  1187.     There is no way SegTracker can follow a seglist that is not
  1188.     unloaded via the dos.library call to UnLoadSeg().  For this
  1189.     reason, SegTracker adds new LoadSeg() segments to the top
  1190.     of its list.  This way, if any old segments are still on
  1191.     the list but have been unloaded via some other method
  1192.     they will not clash with newer segments during the find operation.
  1193.  
  1194.     Note that the resident list is one such place where
  1195.     UnLoadSeg() is not called to free the seglist.  Thus,
  1196.     if something is made resident and then later unloaded
  1197.     it will still be listed as tracked by SegTracker.
  1198.  
  1199.     In order to support a new feature in CPR, the SegTracker function
  1200.     got a "kludge" added to it.  If a segment is found, you can then
  1201.     call the function again with the same address but with having
  1202.     both pointers point to the same longword of storage.  By doing
  1203.     this, the function will now return (in that longword) the
  1204.     SegList pointer (CPTR not BPTR) of the file that contains
  1205.     the address.  The reason this method was used was such that
  1206.     it was compatible with older SegTracker versions.  In older
  1207.     versions you would not get the result you wanted but you would
  1208.     also not crash.  See the example above for more details on how
  1209.     to use this feature.  The SegTracker FIND option has been
  1210.     expanded to include this information.
  1211.  
  1212.     Due to the fact that I am working on a design of a new set of
  1213.     debugging tools (Enforcer/SegTracker/etc)  I do not wish to
  1214.     expand the current SegTracker model in too many ways.
  1215.  
  1216.    SEE ALSO
  1217.     "Quantum Physics:  The Dreams that Stuff is made of." - Michael Sinz
  1218.  
  1219.    BUGS
  1220.  
  1221.