home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / FINDSEG.ZIP / TRAPD.DOC < prev   
Text File  |  1992-06-03  |  11KB  |  263 lines

  1.  
  2. 1. About TRAP-D
  3.  
  4. TRAP-D is caused by invalid memory access operations. The following operations
  5. cause TRAP-D.
  6.    (1) The segment register points to an invalid segment descriptor.
  7.    (2) The offset of the address goes beyond the size of the segment descriptor.
  8.        (When the stack pointer goes beyond the size of the stack segment,
  9.         TRAP-C(stack overflow) occurs.)
  10.    (3) The privilege rules have been violated. (A program accesses the data
  11.        which belongs to the different privilege level.)
  12.    (4) Code segment is loaded with a value of the data segment descriptor.
  13.        (except alias segment)
  14.    (5) A program tries to read from an execute only code segment or to write
  15.        into a read only data segment.
  16.  
  17. Generally, most of TRAP-Ds are caused by case (1) or (2).
  18. The following C sample programs illustrate case (1) and (2).
  19.  
  20. Case (1).
  21.         void main(int argc, char **argv, char **envp)
  22.         {
  23.              /* p has an invalid selector value 0x1234 */
  24.              int far *p = (int far *)0x12345678L;
  25.  
  26.              /* TRAP-D will occur on the following code  because it tries */
  27.              /* to access an invalid address.                             */
  28.              *p = 1;
  29.                 :
  30.                 :
  31.         }
  32.  
  33.  
  34. Case (2).
  35.         void main(int argc, char **argv, char **envp)
  36.         {
  37.              char far *p;
  38.              unsigned selector;
  39.  
  40.              /* Allocate a memory whose size is 10 bytes */
  41.              DosAllocSeg(10, &selector, 0);
  42.  
  43.              /* Make p point to the top of the memory area allocated by the */
  44.              /* above DosAllocSeg                                           */
  45.              FP_SEG(p) = selector;
  46.              FP_OFF(p) = 0;
  47.  
  48.              p += 100;
  49.  
  50.              /* TRAP-D will occur on the following code because the offset */
  51.              /* of p goes beyond the size of the memory area (i.e. 10).     */
  52.              *p = 1;
  53.                 :
  54.                 :
  55.         }
  56.  
  57.  
  58. 2. About TRAP-D popup panel
  59.  
  60. When a program causes a protection violation (TRAP-D), you will see the
  61. following popup panel.
  62.  
  63.    +-----------------------------------------------------------------+
  64.    |                                                                 |
  65.  1 |    Session Title:                                               |
  66.  2 |    XXXXXXXX                                                     |
  67.  3 |                                                                 |
  68.  4 |          SYS1943: A program caused a protection violation.      |
  69.  5 |                                                                 |
  70.  6 |          TRAP 000D                                              |
  71.  7 |          AX=0000 BX=0000 CX=000B DX=0047 BP=0FF0                |
  72.  8 |          SI=02FF DI=1349 DS=0067 ES=0000 FLG=2246               |
  73.  9 |          CS=0047 IP=0017 SS=0067 SP=0FEC MSW=FFFB               |
  74. 10 |          CSLIM=0624 SSLIM=134F DSLIM=134F ESLIM=****            |
  75. 11 |          CSACC=FB SSACC=F3 DSACC=F3 ESACC=**                    |
  76. 12 |          ERRCD=1234 ERLIM=**** ERACC=**                         |
  77.    |                                                                 |
  78.    +-----------------------------------------------------------------+
  79.    |              End the program                                    |
  80.    |                                                                 |
  81.    |                                                                 |
  82.    +-----------------------------------------------------------------+
  83.  
  84.  
  85.    Line 2  : Session name to which the TRAP-D program belongs.
  86.    Line 4  : Cause of the error. When a program causes a TRAP-C, this line will
  87.              be
  88.              SYS1942: A program attempted to reference storage outside the
  89.              limits of a stack segment.
  90.    Line 6  : Kind of traps
  91.    Line 7-9: Dump of registers.
  92.    Line 10 : Size of the segment that segment register points to.
  93.              When a value of the segment register is invalid, this field is
  94.              filled with ****.
  95.    Line 11 : (80286 architecture) Access rights byte of the segment descriptor.
  96.              This access rights byte of the general segment descriptor is as
  97.              follows.
  98.  
  99.                 bit 0: Access bit (used by OS for memory management)
  100.                 bit 1: Access type
  101.                             Code segment     Data segment
  102.                          0  Execute only     Read only
  103.                          1  Execute/Read     Read/Write
  104.                 bit 2: Expansion direction (DATA segment)
  105.                          0  Downward
  106.                          1  Upward
  107.                        Conforming type
  108.                          0  Non-conforming segment
  109.                          1  Conforming segment
  110.                 bit 3: Segment type
  111.                          0  Non-executable segment (DATA segment)
  112.                          1  Executable segment (CODE segment)
  113.                 bit 4: Always 1.
  114.                 bit 5-6: Descriptor privilege level (from 0 to 3)
  115.                 bit 7: Present bit (used by OS for memory management)
  116.  
  117.              If A program runs in the descriptor privilege level 3, Access
  118.              rights byte of a CODE segment is 0xFB and DATA segment is 0xF3.
  119.              When a value of the segment register is invalid, this field is
  120.              filled with **.
  121.    Line 12 : Value of the segment descriptor which is the cause of the TRAP-D,
  122.              its size, and its access rights byte.
  123.              In the above sample code of the case (1), ERRCD=1234, ERLIM=****,
  124.              and ERACC=** because 0x1234 is an invalid segment descriptor value.
  125.  
  126.  
  127. 3. How to use FINDSEG to find out TRAP-D module.
  128.  
  129. To search TRAP-D module, you had better set CSLIM value in TRAP-D panel as
  130. segment length(-l switch) rather than DSLIM, ESLIM or SSLIM because data
  131. segments(DS, ES, SS) might be allocated or reallocated after the process
  132. started.
  133.  
  134. You had better set IP value(by -I switch). When two or more modules are found,
  135. you can determine TRAP-D module from disassembled code. For example, TRAP-D
  136. may not occur on the following instructions
  137.         mov   ax, 0
  138.         inc   cx
  139.            :
  140.         etc.
  141.  
  142. and the following instructions have potential of TRAP-D (or TRAP-C).
  143.                                      TRAP-D occurs on the following condition.
  144.         pop   ax                <--- sp exceeds SSLIM (In this case TRAP-C will
  145.                                      occur).
  146.         les   bx, es:╒bx+si■    <--- bx+si exceeds ESLIM or ES is invalid
  147.         rep   movs              <--- si exceeds DSLIM or di exceeds ESLIM
  148.            :
  149.         etc.
  150.  
  151. So I recommend you the following command to find out TRAP-D module from the
  152. TRAP-D popup panel mentioned in section 3.
  153.  
  154.         findseg -c -lx624 -Ix17 -s C:\
  155.         (CODE segment only, CSLIM=0624, IP=0017)
  156.  
  157. If doubtful EXEs and DLLs are in other drives too (i.e. LIBPATH and PATH
  158. environment includes other drives), you have to check out each drives.
  159.  
  160.  
  161. 4. How to interpret outputs of FINDSEG
  162.  
  163. The following depicts outputs of findseg.
  164.   C:\OS2\DLL\PMGPI.DLL <--- File name
  165.      265110bytes    90-03-30    01:20:00   <--- File size and update time
  166.   001 05FCC CODE MOVABLE NONSHARED LOADONCALL E/R RELOC   PL2 NONDISCARDABLE
  167.   (1)  (2)   (3)   (4)     (5)       (6)      (7)   (8)   (9)       (10)
  168.     Instruction: push     es:╒bx+si■  <-- Disassembled code
  169.  
  170.   (1) : Entry id in segment table
  171.   (2) : Minimum allocation size for the segment
  172.   (3) : Segment type (CODE/DATA)
  173.   (4) : Fixed or Movable segment
  174.   (5) : Shared or Nonshared segment
  175.   (6) : Preload or Load on call segment
  176.   (7) : Execute/read or Execute-only if code, Read/Write or Read-only if data
  177.   (8) : Relocation table present or not
  178.   (9) : Privilege level
  179.   (10): Discardable or Nondiscardable segment
  180.  
  181. File name, Minimum allocation size, segment type and disassembled code are
  182. important in above information. Other information is not used usually.
  183.  
  184. If two or more segments are reported, you have to check disassembled code.
  185. If "IP is invalid value" is printed, the code segment is not what you want.
  186. If a disassembled code is printed, you have to check whether the instruction
  187. caused a TRAP-D(or TRAP-C) or not from segment register values in a popup panel.
  188.  
  189.    Example 1.
  190.  
  191.       Instruction:   mov ax, es:╒bx+si■
  192.       ESLIM=0020, BX=0010, SI=0016
  193.                     |
  194.                     |
  195.                     V
  196.       Because offset(bx+si=0026) exceeds ESLIM, this instruction will cause a
  197.       TRAP-D.
  198.  
  199.    Example 2.
  200.  
  201.       Instruction:   mov ax, es:╒bx+si■
  202.       ESLIM=0020, BX=0010, SI=0006
  203.                     |
  204.                     |
  205.                     V
  206.       Because offset(bx+si=0016) is within ESLIM, this instruction will not
  207.       cause a TRAP-D.
  208.  
  209.    Example 3.
  210.  
  211.       Instruction:   mov es, bx
  212.       ERRCD=1234, ERLIM=****, BX=1234
  213.                     |
  214.                     |
  215.                     V
  216.       Because this instruction tries to load an invalid segment descriptor value
  217.       (BX=1234) into ES, it will cause a TRAP-D.
  218.  
  219.    Example 4.
  220.  
  221.       Instruction:   rep movs
  222.       DSLIM=1000, ESLIM=8000, SI=1002, DI=2002
  223.                     |
  224.                     |
  225.                     V
  226.       Because SI(=1002) exceeds DSLIM, this instruction will cause a TRAP-D.
  227.  
  228.    Example 5.
  229.  
  230.       Instruction:   pop ax
  231.       SSLIM=0BFF, SP=0C00
  232.                     |
  233.                     |
  234.                     V
  235.       Because SP(=0C00) exceeds SSLIM, this instruction will cause a TRAP-C.
  236.  
  237. In case that two or more segments are left after the above considerations,
  238. it might help you that search DSLIM, ESLIM or SSLIM from DATA segments unless
  239. DS, ES and SS were allocated or reallocated after a process started.
  240.  
  241.  
  242. 5. Other Information
  243.  
  244.    (1) Sometimes CSLIM is one byte different from the segment size written in
  245.        an executable file. So the default search range is form CSLIM-1 to
  246.        CSLIM+1.
  247.  
  248.    (2) The module reported by FINDSEG is not always a perpetrator of TRAP-D.
  249.        There is a case that the callee of the module passes an invalid parameter
  250.        to the module.
  251.  
  252.    (3) In case that the TRAP-D is reproducible, Code view(a debugger) will help
  253.        you. For example, If TRAP-D occurs while EXCEL.EXE is running, type
  254.        CVP EXCEL.EXE from OS/2 command prompt. After code view starts, just type
  255.        g and ENTER-KEY, and replay operations that cause TRAP-D. When TRAP-D
  256.        occurs, the code view shows you instructions where TRAP-D occurs and more
  257.        information.
  258.  
  259.  
  260.  
  261.  
  262.  
  263.