Subversion Repositories Kolibri OS

Rev

Rev 5461 | Rev 7677 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;COLOR_THEME fix BLACK_ON_WHITE
  2. COLOR_THEME fix MOVIEOS
  3.  
  4. format binary as ""
  5. include '../../macros.inc'
  6. use32
  7.         db      'MENUET01'
  8.         dd      1
  9.         dd      start
  10.         dd      i_end
  11.         dd      used_mem
  12.         dd      used_mem
  13.         dd      i_param
  14.         dd      0
  15.  
  16. ;-----------------------------------------------------------------------------
  17.  
  18. REG_MODE_CPU equ 1
  19. REG_MODE_MMX equ 2
  20. REG_MODE_SSE equ 3
  21. REG_MODE_AVX equ 4
  22.  
  23. ;-----------------------------------------------------------------------------
  24.  
  25. include 'gui.inc' ; GUI routines
  26.  
  27. ;-----------------------------------------------------------------------------
  28. ;                          Find command in list
  29.  
  30. find_cmd:
  31. ; all commands are case-insensitive
  32.         push    edi
  33.  
  34.     .x4:
  35.         mov     al, [edi]
  36.         cmp     al, 0
  37.         jz      .x5
  38.         cmp     al, 'A'
  39.         jb      @f
  40.         cmp     al, 'Z'
  41.         ja      @f
  42.         or      al, 20h
  43.  
  44.     @@:
  45.         stosb
  46.         jmp     .x4
  47.  
  48.     ; find command
  49.     .x5:
  50.         pop     edi
  51.  
  52.     .x6:
  53.         cmp     dword [esi], 0
  54.         jz      .x7
  55.         push    esi
  56.         mov     esi, [esi]
  57.         lodsb
  58.         movzx   ecx, al
  59.         push    edi
  60.         repz cmpsb
  61.         pop     edi
  62.         pop     esi
  63.         jz      .x8
  64.         add     esi, 17
  65.         jmp     .x6
  66.  
  67.     .x7:
  68.         stc
  69.  
  70.     .x8:
  71.         ret
  72.  
  73. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DEBUGGING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  76.  
  77. ;-----------------------------------------------------------------------------
  78. ;                                 Help event
  79.  
  80. OnHelp:
  81.         mov     esi, help_msg
  82.         mov     edi, [curarg]
  83.         cmp     byte [edi], 0
  84.         jz      .x
  85.         mov     esi, help_groups
  86.         call    find_cmd
  87.         jc      .nocmd
  88.         mov     esi, [esi+12]
  89.  
  90.     .x:
  91.         jmp     put_message
  92.  
  93.     .nocmd:
  94.         mov     esi, aUnknownCommand
  95.         jmp     .x
  96.  
  97. ;-----------------------------------------------------------------------------
  98. ;                                Quit event
  99. OnQuit:
  100.         mcall  -1
  101.  
  102. ;-----------------------------------------------------------------------------
  103. ;                        Working with debug context
  104.  
  105. get_new_context:
  106.         mov     esi, context
  107.         mov     edi, oldcontext
  108.         mov     ecx, (ctx_end-context)/4
  109.         rep movsd
  110.  
  111. get_context:
  112.         mcall   69, 1, [debuggee_pid], ctx_end-context, context
  113.         ret
  114.  
  115. set_context:
  116.         mcall    69, 2, [debuggee_pid], 28h, context
  117.         ret
  118.  
  119. get_dump:
  120.         mov     edi, dumpdata
  121.         mov     esi, [edi-4]
  122.         mov     edx, dump_height*10h
  123.         mov     ecx, edx
  124.         xor     eax, eax
  125.         push    edi
  126.         rep stosb
  127.         pop     edi
  128.         mcall    69, 6, [debuggee_pid]
  129.         cmp     eax, -1
  130.         jnz     @f
  131.         mov     esi, read_mem_err
  132.         call    put_message
  133.         xor     eax, eax
  134.  
  135.     @@:
  136.         mov     [edi-8], eax
  137. ;       call    restore_from_breaks
  138. ;       ret
  139.  
  140. ; in: edi=buffer,eax=size,esi=address
  141. restore_from_breaks:
  142.         mov     ebx, breakpoints
  143.  
  144.     @@:
  145.         test    byte [ebx+4], 1
  146.         jz      .cont           ; ignore invalid
  147.         test    byte [ebx+4], 2 or 8
  148.         jnz     .cont           ; ignore disabled and memory breaks
  149.         mov     ecx, [ebx]
  150.         sub     ecx, esi
  151.         cmp     ecx, eax
  152.         jae     .cont
  153.         mov     dl, [ebx+5]
  154.         mov     [edi+ecx], dl
  155.  
  156.     .cont:
  157.         add     ebx, 6
  158.         cmp     ebx, breakpoints+breakpoints_n*6
  159.         jb      @b
  160.         ret
  161.  
  162. ;-----------------------------------------------------------------------------
  163. ;                           Load executable event
  164.  
  165. OnLoad:
  166.         mov     esi, [curarg]
  167.  
  168. OnLoadInit:
  169.         mov     edi, loadname
  170.         or      [prgname_len], -1
  171.         mov     [prgname_ptr], edi
  172.  
  173.     .copyname:
  174.         lodsb
  175.         stosb
  176.         inc     [prgname_len]
  177.         cmp     al, '/'
  178.         jnz     @f
  179.         or      [prgname_len], -1
  180.         mov     [prgname_ptr], edi
  181.  
  182.     @@:
  183.         cmp     al, ' '
  184.         ja      .copyname
  185.         mov     byte [edi-1], 0
  186.         and     [load_params], 0
  187.         dec     esi
  188.         call    get_arg.skip_spaces
  189.         cmp     al, 0
  190.         jz      @f
  191.         mov     [load_params], esi
  192.  
  193.     @@:
  194.         and     [dumppos], 0
  195.         mov     ecx, [symbols]
  196.         jecxz   do_reload
  197.         mcall   68, 13
  198.         and     [symbols], 0
  199.         and     [num_symbols], 0
  200.  
  201. ; TODO: make it local
  202. do_reload:
  203.         ;push    18
  204.         ;pop     eax
  205.         ;push    7
  206.         ;pop     ebx
  207.         mcall    18, 7
  208.         mov     [dbgwnd], eax
  209.         xchg    ecx, eax
  210.         ;push    70
  211.         ;pop     eax
  212.         ;mov     ebx, fn70_load_block
  213.         mcall    70, fn70_load_block
  214.         test    eax, eax
  215.         jns     .load_ok
  216.  
  217.     .load_err:
  218.         push    eax
  219.         mov     esi, load_err_msg
  220.         call    put_message
  221.         pop     eax
  222.         not     eax
  223.         cmp     eax, 0x20
  224.         jae     .unk_err
  225.         mov     esi, [load_err_msgs+eax*4]
  226.         test    esi, esi
  227.         jnz     put_message
  228.  
  229.     .unk_err:
  230.         mov     esi, unk_err_msg
  231.         inc     eax
  232.         push    eax
  233.         call    put_message_nodraw
  234.         jmp     draw_messages
  235.  
  236.     .load_ok:
  237.         mov     [debuggee_pid], eax
  238.         mov     [bSuspended], 1
  239.         mov     eax, 5
  240.         mov     ebx, 20
  241.         int     0x40
  242.  
  243.         push    ecx
  244.         call    get_context
  245.         mov     edi, oldcontext
  246.         mov     ecx, (ctx_end-context)/4
  247.         rep movsd
  248.  
  249.     ; activate debugger window
  250.         pop     ecx
  251.         ;mov     bl, 3
  252.         ;push    18
  253.         ;pop     eax
  254.         mcall    18, 3
  255.         call    redraw_title
  256.         call    draw_registers.redraw
  257.     ; read and draw dump of memory
  258.         call    get_dump
  259.         call    draw_dump.redraw
  260.         call    update_disasm_eip_force
  261.         mov     esi, load_succ_msg
  262.         push    [debuggee_pid]
  263.         call    put_message_nodraw
  264.         call    draw_messages
  265.     ; try to load symbols
  266.         mov     esi, loadname
  267.         mov     edi, symbolsfile
  268.         push    edi
  269.  
  270.     @@:
  271.         lodsb
  272.         stosb
  273.         test    al, al
  274.         jnz     @b
  275.         lea     ecx, [edi-1]
  276.  
  277.     @@:
  278.         dec     edi
  279.         cmp     edi, symbolsfile
  280.         jb      @f
  281.         cmp     byte [edi], '/'
  282.         jz      @f
  283.         cmp     byte [edi], '.'
  284.         jnz     @b
  285.         mov     ecx, edi
  286.  
  287.     @@:
  288.         mov     dword[ecx], '.dbg'
  289.         mov     byte[ecx+4], 0
  290.         pop     esi
  291.         mov     ebp, esi
  292.         push    ecx esi
  293.         call    OnLoadSymbols.silent            ; Try to load .dbg file
  294.         pop     esi ecx
  295.         xor     eax, eax
  296.         cmp     [num_symbols], eax
  297.         jne     @f
  298.         mov     dword[ecx], '.map'              ; If failed, try .map file too
  299.         call    OnLoadSymbols.silent
  300.     @@:
  301.  
  302.     ; now test for packed progs
  303.         cmp     [disasm_buf_size], 100h
  304.         jz      @f
  305.         ret
  306.  
  307.     @@:
  308.         mov     esi, mxp_nrv_sig
  309.         mov     ebp, disasm_buffer
  310.         mov     edi, ebp
  311.         push    3
  312.         pop     ecx
  313.         repz cmpsb
  314.         jnz     .not_mxp_nrv
  315.         cmpsb
  316.         mov     cl, mxp_nrv_sig_size-4
  317.         repz cmpsb
  318.         mov     esi, mxp_nrv_name
  319.         jz      .packed
  320.  
  321.     .not_mxp_nrv:
  322.         mov     esi, mxp_sig
  323.         mov     edi, ebp
  324.         mov     cl, mxp_sig_size
  325.         repz cmpsb
  326.         mov     esi, mxp_name
  327.         jz      .packed
  328.  
  329.     .not_mxp:
  330.         mov     esi, mxp_lzo_sig1
  331.         mov     edi, ebp
  332.         mov     cl, mxp_lzo_sig1_size
  333.         repz cmpsb
  334.         mov     esi, mxp_lzo_name
  335.         jz      .packed
  336.         mov     esi, mxp_lzo_sig2
  337.         mov     edi, ebp
  338.         mov     cl, 8
  339.         repz cmpsb
  340.         jnz     .not_mxp_lzo
  341.         cmpsb
  342.         mov     cl, mxp_lzo_sig2_size - 9
  343.         repz cmpsb
  344.         mov     esi, mxp_lzo_name
  345.         jz      .packed
  346.  
  347.     .not_mxp_lzo:
  348.         mov     esi, mtappack_name
  349.         cmp     dword [ebp], 0xBF5E246A
  350.         jnz     .not_mtappack
  351.         cmp     dword [ebp+8], 0xEC4E8B57
  352.         jnz     .not_mtappack1
  353.         cmp     dword [ebp+12], 0x8D5EA4F3
  354.         jnz     .not_mtappack1
  355.         cmp     byte [ebp+12h], 0xE9
  356.         jz      .packed
  357.  
  358.     .not_mtappack1:
  359.         cmp     word [ebp+8], 0xB957
  360.         jnz     .not_mtappack
  361.         cmp     dword [ebp+14], 0x575EA4F3
  362.         jnz     .not_mtappack2
  363.         cmp     byte [ebp+17h], 0xE9
  364.         jz      .packed
  365.  
  366.     .not_mtappack2:
  367.         cmp     dword [ebp+14], 0x5F8DA4F3
  368.         jnz     .not_mtappack3
  369.         cmp     word [ebp+18], 0xE9FC
  370.         jz      .packed
  371.  
  372.     .not_mtappack3:
  373.         cmp     word [ebp+14], 0xA4F3
  374.         jnz     .not_mtappack
  375.         cmp     byte [ebp+15h], 0xE9
  376.         jz      .packed
  377.  
  378.     .not_mtappack:
  379.         ret
  380.  
  381.     .packed:
  382.         push    esi
  383.         mov     esi, aPacked1
  384.         call    put_message_nodraw
  385.         pop     esi
  386.         call    put_message_nodraw
  387.         mov     esi, aPacked2
  388.         call    put_message
  389.         call    hide_cursor
  390.         ;push    40
  391.         ;pop     eax
  392.         ;push    7
  393.         ;pop     ebx
  394.         mcall    40, 7
  395.  
  396.     .wait:
  397.         ;push    10
  398.         ;pop     eax
  399.         mcall    10
  400.         dec     eax
  401.         jz      .redraw
  402.         dec     eax
  403.         jz      .key
  404.         or      eax, -1
  405.         mcall
  406.  
  407.     .redraw:
  408.         call    draw_window
  409.         call    hide_cursor
  410.         jmp     .wait
  411.  
  412.     .key:
  413.         mov     al, 2
  414.         mcall
  415.         cmp     ah, 'y'
  416.         jz      .yes
  417.         cmp     ah, 'Y'
  418.         jz      .yes
  419.         cmp     ah, 0xD
  420.         jz      .yes
  421.         cmp     ah, 'n'
  422.         jz      .no
  423.         cmp     ah, 'N'
  424.         jnz     .wait
  425.  
  426.     .no:
  427.         ;push    40
  428.         ;pop     eax
  429.         ;mov     ebx, 0x107
  430.         mcall    40, 0x107
  431.         call    draw_cursor
  432.         mov     esi, aN_str
  433.         jmp     put_message
  434.  
  435.     .yes:
  436.         ;push    40
  437.         ;pop     eax
  438.         ;mov     ebx, 0x107
  439.         mcall    40, 0x107
  440.         call    draw_cursor
  441.         mov     esi, aY_str
  442.         call    put_message
  443.         call    OnUnpack
  444.         ret
  445.  
  446. ;-----------------------------------------------------------------------------
  447. ;                       Searching signatures
  448.  
  449. mxp_nrv_sig:
  450.         xor     eax, eax
  451.         mov     ecx, 0x95       ; 0xA1 for programs with parameters
  452.         mov     [eax], ecx
  453.         add     ecx, [eax+24h]
  454.         push    40h
  455.         pop     esi
  456.         mov     edi, [eax+20h]
  457.         push    edi
  458.         rep movsb
  459.         jmp     dword [esp]
  460.         pop     esi
  461.         add     esi, [eax]
  462.         xor     edi, edi
  463.  
  464. mxp_nrv_sig_size = $ - mxp_nrv_sig
  465.  
  466. mxp_sig:
  467.         mov     ecx, 1CBh
  468.         push    46h
  469.         pop     esi
  470.         mov     edi, [20h]
  471.         rep movsb
  472.         mov     ecx, [24h]
  473.         rep movsb
  474.         jmp     dword [20h]
  475.         mov     eax, [20h]
  476.         add     eax, 1CBh
  477.         push    eax
  478.         push    dword [24h]
  479.         push    0
  480.         push    8
  481.         call    $+0x25
  482.  
  483. mxp_sig_size = $ - mxp_sig
  484.  
  485. mxp_lzo_sig1:
  486.         xor     eax, eax
  487.         mov     ebp, 0FFh
  488.         mov     ecx, 175h
  489.         mov     [eax], ecx
  490.         add     ecx, [eax+24h]
  491.         push    45h
  492.         pop     esi
  493.         mov     edi, [eax+20h]
  494.         push    edi
  495.         rep movsb
  496.         jmp     dword [esp]
  497.         pop     ebx
  498.         add     ebx, [eax]
  499.         xor     edi, edi
  500.         cmp     byte [ebx], 11h
  501.         jbe     $+0x1A
  502.  
  503. mxp_lzo_sig1_size = $ - mxp_lzo_sig1
  504.  
  505. mxp_lzo_sig2:
  506.         xor     eax, eax
  507.         mov     ebp, 0FFh
  508.         mov     ecx, 188h       ; or 177h
  509.         mov     [eax], ecx
  510.         add     ecx, [eax+24h]
  511.         push    44h
  512.         pop     esi
  513.         mov     edi, [eax+20h]
  514.         rep movsb
  515.         jmp     dword [eax+20h]
  516.         mov     ebx, [eax+20h]
  517.         add     ebx, [eax]
  518.  
  519. mxp_lzo_sig2_size = $ - mxp_lzo_sig2
  520.  
  521. ;-----------------------------------------------------------------------------
  522. ;                         Reload executable event
  523.  
  524. OnReload:
  525.         cmp     [debuggee_pid], 0
  526.         jnz     terminate_reload
  527.         mov     esi, need_debuggee
  528.         cmp     byte [loadname], 0
  529.         jnz     do_reload
  530.         jz      put_message
  531.  
  532. ; TODO: make it local
  533. terminate_reload:
  534.         mov     [bReload], 1
  535.  
  536. ;-----------------------------------------------------------------------------
  537. ;                        Terminate process event
  538.  
  539. OnTerminate:
  540.         ;mov     ecx, [debuggee_pid]
  541.         ;push    8
  542.         ;pop     ebx
  543.         ;push    69
  544.         ;pop     eax
  545.         mcall    69, 8, [debuggee_pid]
  546.         ret
  547. ;-----------------------------------------------------------------------------
  548. ;                         Suspend process event
  549.  
  550. AfterSuspend:
  551.         mov     [bSuspended], 1
  552.         call    get_new_context
  553.         call    get_dump
  554.         call    redraw_title
  555.         call    draw_registers.redraw
  556.         call    draw_dump.redraw
  557.         call    update_disasm_eip
  558.         ret
  559.  
  560. OnSuspend:
  561.         ;mov     ecx, [debuggee_pid]
  562.         ;push    4
  563.         ;pop     ebx
  564.         ;push    69
  565.         ;pop     eax
  566.         mcall    69, 4, [debuggee_pid]
  567.         call    AfterSuspend
  568.         mov     esi, aSuspended
  569.         jmp     put_message
  570.  
  571. ;-----------------------------------------------------------------------------
  572. ;                        Resume process event
  573.  
  574. DoResume:
  575.         ;mov     ecx, [debuggee_pid]
  576.         ;push    5
  577.         ;pop     ebx
  578.         ;push    69
  579.         ;pop     eax
  580.         mcall    69, 5, [debuggee_pid]
  581.         mov     [bSuspended], 0
  582.         ret
  583.  
  584. OnResume:
  585.         mov     esi, [curarg]
  586.         cmp     byte [esi], 0
  587.         jz      GoOn
  588.         call    calc_expression
  589.         jc      .ret
  590.         mov     eax, ebp
  591.         push    eax
  592.         call    find_enabled_breakpoint
  593.         pop     eax
  594.         jz      GoOn
  595.         mov     bl, 5   ; valid enabled one-shot
  596.         call    add_breakpoint
  597.         jnc     GoOn
  598.         mov     esi, aBreakpointLimitExceeded
  599.         call    put_message
  600.  
  601.     .ret:
  602.         ret
  603.  
  604. GoOn:
  605.     ; test for enabled breakpoint at eip
  606.         mov     eax, [_eip]
  607.         call    find_enabled_breakpoint
  608.         jnz     .nobreak
  609.     ; temporarily disable breakpoint, make step, enable breakpoint, continue
  610.         inc     eax
  611.         mov     [temp_break], eax
  612.         mov     [bAfterGo], 1
  613.         dec     eax
  614.         call    disable_breakpoint
  615.         call    get_context
  616.         or      byte [_eflags+1], 1             ; set TF
  617.         call    set_context
  618.         and     byte [_eflags+1], not 1
  619.         call    DoResume
  620.         ret
  621.  
  622.     .nobreak:
  623.         call    DoResume
  624.         call    redraw_title
  625.         call    draw_registers.redraw
  626.         call    draw_dump.redraw
  627.         ret
  628.  
  629. ;-----------------------------------------------------------------------------
  630. ;                        Detach process event
  631.  
  632. OnDetach:
  633.         ;mov     ecx, [debuggee_pid]
  634.         ;push    3
  635.         ;pop     ebx
  636.         ;push    69
  637.         ;pop     eax
  638.         mcall    69, 3, [debuggee_pid]
  639.         and     [debuggee_pid], 0
  640.         call    redraw_title
  641.         call    draw_registers.redraw
  642.         call    draw_dump.redraw
  643.         call    free_symbols
  644.         mov     esi, aContinued
  645.         jmp     put_message
  646.  
  647. after_go_exception:
  648.         push    eax
  649.         mov     eax, [temp_break]
  650.         dec     eax
  651.         push    esi
  652.         call    enable_breakpoint
  653.     ; in any case, clear TF and RF
  654.         call    get_new_context
  655.         and     [_eflags], not 10100h           ; clear TF,RF
  656.         call    set_context
  657.         xor     edx, edx
  658.         mov     [temp_break], edx
  659.         xchg    dl, [bAfterGo]
  660.         pop     esi
  661.         pop     eax
  662.         cmp     dl, 2
  663.         jnz     @f
  664.         lodsd
  665.         push    esi
  666.         call    get_dump
  667.         jmp     exception.done
  668.  
  669.     @@:
  670.         test    eax, eax
  671.         jz      .notint1
  672.     ; if exception is result of single step, simply ignore it and continue
  673.         test    dword [esi], 0xF
  674.         jnz     dbgmsgstart.5
  675.         lodsd
  676.         push    esi
  677.         mov     esi, oldcontext
  678.         mov     edi, context
  679.         mov     ecx, 28h/4
  680.         rep movsd
  681.         call    DoResume
  682.         jmp     dbgmsgend
  683.  
  684.     .notint1:
  685.     ; in other case, work as without temp_break
  686.         lodsd
  687.         push    esi
  688.         push    eax
  689.         jmp     exception.4
  690.  
  691.     .notour:
  692.  
  693. ; TODO: split it out
  694. debugmsg:
  695.         neg     [dbgbufsize]
  696.         mov     esi, dbgbuf
  697.  
  698. ; TODO: make it local
  699. dbgmsgstart:
  700.         lodsd
  701. ;       push    eax esi
  702. ;       push    dword [esi]
  703. ;       mov     esi, dbgmsg_str
  704. ;       call    put_message_nodraw
  705. ;       pop     esi eax
  706.         add     esi, 4
  707.         dec     eax
  708.         jz      exception
  709.         dec     eax
  710.         jz      terminated
  711.         dec     eax
  712.         jnz     dbg_notify
  713.  
  714.         mov     [bSuspended], 1
  715.         cmp     [bAfterGo], 0
  716.         jnz     after_go_exception
  717.         push    esi
  718.         call    get_new_context
  719.         and     [_eflags], not 10100h           ; clear TF,RF
  720.         call    set_context
  721.         pop     esi
  722.  
  723.     ; TODO: WTF? Need for meaning label names
  724.     .5:
  725.         push    esi
  726.         call    get_dump
  727.         pop     esi
  728.         lodsd
  729.         xor     ecx, ecx
  730.  
  731.     .6:
  732.         bt      eax, ecx
  733.         jnc     .7
  734.         mov     ebx, [drx_break+ecx*4]
  735.         test    ebx, ebx
  736.         jz      .7
  737.         pushad
  738.         dec     ebx
  739.         push    ebx
  740.         mov     esi, aBreakStop
  741.         call    put_message_nodraw
  742.         popad
  743.  
  744.     .7:
  745.         inc     ecx
  746.         cmp     cl, 4
  747.         jb      .6
  748.         push    esi
  749.         jmp     exception.done_draw
  750.  
  751. dbg_notify:
  752.         int3
  753.         add     esi, 32
  754.         push    esi
  755.         jmp     dbgmsgend
  756.  
  757.  
  758. ; TODO: make it local
  759. terminated:
  760.         push    esi
  761.         mov     esi, terminated_msg
  762.         call    put_message
  763.         and     [debuggee_pid], 0
  764.         and     [temp_break], 0
  765.         mov     [bAfterGo], 0
  766.         xor     eax, eax
  767.         mov     ecx, breakpoints_n*6/4+4
  768.         mov     edi, breakpoints
  769.         rep stosd
  770.         cmp     [bReload], 1
  771.         sbb     [bReload], -1
  772.         jnz     exception.done
  773.         call    free_symbols
  774.         jmp     exception.done
  775.  
  776. exception:
  777.         mov     [bSuspended], 1
  778.         cmp     [bAfterGo], 0
  779.         jnz     after_go_exception
  780.         lodsd
  781.         push    esi
  782.         push    eax
  783.         call    get_new_context
  784.         and     [_eflags], not 10100h           ; clear TF,RF
  785.         call    set_context
  786.  
  787.     ; TODO: fix for useful name
  788.     .4:
  789.         call    get_dump
  790.         pop     eax
  791.     ; int3 command generates exception 0D, #GP
  792.         push    eax
  793.         cmp     al, 0Dh
  794.         jnz     .notdbg
  795.     ; check for 0xCC byte at eip
  796.         push    0
  797.         mcall   69, 6, [debuggee_pid], 1, [_eip], esp
  798.         pop     eax
  799.         cmp     al, 0xCC
  800.         je      .int3
  801.     ; check for 0xCD03 word at eip
  802.         push    0
  803.         inc     edx
  804.         mcall   69;, 6, [debuggee_pid], 2, [_eip], esp
  805.         pop     eax
  806.         cmp     ax, 0x03CD
  807.         jne     .notdbg
  808.         mov     eax, [_eip]
  809.         inc     [_eip]
  810.         inc     [_eip]
  811.         jmp     .user_int3_
  812.     .int3:
  813.     ; this is either dbg breakpoint or int3 cmd in debuggee
  814.         mov     eax, [_eip]
  815.         call    find_enabled_breakpoint
  816.         jnz     .user_int3
  817.     ; dbg breakpoint; clear if one-shot
  818.         pop     ecx
  819.         push    eax
  820.         mov     esi, aBreakStop
  821.         test    byte [edi+4], 4
  822.         jz      .put_msg_eax
  823.         pop     ecx
  824.         call    clear_breakpoint
  825.         jmp     .done
  826.  
  827.     .user_int3:
  828.         mov     eax, [_eip]
  829.         inc     [_eip]
  830.     .user_int3_:
  831.         pop     ecx
  832.         push    eax
  833.         call    set_context
  834.         mov     esi, aUserBreak
  835.         jmp     .put_msg_eax
  836.  
  837.     .notdbg:
  838.         pop     eax
  839.         push    eax
  840.         push    eax
  841.         mov     esi, aException
  842.         call    put_message_nodraw
  843.         pop     eax
  844.  
  845.         cmp     al, 16
  846.         ja      .suspended
  847.         mov     esi, [msg_fault_sel + eax*4]
  848.         call    put_message_nodraw
  849.  
  850.     .suspended:
  851.         mov     esi, aSuspended
  852.  
  853.     .put_msg_eax:
  854.         call    put_message_nodraw
  855.  
  856.     .done_draw:
  857.         call    draw_messages
  858.  
  859.     .done:
  860.         mcall   18, 3, [dbgwnd]    ; activate dbg window
  861.         call    redraw_title
  862.         call    draw_registers.redraw
  863.         call    draw_dump.redraw
  864.         call    update_disasm_eip
  865.  
  866. dbgmsgend:
  867.         pop     esi
  868.         mov     ecx, [dbgbuflen]
  869.         add     ecx, dbgbuf
  870.         cmp     esi, ecx
  871.         jnz     dbgmsgstart
  872.         and     [dbgbuflen], 0
  873.         neg     [dbgbufsize]
  874.         cmp     [bReload], 2
  875.         jnz     @f
  876.         mov     [bReload], 0
  877.         call    do_reload
  878.  
  879.     @@:
  880.         jmp     waitevent
  881.  
  882. ; TODO: make it local
  883. F7:
  884.         cmp     [debuggee_pid], 0
  885.         jz      .no
  886.         call    OnStep
  887.  
  888.     .no:
  889.         jmp     waitevent
  890.  
  891. ; TODO: make it local
  892. F8:
  893.         cmp     [debuggee_pid], 0
  894.         jz      F7.no
  895.         call    OnProceed
  896.         jmp     F7.no
  897.  
  898. ;-----------------------------------------------------------------------------
  899. ;                       Step execution event
  900.  
  901. ;Here we get [<number>] argument at do step <number> times
  902. OnStepMultiple:
  903.         cmp     [bSuspended], 0
  904.         jz      OnStep.running
  905.         mov     [step_num], 1
  906.         mov     esi, [curarg]
  907.         test    esi, esi
  908.         jz      .do
  909.         cmp     byte [esi], 0
  910.         jz      .do
  911.         call    get_hex_number
  912.         jc      .ret
  913.         cmp     eax, 0 ; check if lesser or equal than 0
  914.         jle     .ret
  915.         mov     [step_num], eax
  916. .do:
  917.         call    OnStep
  918.         dec     [step_num]
  919.         jnz     .do
  920. .ret:
  921.         ret
  922.  
  923. OnStep:
  924.         cmp     [bSuspended], 0
  925.         jz      .running
  926.         call    get_context
  927.         or      byte [_eflags+1], 1             ; set TF
  928.         call    set_context
  929.         and     byte [_eflags+1], not 1
  930.     ; if instruction at eip is "int xx", set one-shot breakpoint immediately after
  931.         mov     eax, [_eip]
  932.         call    find_enabled_breakpoint
  933.         jnz     @f
  934.         cmp     byte [edi+5], 0xCD
  935.         jz      .int
  936.  
  937.      @@:
  938.         push    0
  939.         ;push    69
  940.         ;pop     eax
  941.         ;push    6
  942.         ;pop     ebx
  943.         ;mov     ecx, [debuggee_pid]
  944.         ;push    3
  945.         ;pop     edx
  946.         ;mov     edi, esp
  947.         ;mov     esi, [_eip]
  948.         mcall    69, 6, [debuggee_pid], 3, [_eip], esp
  949.         cmp     eax, edx
  950.         pop     eax
  951.         jnz     .doit
  952.         cmp     al, 0xCD
  953.         jz      .int
  954.         cmp     ax, 0x050F
  955.         jz      .syscall
  956.         cmp     ax, 0x340F
  957.         jz      .sysenter
  958.  
  959.     ; resume process
  960.     .doit:
  961.         call    GoOn
  962.         cmp     [bAfterGo], 0
  963.         jz      @f
  964.         mov     [bAfterGo], 2
  965.  
  966.     @@:
  967.         ret
  968.  
  969.     ; return address is [ebp-4]
  970.     .sysenter:
  971.         push    0
  972.         ;push    69
  973.         ;pop     eax
  974.         inc     edx     ; read 4 bytes
  975.         mov     esi, [_ebp]
  976.         sub     esi, 4
  977.         mcall    69
  978.         cmp     eax, edx
  979.         pop     eax
  980.         jnz     .syscall
  981.         push    eax
  982.         and     byte [_eflags+1], not 1
  983.         call    set_context
  984.         pop     eax
  985.         jmp     @f
  986.  
  987.     .syscall:
  988.         and     byte [_eflags+1], not 1 ; clear TF - avoid system halt (!)
  989.         call    set_context
  990.  
  991.     .int:
  992.         mov     eax, [_eip]
  993.         inc     eax
  994.         inc     eax
  995.  
  996.     @@:
  997.         push    eax
  998.         call    find_enabled_breakpoint
  999.         pop     eax
  1000.         jz      .doit
  1001.     ; there is no enabled breakpoint yet; set temporary breakpoint
  1002.         mov     bl, 5
  1003.         call    add_breakpoint
  1004.         jmp     .doit
  1005.  
  1006.     .running:
  1007.         mov     esi, aRunningErr
  1008.         jmp     put_message
  1009.  
  1010. ;-----------------------------------------------------------------------------
  1011. ;                       Proceed process event
  1012. ;Here we get [<number>] argument at do step <number> times
  1013. OnProceedMultiple:
  1014.         cmp     [bSuspended], 0
  1015.         jz      OnStep.running
  1016.         mov     [proc_num], 1
  1017.         mov     esi, [curarg]
  1018.         test    esi, esi
  1019.         jz      .do
  1020.         cmp     byte [esi], 0
  1021.         jz      .do
  1022.         call    get_hex_number
  1023.         jc      .ret
  1024.         cmp     eax, 0 ; check if lesser or equal than 0
  1025.         jle     .ret
  1026.         mov     [proc_num], eax
  1027.         mov     [curarg], 0
  1028. .do:
  1029.         call    OnProceed
  1030.         dec     [proc_num]
  1031.         jnz     .do
  1032. .ret:
  1033.         ret
  1034.  
  1035.  
  1036. OnProceed:
  1037.         cmp     [bSuspended], 0
  1038.         jz      OnStep.running
  1039.         mov     esi, [_eip]
  1040.  
  1041.     @@:
  1042.         call    get_byte_nobreak
  1043.         jc      OnStep
  1044.         inc     esi
  1045.     ; skip prefixes
  1046.         call    is_prefix
  1047.         jz      @b
  1048.         cmp     al, 0xE8        ; call
  1049.         jnz     @f
  1050.         add     esi, 4
  1051.         jmp     .doit
  1052.  
  1053.     ; A4,A5 = movs; A6,A7 = cmps
  1054.     @@:
  1055.         cmp     al, 0xA4
  1056.         jb      @f
  1057.         cmp     al, 0xA8
  1058.         jb      .doit
  1059.  
  1060.     ; AA,AB = stos; AC,AD = lods; AE,AF = scas
  1061.     @@:
  1062.         cmp     al, 0xAA
  1063.         jb      @f
  1064.         cmp     al, 0xB0
  1065.         jb      .doit
  1066.  
  1067.     ; E0 = loopnz; E1 = loopz; E2 = loop
  1068.     @@:
  1069.         cmp     al, 0xE0
  1070.         jb      .noloop
  1071.         cmp     al, 0xE2
  1072.         ja      .noloop
  1073.         inc     esi
  1074.         jmp     .doit
  1075.  
  1076.     ; FF /2 = call
  1077.     .noloop:
  1078.         cmp     al, 0xFF
  1079.         jnz     OnStep
  1080.         call    get_byte_nobreak
  1081.         jc      OnStep
  1082.         inc     esi
  1083.         mov     cl, al
  1084.         and     al, 00111000b
  1085.         cmp     al, 00010000b
  1086.         jnz     OnStep
  1087.     ; skip instruction
  1088.         mov     al, cl
  1089.         and     eax, 7
  1090.         shr     cl, 6
  1091.         jz      .mod0
  1092.         jp      .doit
  1093.         cmp     al, 4
  1094.         jnz     @f
  1095.         inc     esi
  1096.  
  1097.     @@:
  1098.         inc     esi
  1099.         dec     cl
  1100.         jz      @f
  1101.         add     esi, 3
  1102.  
  1103.     @@:
  1104.         jmp     .doit
  1105.  
  1106.     .mod0:
  1107.         cmp     al, 4
  1108.         jnz     @f
  1109.         call    get_byte_nobreak
  1110.         jc      OnStep
  1111.         inc     esi
  1112.         and     al, 7
  1113.  
  1114.     @@:
  1115.         cmp     al, 5
  1116.         jnz     .doit
  1117.         add     esi, 4
  1118.  
  1119.     .doit:
  1120.     ; insert one-shot breakpoint at esi and resume
  1121.         call    get_byte_nobreak
  1122.         jc      OnStep
  1123.         mov     eax, esi
  1124.         call    find_enabled_breakpoint
  1125.         jz      @f
  1126.         mov     eax, esi
  1127.         mov     bl, 5
  1128.         call    add_breakpoint
  1129.         jmp     OnStep.doit
  1130.  
  1131.     @@:
  1132.         ret
  1133.  
  1134. ;-----------------------------------------------------------------------------
  1135. ;                        Read next byte of machine code
  1136.  
  1137. get_byte_nobreak:
  1138.         mov     eax, esi
  1139.         call    find_enabled_breakpoint
  1140.         jnz     .nobreak
  1141.         mov     al, [edi+5]
  1142.         clc
  1143.         ret
  1144.  
  1145.     .nobreak:
  1146.         ;push    69
  1147.         ;pop     eax
  1148.         ;push    6
  1149.         ;pop     ebx
  1150.         ;mov     ecx, [debuggee_pid]
  1151.         xor     edx, edx
  1152.         push    edx
  1153.         inc     edx
  1154.         mov     edi, esp
  1155.         mcall    69, 6, [debuggee_pid]
  1156.         dec     eax
  1157.         clc
  1158.         jz      @f
  1159.         stc
  1160.  
  1161.     @@:
  1162.         pop     eax
  1163.         ret
  1164.  
  1165. include 'parser.inc'
  1166.  
  1167. ;-----------------------------------------------------------------------------
  1168. ;                        Calculate expression event
  1169.  
  1170. OnCalc:
  1171.         mov     esi, [curarg]
  1172.         call    calc_expression
  1173.         jc      .ret
  1174.         push    ebp
  1175.         mov     esi, calc_string
  1176.         call    put_message_nodraw
  1177.         jmp     draw_messages
  1178.  
  1179.     .ret:
  1180.         ret
  1181.  
  1182. ;-----------------------------------------------------------------------------
  1183. ;                            Dump memory event
  1184.  
  1185. OnDump:
  1186.         mov     esi, [curarg]
  1187.         cmp     byte [esi], 0
  1188.         jnz     .param
  1189.         add     [dumppos], dump_height*10h
  1190.         jmp     .doit
  1191.  
  1192.     .param:
  1193.         call    calc_expression
  1194.         jc      .ret
  1195.         mov     [dumppos], ebp
  1196.  
  1197.     .doit:
  1198.         call    get_dump
  1199.         call    draw_dump.redraw
  1200.  
  1201.     .ret:
  1202.         ret
  1203.  
  1204. ;-----------------------------------------------------------------------------
  1205. ;                   Dissassemble block of executable event
  1206.  
  1207. OnUnassemble:
  1208.         mov     esi, [curarg]
  1209.         cmp     byte [esi], 0
  1210.         jnz     .param
  1211.         mov     eax, [disasm_start_pos]
  1212.         mov     ecx, [disasm_height_dd]
  1213.         mov     [disasm_cur_pos], eax
  1214.  
  1215.     .l:
  1216.         mov     eax, [disasm_cur_pos]
  1217.         call    find_symbol
  1218.         jc      @f
  1219.         dec     ecx
  1220.         jz      .m
  1221.  
  1222.     @@:
  1223.         push    ecx
  1224.         call    disasm_instr
  1225.         pop     ecx
  1226.         jc      .err
  1227.         loop    .l
  1228.  
  1229.     .m:
  1230.         mov     eax, [disasm_cur_pos]
  1231.         jmp     .doit
  1232.  
  1233.     .param:
  1234.         call    calc_expression
  1235.         jc      .ret
  1236.         mov     eax, ebp
  1237.  
  1238.     .doit:
  1239.         push    eax
  1240.         push    [disasm_start_pos]
  1241.         mov     [disasm_start_pos], eax
  1242.         call    update_disasm
  1243.         pop     [disasm_start_pos]
  1244.         pop     eax
  1245.         cmp     [disasm_cur_str], 0
  1246.         jz      @f
  1247.         mov     [disasm_start_pos], eax
  1248.  
  1249.     .ret:
  1250.         ret
  1251.  
  1252.     @@:
  1253.         call    update_disasm
  1254.  
  1255.     .err:
  1256.         mov     esi, aInvAddr
  1257.         jmp     put_message
  1258.  
  1259. ;-----------------------------------------------------------------------------
  1260. ;                       Access to register value event
  1261.  
  1262. OnReg:
  1263.         mov     esi, [curarg]
  1264.         call    get_arg.skip_spaces
  1265.         call    find_reg
  1266.         jnc     @f
  1267.  
  1268.     .err:
  1269.         mov     esi, RSyntax
  1270.         jmp     put_message
  1271.  
  1272.     @@:
  1273.         call    get_arg.skip_spaces
  1274.         test    al, al
  1275.         jz      .err
  1276.         cmp     al, '='
  1277.         jnz     @f
  1278.         inc     esi
  1279.         call    get_arg.skip_spaces
  1280.         test    al, al
  1281.         jz      .err
  1282.  
  1283.     @@:
  1284.         push    edi
  1285.         call    calc_expression
  1286.         pop     edi
  1287.         jc      .ret
  1288.     ; now edi=register id, ebp=value
  1289.         cmp     [bSuspended], 0
  1290.         mov     esi, aRunningErr
  1291.         jz      put_message
  1292.         xchg    eax, ebp
  1293.         cmp     edi, 24
  1294.         jz      .eip
  1295.         sub     edi, 4
  1296.         jb      .8lo
  1297.         sub     edi, 4
  1298.         jb      .8hi
  1299.         sub     edi, 8
  1300.         jb      .16
  1301.         mov     [_eax+edi*4], eax
  1302.         jmp     .ret
  1303.  
  1304.     .16:
  1305.         mov     word [_eax+(edi+8)*4], ax
  1306.         jmp     .ret
  1307.  
  1308.     .8lo:
  1309.         mov     byte [_eax+(edi+4)*4], al
  1310.         jmp     .ret
  1311.  
  1312.     .8hi:
  1313.         mov     byte [_eax+(edi+4)*4+1], al
  1314.         jmp     .ret
  1315.  
  1316.     .eip:
  1317.         mov     [_eip], eax
  1318.         call    update_disasm_eip
  1319.  
  1320.     .ret:
  1321.         call    set_context
  1322.         jmp     draw_registers.redraw
  1323.  
  1324. ;-----------------------------------------------------------------------------
  1325. ;                        Breakpoints manipulation
  1326. OnBp:
  1327.         mov     esi, [curarg]
  1328.         call    calc_expression
  1329.         jc      .ret
  1330.         xchg    eax, ebp
  1331.         push    eax
  1332.         call    find_breakpoint
  1333.         inc     eax
  1334.         pop     eax
  1335.         jz      .notfound
  1336.         mov     esi, aDuplicateBreakpoint
  1337.         jmp     .sayerr
  1338.  
  1339.     .notfound:
  1340.         mov     bl, 1
  1341.         call    add_breakpoint
  1342.         jnc     .ret
  1343.         mov     esi, aBreakpointLimitExceeded
  1344.  
  1345.     .sayerr:
  1346.         call    put_message
  1347.  
  1348.     .ret:
  1349.         jmp     draw_disasm
  1350.  
  1351. OnBpmb:
  1352.         mov     dh, 0011b
  1353.         jmp     DoBpm
  1354.  
  1355. OnBpmw:
  1356.         mov     dh, 0111b
  1357.         jmp     DoBpm
  1358.  
  1359. OnBpmd:
  1360.         mov     dh, 1111b
  1361.  
  1362. DoBpm:
  1363.         mov     esi, [curarg]
  1364.         cmp     byte [esi], 'w'
  1365.         jnz     @f
  1366.         and     dh, not 2
  1367.         inc     esi
  1368.  
  1369.     @@:
  1370.         push    edx
  1371.         call    calc_expression
  1372.         pop     edx
  1373.         jnc     @f
  1374.         ret
  1375.  
  1376.     ; ebp = expression, dh = flags
  1377.     @@:
  1378.         movzx   eax, dh
  1379.         shr     eax, 2
  1380.         test    ebp, eax
  1381.         jz      @f
  1382.         mov     esi, aUnaligned
  1383.         jmp     put_message
  1384.  
  1385.     @@:
  1386.         mov     eax, ebp
  1387.         mov     bl, 0Bh
  1388.         call    add_breakpoint
  1389.         jnc     @f
  1390.         mov     esi, aBreakpointLimitExceeded
  1391.         jmp     put_message
  1392.  
  1393.     ; now find index
  1394.     @@:
  1395.         push    eax
  1396.         xor     ecx, ecx
  1397.  
  1398.     .l1:
  1399.         cmp     [drx_break+ecx*4], 0
  1400.         jnz     .l2
  1401.         ;push    69
  1402.         ;pop     eax
  1403.         push    ecx
  1404.         mov     dl, cl
  1405.         ;mov     ecx, [debuggee_pid]
  1406.         mov     esi, ebp
  1407.         ;push    9
  1408.         ;pop     ebx
  1409.         mcall    69, 9, [debuggee_pid]
  1410.         test    eax, eax
  1411.         jz      .ok
  1412.         pop     ecx
  1413.  
  1414.     .l2:
  1415.         inc     ecx
  1416.         cmp     ecx, 4
  1417.         jb      .l1
  1418.         pop     eax
  1419.         call    clear_breakpoint
  1420.         mov     esi, aBreakpointLimitExceeded
  1421.         jmp     put_message
  1422.  
  1423.     .ok:
  1424.         pop     ecx
  1425.         pop     eax
  1426.         and     byte [edi], not 2       ; breakpoint is enabled
  1427.         shl     dl, 6
  1428.         or      dl, dh
  1429.         mov     byte [edi+1], dl
  1430.         inc     eax
  1431.         mov     [drx_break+ecx*4], eax
  1432.         ret
  1433.  
  1434. OnBc:
  1435.         mov     esi, [curarg]
  1436.  
  1437.     @@:
  1438.         call    get_hex_number
  1439.         jc      OnBp.ret
  1440.         call    clear_breakpoint
  1441.         jmp     @b
  1442.  
  1443. OnBd:
  1444.         mov     esi, [curarg]
  1445.  
  1446.     @@:
  1447.         call    get_hex_number
  1448.         jc      OnBp.ret
  1449.         call    disable_breakpoint
  1450.         jmp     @b
  1451.  
  1452. OnBe:
  1453.         mov     esi, [curarg]
  1454.  
  1455.     @@:
  1456.         call    get_hex_number
  1457.         jc      OnBp.ret
  1458.         push    eax
  1459.         call    find_enabled_breakpoint
  1460.         pop     eax
  1461.         jz      .err
  1462.         call    enable_breakpoint
  1463.         jmp     @b
  1464.  
  1465.     .err:
  1466.         mov     esi, OnBeErrMsg
  1467.         jmp     put_message
  1468.  
  1469. ; TODO: split it out in parser.inc
  1470. get_hex_number:
  1471.         call    get_arg.skip_spaces
  1472.         xor     ecx, ecx
  1473.         xor     edx, edx
  1474.  
  1475.     @@:
  1476.         lodsb
  1477.         call    is_hex_digit
  1478.         jc      .ret
  1479.         shl     edx, 4
  1480.         or      dl, al
  1481.         inc     ecx
  1482.         jmp     @b
  1483.  
  1484.     .ret:
  1485.         dec     esi
  1486.         cmp     ecx, 1
  1487.         xchg    eax, edx
  1488.         ret
  1489.  
  1490. ;-----------------------------------------------------------------------------
  1491. ;                       Breakpoints list event
  1492.  
  1493. OnBl:
  1494.         mov     esi, [curarg]
  1495.         cmp     byte [esi], 0
  1496.         jz      .listall
  1497.         call    get_hex_number
  1498.         jc      .ret
  1499.         cmp     eax, breakpoints_n
  1500.         jae     .err
  1501.         push    eax
  1502.         add     eax, eax
  1503.         lea     edi, [breakpoints + eax + eax*2]
  1504.         pop     eax
  1505.         test    byte [edi+4], 1
  1506.         jz      .err
  1507.         call    show_break_info
  1508.  
  1509.     .ret:
  1510.         ret
  1511.  
  1512.     .err:
  1513.         mov     esi, aInvalidBreak
  1514.         jmp     put_message
  1515.  
  1516.     .listall:
  1517.         mov     edi, breakpoints
  1518.         xor     eax, eax
  1519.  
  1520.     @@:
  1521.         test    byte [edi+4], 1
  1522.         jz      .cont
  1523.         push    edi eax
  1524.         call    show_break_info
  1525.         pop     eax edi
  1526.  
  1527.     .cont:
  1528.         add     edi, 6
  1529.         inc     eax
  1530.         cmp     eax, breakpoints_n
  1531.         jb      @b
  1532.         ret
  1533.  
  1534. ;-----------------------------------------------------------------------------
  1535.  
  1536. show_break_info:
  1537.         push    edi
  1538.         test    byte [edi+4], 8
  1539.         jnz     .dr
  1540.         push    dword [edi]
  1541.         push    eax
  1542.         mov     esi, aBreakNum
  1543.         call    put_message_nodraw
  1544.         jmp     .cmn
  1545.  
  1546.     .dr:
  1547.         push    eax
  1548.         mov     esi, aMemBreak1
  1549.         call    put_message_nodraw
  1550.         pop     edi
  1551.         push    edi
  1552.         mov     esi, aMemBreak2
  1553.         test    byte [edi+5], 2
  1554.         jz      @f
  1555.         mov     esi, aMemBreak3
  1556.  
  1557.     @@:
  1558.         call    put_message_nodraw
  1559.         pop     edi
  1560.         push    edi
  1561.         mov     esi, aMemBreak6
  1562.         test    byte [edi+5], 8
  1563.         jnz     @f
  1564.         mov     esi, aMemBreak5
  1565.         test    byte [edi+5], 4
  1566.         jnz     @f
  1567.         mov     esi, aMemBreak4
  1568.  
  1569.     @@:
  1570.         call    put_message_nodraw
  1571.         pop     edi
  1572.         push    edi
  1573.         push    dword [edi]
  1574.         mov     esi, aMemBreak7
  1575.         call    put_message_nodraw
  1576.  
  1577.     .cmn:
  1578.         pop     edi
  1579.         test    byte [edi+4], 2
  1580.         jz      @f
  1581.         push    edi
  1582.         mov     esi, aDisabled
  1583.         call    put_message_nodraw
  1584.         pop     edi
  1585.  
  1586.     @@:
  1587.         test    byte [edi+4], 4
  1588.         jz      @f
  1589.         mov     esi, aOneShot
  1590.         call    put_message_nodraw
  1591.  
  1592.     @@:
  1593.         mov     esi, newline
  1594.         jmp     put_message
  1595.  
  1596. ;-----------------------------------------------------------------------------
  1597. ;                        Add breakpoint
  1598. ; in: EAX = address; BL = flags
  1599. ; out: CF = 1 => error
  1600. ;      CF = 0 and EAX = breakpoint number
  1601.  
  1602. add_breakpoint:
  1603.         xor     ecx, ecx
  1604.         mov     edi, breakpoints
  1605.  
  1606.     @@:
  1607.         test    byte [edi+4], 1
  1608.         jz      .found
  1609.         add     edi, 6
  1610.         inc     ecx
  1611.         cmp     ecx, breakpoints_n
  1612.         jb      @b
  1613.         stc
  1614.         ret
  1615.  
  1616.     .found:
  1617.         stosd
  1618.         xchg    eax, ecx
  1619.         mov     [edi], bl
  1620.         test    bl, 2
  1621.         jnz     @f
  1622.         or      byte [edi], 2
  1623.         push    eax
  1624.         call    enable_breakpoint
  1625.         pop     eax
  1626.  
  1627.     @@:
  1628.         clc
  1629.         ret
  1630.  
  1631. ;-----------------------------------------------------------------------------
  1632. ;                         Remove breakpoint
  1633.  
  1634. clear_breakpoint:
  1635.         cmp     eax, breakpoints_n
  1636.         jae     .ret
  1637.         mov     ecx, 4
  1638.         inc     eax
  1639.  
  1640.     .1:
  1641.         cmp     [drx_break-4+ecx*4], eax
  1642.         jnz     @f
  1643.         and     [drx_break-4+ecx*4], 0
  1644.  
  1645.     @@:
  1646.         loop    .1
  1647.         dec     eax
  1648.         push    eax
  1649.         add     eax, eax
  1650.         lea     edi, [breakpoints + eax + eax*2 + 4]
  1651.         test    byte [edi], 1
  1652.         pop     eax
  1653.         jz      .ret
  1654.         push    edi
  1655.         call    disable_breakpoint
  1656.         pop     edi
  1657.         mov     byte [edi], 0
  1658.  
  1659.     .ret:
  1660.         ret
  1661.  
  1662. ;-----------------------------------------------------------------------------
  1663. ;                          Disable breakpoint
  1664.  
  1665. disable_breakpoint:
  1666.         cmp     eax, breakpoints_n
  1667.         jae     .ret
  1668.         add     eax, eax
  1669.         lea     edi, [breakpoints + eax + eax*2 + 5]
  1670.         test    byte [edi-1], 1
  1671.         jz      .ret
  1672.         test    byte [edi-1], 2
  1673.         jnz     .ret
  1674.         or      byte [edi-1], 2
  1675.         test    byte [edi-1], 8
  1676.         jnz     .dr
  1677.         push    esi
  1678.         ;push    7
  1679.         ;pop     ebx
  1680.         ;push    69
  1681.         ;pop     eax
  1682.         ;mov     ecx, [debuggee_pid]
  1683.         ;xor     edx, edx
  1684.         ;inc     edx
  1685.         ;mov     esi, [edi-5]
  1686.         mcall    69, 7, [debuggee_pid], 1, [edi-5]
  1687.         pop     esi
  1688.  
  1689.     .ret:
  1690.         ret
  1691.  
  1692.     .dr:
  1693.         mov     dl, [edi]
  1694.         shr     dl, 6
  1695.         mov     dh, 80h
  1696.         ;push    69
  1697.         ;pop     eax
  1698.         ;push    9
  1699.         ;pop     ebx
  1700.         ;mov     ecx, [debuggee_pid]
  1701.         mcall    69, 9, [debuggee_pid]
  1702.         ret
  1703.  
  1704. ;-----------------------------------------------------------------------------
  1705. ;                           Enable breakpoint
  1706.  
  1707. enable_breakpoint:
  1708.         push    esi
  1709.         cmp     eax, breakpoints_n
  1710.         jae     .ret
  1711.         add     eax, eax
  1712.         lea     edi, [breakpoints + eax + eax*2 + 5]
  1713.         test    byte [edi-1], 1
  1714.         jz      .ret
  1715.         test    byte [edi-1], 2
  1716.         jz      .ret
  1717.         and     byte [edi-1], not 2
  1718.         test    byte [edi-1], 8
  1719.         jnz     .dr
  1720.         ;push    6
  1721.         ;pop     ebx
  1722.         ;push    69
  1723.         ;pop     eax
  1724.         ;mov     esi, [edi-5]
  1725.         ;mov     ecx, [debuggee_pid]
  1726.         ;xor     edx, edx
  1727.         ;inc     edx
  1728.         mcall    69, 6, [debuggee_pid], 1, [edi-5]
  1729.         dec     eax
  1730.         jnz     .err
  1731.         ;mov     al, 69
  1732.         push    0xCC
  1733.         mov     edi, esp
  1734.         inc     ebx
  1735.         mcall    69
  1736.         pop     eax
  1737.  
  1738.     .ret:
  1739.         pop     esi
  1740.         ret
  1741.  
  1742.     .err:
  1743.         or      byte [edi-1], 2
  1744.         mov     esi, aBreakErr
  1745.         call    put_message
  1746.         pop     esi
  1747.         ret
  1748.  
  1749.     .dr:
  1750.         ;push    9
  1751.         ;pop     ebx
  1752.         ;push    69
  1753.         ;pop     eax
  1754.         mov     esi, [edi-5]
  1755.         ;mov     ecx, [debuggee_pid]
  1756.         mov     dl, [edi]
  1757.         shr     dl, 6
  1758.         mov     dh, [edi]
  1759.         and     dh, 0xF
  1760.         mcall    69, 9, [debuggee_pid]
  1761.         test    eax, eax
  1762.         jnz     .err
  1763.         pop     esi
  1764.         ret
  1765.  
  1766. ;-----------------------------------------------------------------------------
  1767. ;                             Find breakpoint
  1768.  
  1769. find_breakpoint:
  1770.         xor     ecx, ecx
  1771.         xchg    eax, ecx
  1772.         mov     edi, breakpoints
  1773.  
  1774.     @@:
  1775.         test    byte [edi+4], 1
  1776.         jz      .cont
  1777.         test    byte [edi+4], 8
  1778.         jnz     .cont
  1779.         cmp     [edi], ecx
  1780.         jz      .found
  1781.  
  1782.     .cont:
  1783.         add     edi, 6
  1784.         inc     eax
  1785.         cmp     eax, breakpoints_n
  1786.         jb      @b
  1787.         or      eax, -1
  1788.  
  1789.     .found:
  1790.         ret
  1791.  
  1792. ;-----------------------------------------------------------------------------
  1793. ;
  1794.  
  1795. find_enabled_breakpoint:
  1796.         xor     ecx, ecx
  1797.         xchg    eax, ecx
  1798.         mov     edi, breakpoints
  1799.  
  1800.     @@:
  1801.         test    byte [edi+4], 1
  1802.         jz      .cont
  1803.         test    byte [edi+4], 2 or 8
  1804.         jnz     .cont
  1805.         cmp     [edi], ecx
  1806.         jz      .found
  1807.  
  1808.     .cont:
  1809.         add     edi, 6
  1810.         inc     eax
  1811.         cmp     eax, breakpoints_n
  1812.         jb      @b
  1813.         or      eax, -1
  1814.  
  1815.     .found:
  1816.         ret
  1817.  
  1818. ; TODO: add find_disabled_breakpoint
  1819.  
  1820. ;-----------------------------------------------------------------------------
  1821. ;                       Unpack executable event
  1822.  
  1823. OnUnpack:
  1824.     ; program must be loaded - checked when command was parsed
  1825.     ; program must be stopped
  1826.         mov     esi, aRunningErr
  1827.         cmp     [bSuspended], 0
  1828.         jz      put_message
  1829.    ; all breakpoints must be disabled
  1830.         mov     edi, breakpoints
  1831.  
  1832.     @@:
  1833.         test    byte [edi+4], 1
  1834.         jz      .cont
  1835.         test    byte [edi+4], 2
  1836.         jnz     .cont
  1837.         mov     esi, aEnabledBreakErr
  1838.         jmp     put_message
  1839.  
  1840.     .cont:
  1841.         add     edi, 6
  1842.         cmp     edi, breakpoints+breakpoints_n*6
  1843.         jb      @b
  1844.     ; ok, now do it
  1845.     ; set breakpoint on 0xC dword access
  1846.         push    9
  1847.         pop     ebx
  1848.         mov     ecx, [debuggee_pid]
  1849.         mov     dx, 1111b*256
  1850.         push    0xC
  1851.         pop     esi
  1852.  
  1853.     @@:
  1854.         ;push    69
  1855.         ;pop     eax
  1856.         mcall    69
  1857.         test    eax, eax
  1858.         jz      .breakok
  1859.         inc     edx
  1860.         cmp     dl, 4
  1861.         jb      @b
  1862.  
  1863.     .breakok:
  1864.         call    GoOn
  1865.  
  1866.     ; now wait for event
  1867.     .wait:
  1868.         ;push    10
  1869.         ;pop     eax
  1870.         mcall    10
  1871.         dec     eax
  1872.         jz      .redraw
  1873.         dec     eax
  1874.         jz      .key
  1875.         dec     eax
  1876.         jnz     .debug
  1877.     ; button; we have only one button, close
  1878.         or      eax, -1
  1879.         mcall
  1880.  
  1881.     .redraw:
  1882.         call    draw_window
  1883.         jmp     .wait
  1884.  
  1885.     .key:
  1886.         mov     al, 2
  1887.         mcall
  1888.         cmp     ah, 3   ; Ctrl+C
  1889.         jnz     .wait
  1890.  
  1891.     .userbreak:
  1892.         mov     esi, aInterrupted
  1893.  
  1894.     .x1:
  1895.         push    edx esi
  1896.         call    put_message
  1897.         pop     esi edx
  1898.         or      dh, 80h
  1899.         ;push    69
  1900.         ;pop     eax
  1901.         ;push    9
  1902.         ;pop     ebx
  1903.         ;mov     ecx, [debuggee_pid]
  1904.         mcall    69, 9, [debuggee_pid]
  1905.         cmp     esi, aUnpacked
  1906.         jnz     OnSuspend
  1907.         jmp     AfterSuspend
  1908.  
  1909.     .debug:
  1910.         cmp     [dbgbuflen], 4*3
  1911.         jnz     .notour
  1912.         cmp     dword [dbgbuf], 3
  1913.         jnz     .notour
  1914.         test    byte [dbgbuf+8], 1
  1915.         jnz     .our
  1916.  
  1917.     .notour:
  1918.         mov     esi, aInterrupted
  1919.         push    edx
  1920.         call    put_message
  1921.         pop     edx
  1922.         or      dh, 80h
  1923.         ;push    69
  1924.         ;pop     eax
  1925.         ;push    9
  1926.         ;pop     ebx
  1927.         ;mov     ecx, [debuggee_pid]
  1928.         mcall    69, 9, [debuggee_pid]
  1929.         jmp     debugmsg
  1930.  
  1931.     .our:
  1932.         and     [dbgbuflen], 0
  1933.         push    edx
  1934.         call    get_context
  1935.         push    eax
  1936.         ;mov     al, 69
  1937.         ;mov     bl, 6
  1938.         ;mov     ecx, [debuggee_pid]
  1939.         ;mov     edi, esp
  1940.         ;push    4
  1941.         ;pop     edx
  1942.         ;push    0xC
  1943.         ;pop     esi
  1944.         mcall    69, 6, [debuggee_pid], 4, 0xC, esp
  1945.         pop     eax
  1946.         pop     edx
  1947.         cmp     eax, [_eip]
  1948.         jz      .done
  1949.         call    DoResume
  1950.         jmp     .wait
  1951.  
  1952.     .done:
  1953.         mov     esi, aUnpacked
  1954.         jmp     .x1
  1955.  
  1956. ;-----------------------------------------------------------------------------
  1957. ;                            Include Symbol parser
  1958.  
  1959. include 'symbols.inc'
  1960.  
  1961. ;-----------------------------------------------------------------------------
  1962. ;                        Include disassembler engine
  1963.  
  1964. include 'disasm.inc'
  1965.  
  1966. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1967. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1968. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1969.  
  1970. caption_str db  'Kolibri Debugger',0
  1971.  
  1972. begin_str db    'Kolibri Debugger, version 0.35',10
  1973.         db      'Hint: type "help" for help, "quit" to quit'
  1974. newline db      10,0
  1975. prompt  db      '> ',0
  1976.  
  1977. help_groups:
  1978.         dd      aControl, 0, 0, help_control_msg
  1979.         db      0
  1980.         dd      aData, 0, 0, help_data_msg
  1981.         db      0
  1982.         dd      aBreakpoints, 0, 0, help_breaks_msg
  1983.         db      0
  1984.  
  1985. ;-----------------------------------------------------------------------------
  1986. ;                   Commands format definitions
  1987.  
  1988. ; TODO: make it with macros
  1989.  
  1990. ; flags field:
  1991. ; &1: command may be called without parameters
  1992. ; &2: command may be called with parameters
  1993. ; &4: command may be called without loaded program
  1994. ; &8: command may be called with loaded program
  1995. commands:
  1996.         dd      _aH, OnHelp, HelpSyntax, HelpHelp
  1997.         db      0Fh
  1998.         dd      aHelp, OnHelp, HelpSyntax, HelpHelp
  1999.         db      0Fh
  2000.         dd      aQuit, OnQuit, QuitSyntax, QuitHelp
  2001.         db      0Dh
  2002.         dd      aLoad, OnLoad, LoadSyntax, LoadHelp
  2003.         db      6
  2004.         dd      aReload, OnReload, ReloadSyntax, ReloadHelp
  2005.         db      0Dh
  2006.         dd      aTerminate, OnTerminate, TerminateSyntax, TerminateHelp
  2007.         db      9
  2008.         dd      aDetach, OnDetach, DetachSyntax, DetachHelp
  2009.         db      9
  2010.         dd      aSuspend, OnSuspend, SuspendSyntax, SuspendHelp
  2011.         db      9
  2012.         dd      aResume, OnResume, ResumeSyntax, ResumeHelp
  2013.         db      0Bh
  2014.         dd      aStep, OnStepMultiple, StepSyntax, StepHelp
  2015.         db      0Bh
  2016.         dd      aProceed, OnProceedMultiple, ProceedSyntax, ProceedHelp
  2017.         db      0Bh
  2018.         dd      aCalc, OnCalc, CalcSyntax, CalcHelp
  2019.         db      0Eh
  2020.         dd      aDump, OnDump, DumpSyntax, DumpHelp
  2021.         db      0Bh
  2022.         dd      aUnassemble, OnUnassemble, UnassembleSyntax, UnassembleHelp
  2023.         db      0Bh
  2024.         dd      aBp, OnBp, BpSyntax, BpHelp
  2025.         db      0Ah
  2026.         dd      aBpm, OnBpmb, BpmSyntax, BpmHelp
  2027.         db      0Ah
  2028.         dd      aBpmb, OnBpmb, BpmSyntax, BpmHelp
  2029.         db      0Ah
  2030.         dd      aBpmw, OnBpmw, BpmSyntax, BpmHelp
  2031.         db      0Ah
  2032.         dd      aBpmd, OnBpmd, BpmSyntax, BpmHelp
  2033.         db      0Ah
  2034.         dd      aBl, OnBl, BlSyntax, BlHelp
  2035.         db      0Bh
  2036.         dd      aBc, OnBc, BcSyntax, BcHelp
  2037.         db      0Ah
  2038.         dd      aBd, OnBd, BdSyntax, BdHelp
  2039.         db      0Ah
  2040.         dd      aBe, OnBe, BeSyntax, BeHelp
  2041.         db      0Ah
  2042.         dd      aReg, OnReg, RSyntax, RHelp
  2043.         db      0Ah
  2044.         dd      aUnpack, OnUnpack, UnpackSyntax, UnpackHelp
  2045.         db      9
  2046.         dd      aLoadSymbols, OnLoadSymbols, LoadSymbolsSyntax, LoadSymbolsHelp
  2047.         db      0Ah
  2048.         dd      0
  2049.  
  2050. ;-----------------------------------------------------------------------------
  2051. ;                   Help messages for commands groups
  2052.  
  2053. aHelp   db      5,'help',0
  2054. _aH     db      2,'h',0
  2055. HelpHelp db     'Help on specified function',10
  2056. HelpSyntax db   'Usage: h or help [group | command]',10,0
  2057.  
  2058. help_msg db     'List of known command groups:',10
  2059.         db      '"help control"     - display list of control commands',10
  2060.         db      '"help data"        - display list of commands concerning data',10
  2061.         db      '"help breakpoints" - display list of commands concerning breakpoints',10,0
  2062.  
  2063. ;               Control commands group
  2064.  
  2065. aControl db     8,'control',0
  2066. help_control_msg db     'List of control commands:',10
  2067.         db      'h = help             - help',10
  2068.         db      'quit                 - exit from debugger',10
  2069.         db      'load <name> [params] - load program for debugging',10
  2070.         db      'reload               - reload debugging program',10
  2071.         db      'load-symbols <name>  - load information on symbols for program',10
  2072.         db      'terminate            - terminate loaded program',10
  2073.         db      'detach               - detach from debugging program',10
  2074.         db      'stop                 - suspend execution of debugging program',10
  2075.         db      'g [<expression>]     - go on (resume execution of debugging program)',10
  2076.         db      's [<num>]            - program step, also <F7>',10
  2077.         db      'p [<num>]            - program wide step, also <F8>',10
  2078.         db      'unpack               - try to bypass unpacker code (heuristic)',10,0
  2079.  
  2080. ;               Data commands group
  2081.  
  2082. aData   db      5,'data',0
  2083. help_data_msg db        'List of data commands:',10
  2084.         db      '? <expression>       - calculate value of expression',10
  2085.         db      'd [<expression>]     - dump data at given address',10
  2086.         db      'u [<expression>]     - unassemble instructions at given address',10
  2087.         db      'r <register> <expression> or',10
  2088.         db      'r <register>=<expression> - set register value',10,0
  2089.  
  2090. ;               Breakpoints commands group
  2091.  
  2092. aBreakpoints db 12,'breakpoints',0
  2093. help_breaks_msg db      'List of breakpoints commands:',10
  2094.         db      'bp <expression>      - set breakpoint on execution',10
  2095.         db      'bpm[b|w|d] <type> <expression> - set breakpoint on memory access',10
  2096.         db      'bl [<number>]        - breakpoint(s) info',10
  2097.         db      'bc <number>...       - clear breakpoint',10
  2098.         db      'bd <number>...       - disable breakpoint',10
  2099.         db      'be <number>...       - enable breakpoint',10,0
  2100.  
  2101. ;-----------------------------------------------------------------------------
  2102. ;                    Individual command help messages
  2103.  
  2104. aQuit   db      5,'quit',0
  2105. QuitHelp db     'Quit from debugger',10
  2106. QuitSyntax db   'Usage: quit',10,0
  2107.  
  2108. aLoad   db      5,'load',0
  2109. LoadHelp db     'Load program for debugging',10
  2110. LoadSyntax db   'Usage: load <program-name> [parameters]',10,0
  2111.  
  2112. aReload db      7,'reload',0
  2113. ReloadHelp db   'Reload debugging program (restart debug session)',10
  2114. ReloadSyntax db 'Usage: reload',10,0
  2115.  
  2116. aTerminate db   10,'terminate',0
  2117. TerminateHelp db 'Terminate debugged program',10
  2118. TerminateSyntax db 'Usage: terminate',10,0
  2119.  
  2120. aDetach db      7,'detach',0
  2121. DetachHelp db   'Detach from debugged program',10
  2122. DetachSyntax db 'Usage: detach',10,0
  2123.  
  2124. aSuspend db     5,'stop',0
  2125. SuspendHelp db  'Suspend execution of debugged program',10
  2126. SuspendSyntax db 'Usage: stop',10,0
  2127.  
  2128. aResume db      2,'g',0
  2129. ResumeHelp db   'Go (resume execution of debugged program)',10
  2130. ResumeSyntax db 'Usage: g',10
  2131.         db      '   or: g <expression> - wait until specified address is reached',10,0
  2132.  
  2133. aStep   db      2,'s',0
  2134. StepHelp db     'Make step in debugged program',10
  2135. StepSyntax db   'Usage: s [<number>]',10,0
  2136.  
  2137. aProceed db     2,'p',0
  2138. ProceedHelp db  'Make wide step in debugged program (step over CALL, REPxx, LOOP)',10
  2139. ProceedSyntax db 'Usage: p [<number>]',10,0
  2140.  
  2141. aDump   db      2,'d',0
  2142. DumpHelp db     'Dump data of debugged program',10
  2143. DumpSyntax db   'Usage: d <expression> - dump data at specified address',10
  2144.         db      '   or: d              - continue current dump',10,0
  2145.  
  2146. aCalc   db      2,'?',0
  2147. CalcHelp db     'Calculate value of expression',10
  2148. CalcSyntax db   'Usage: ? <expression>',10,0
  2149.  
  2150. aUnassemble db  2,'u',0
  2151. UnassembleHelp db 'Unassemble',10
  2152. UnassembleSyntax db      'Usage: u <expression> - unassemble instructions at specified address',10
  2153.                  db      '   or: u              - continue current unassemble screen',10,0
  2154.  
  2155. aReg    db      2,'r',0
  2156. RHelp   db      'Set register value',10
  2157. RSyntax db      'Usage: r <register> <expression>',10
  2158.         db      '   or: r <register>=<expression> - set value of <register> to <expression>',10,0
  2159.  
  2160. aBp     db      3,'bp',0
  2161. BpHelp  db      'set BreakPoint on execution',10
  2162. BpSyntax db     'Usage: bp <expression>',10,0
  2163.  
  2164. aBpm    db      4,'bpm',0
  2165. aBpmb   db      5,'bpmb',0
  2166. aBpmw   db      5,'bpmw',0
  2167. aBpmd   db      5,'bpmd',0
  2168. BpmHelp db      'set BreakPoint on Memory access',10
  2169.         db      'Maximum 4 breakpoints of this type are allowed',10
  2170.         db      'Note that for this breaks debugger is activated after access',10
  2171. BpmSyntax db    'Usage: bpmb [w] <expression>',10
  2172.         db      '       bpmw [w] <expression>',10
  2173.         db      '       bpmd [w] <expression>',10
  2174.         db      '       bpm is synonym for bpmd',10
  2175.         db      '"w" means break only on writes (default is on read/write)',10,0
  2176.  
  2177. aBl     db      3,'bl',0
  2178. BlHelp  db      'Breakpoint List',10
  2179. BlSyntax db     'Usage: bl          - list all breakpoints',10
  2180.         db      '       bl <number> - display info on particular breakpoint',10,0
  2181.  
  2182. aBc     db      3,'bc',0
  2183. BcHelp  db      'Breakpoint Clear',10
  2184. BcSyntax db     'Usage: bc <number-list>',10
  2185.         db      'Examples: bc 2',10
  2186.         db      '          bc 1 3 4 A',10,0
  2187.  
  2188. aBd     db      3,'bd',0
  2189. BdHelp  db      'Breakpoint Disable',10
  2190. BdSyntax db     'Usage: bd <number-list>',10
  2191.         db      'Examples: bd 2',10
  2192.         db      '          bd 1 3 4 A',10,0
  2193.  
  2194. aBe     db      3,'be',0
  2195. BeHelp  db      'Breakpoint Enable',10
  2196. BeSyntax db     'Usage: be <number-list>',10
  2197.         db      'Examples: be 2',10
  2198.         db      '          be 1 3 4 A',10,0
  2199.  
  2200. aUnpack db      7,'unpack',0
  2201. UnpackHelp db   'Try to bypass unpacker code',10
  2202. UnpackSyntax db 'Usage: unpack',10,0
  2203.  
  2204. aLoadSymbols db 13,'load-symbols',0
  2205. LoadSymbolsHelp db 'Load symbolic information for executable',10
  2206. LoadSymbolsSyntax db 'Usage: load-symbols <symbols-file-name>',10,0
  2207.  
  2208. aUnknownCommand db 'Unknown command',10,0
  2209.  
  2210. ;-----------------------------------------------------------------------------
  2211. ;                             Error messages
  2212.  
  2213. load_err_msg    db      'Cannot load program. ',0
  2214. unk_err_msg     db      'Unknown error code -%4X',10,0
  2215. aCannotLoadFile db      'Cannot load file. ',0
  2216. unk_err_msg2    db      'Unknown error code %4X.',10,0
  2217. load_err_msgs:
  2218.         dd      .1, 0, .3, 0, .5, .6, 0, 0, .9, .A, 0, 0, 0, 0, 0, 0
  2219.         dd      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, .1E, .1F, .20
  2220. .1              db      'HD undefined.',10,0
  2221. .3              db      'Unknown FS.',10,0
  2222. .5              db      'File not found.',10,0
  2223. .6              db      'Unexpected EOF.',10,0
  2224. .9              db      'FAT table corrupted.',10,0
  2225. .A              db      'Access denied.',10,0
  2226. .1E             db      'No memory.',10,0
  2227. .1F             db      'Not Menuet/Kolibri executable.',10,0
  2228. .20             db      'Too many processes.',10,0
  2229. load_succ_msg   db      'Program loaded successfully! PID=%4X. Use "g" to run.',10,0
  2230. need_debuggee   db      'No program loaded. Use "load" command.',10,0
  2231. aAlreadyLoaded  db      'Program is already loaded. Use "terminate" or "detach" commands',10,0
  2232. terminated_msg  db      'Program terminated.',10,0
  2233. aException      db      'Debugged program caused an exception %2X. ', 0
  2234. msg_fault_sel   dd  aDivide, aDebug, aNonMask, aUndefined, aOverflow
  2235.                 dd  aBounds, aInvalid, aCoProcessorNA, aDoubleFault
  2236.                 dd  aUndefined, aInvalidTSS, aSegment, aStack
  2237.                 dd  aProtection, aPageFault, aUndefined, aCoProcessor
  2238. aDivide         db      '(Divide error)',10,0
  2239. aDebug          db      '(Single-step/debug exception)',10,0
  2240. aNonMask        db      '(Nonmaskable interrupt)',10,0
  2241. aOverflow       db      '(Overflow)',10,0
  2242. aBounds         db      '(Bounds check)',10,0
  2243. aInvalid        db      '(Invalid opcode)',10,0
  2244. aCoProcessorNA  db      '(Coprocessor not available)',10,0
  2245. aDoubleFault    db      '(Double fault)',10,0
  2246. aUndefined      db      '(Undefined fault)',10,0
  2247. aInvalidTSS     db      '(Invalid TSS)',10,0
  2248. aSegment        db      '(Segment not present)',10,0
  2249. aStack          db      '(Stack fault)',10,0
  2250. aProtection     db      '(General protection fault)',10,0
  2251. aPageFault      db      '(Page fault)',10,0
  2252. aCoProcessor    db      '(Coprocessor error)',10,0
  2253. aSuspended      db      'Suspended',10,0
  2254. aContinued      db      'Continuing',10,0
  2255. aRunningErr     db      'Program is running',10,0
  2256. read_mem_err    db      'ERROR: cannot read process memory!!!',10,0
  2257. aBreakpointLimitExceeded db 'Breakpoint limit exceeded',10,0
  2258. aBreakErr       db      'Cannot activate breakpoint, it will be disabled',10,0
  2259. aDuplicateBreakpoint db 'Duplicate breakpoint',10,0
  2260. aInvalidBreak   db      'Invalid breakpoint number',10,0
  2261. OnBeErrMsg      db      'There is already enabled breakpoint on this address',10,0
  2262. aBreakNum       db      '%2X: at %8X',0
  2263. aMemBreak1      db      '%2X: on ',0
  2264. aMemBreak2      db      'read from ',0
  2265. aMemBreak3      db      'access of ',0
  2266. aMemBreak4      db      'byte',0
  2267. aMemBreak5      db      'word',0
  2268. aMemBreak6      db      'dword',0
  2269. aMemBreak7      db      ' at %8X',0
  2270. aOneShot        db      ', one-shot',0
  2271. aDisabled       db      ', disabled',0
  2272. aBreakStop      db      'Breakpoint #%2X',10,0
  2273. aUserBreak      db      'int3 command at %8X',10,0
  2274. ;dbgmsg_str     db      'Debug message for process %4X.',10,0
  2275. aInvAddr        db      'Invalid address',10,0
  2276. NoPrgLoaded_str db      'No program loaded'
  2277. NoPrgLoaded_len = $ - NoPrgLoaded_str
  2278. aRunning        db      'Running'
  2279. aPaused         db      'Paused'
  2280.  
  2281. aFPU            db      '[ FPU ]'
  2282. aMMX            db      '[ MMX ]'
  2283. aSSE            db      '[SSE32]'
  2284. aSSE2           db      '[SSE64]'
  2285. aMMX128         db      '[MMX128]'
  2286.  
  2287. aAVX            db      '[ AVX ]'
  2288. aMSR            db      '[ MSR ]'
  2289. aPoint          db      0x1C
  2290. aMinus          db      '-'
  2291. aColon          db      ':'
  2292. aSpace          db      ' '
  2293. aQuests         db      '??'
  2294. aDots           db      '...'
  2295. aParseError     db      'Parse error',10,0
  2296. aDivByZero      db      'Division by 0',10,0
  2297. calc_string     db      '%8X',10,0
  2298. aNoMemory       db      'No memory',10,0
  2299. aSymbolsLoaded  db      'Symbols loaded',10,0
  2300. aUnaligned      db      'Unaligned address',10,0
  2301. aEnabledBreakErr db     'Enabled breakpoints are not allowed',10,0
  2302. aInterrupted    db      'Interrupted',10,0
  2303. aUnpacked       db      'Unpacked successful!',10,0
  2304. aPacked1        db      'Program is probably packed with ',0
  2305. aPacked2        db      '.',10,'Try to unpack automatically? [y/n]: ',0
  2306. aY_str          db      'y',10,0
  2307. aN_str          db      'n',10,0
  2308. mxp_nrv_name    db      'mxp_nrv',0
  2309. mxp_name        db      'mxp',0
  2310. mxp_lzo_name    db      'mxp_lzo',0
  2311. mtappack_name   db      'mtappack',0
  2312. flags           db      'CPAZSDO'
  2313. flags_bits      db      0,2,4,6,7,10,11
  2314.  
  2315. ;-----------------------------------------------------------------------------
  2316. ;                         Registers strings
  2317.  
  2318. regs_strs:
  2319.         db 'EAX='
  2320.         db 'EBX='
  2321.         db 'ECX='
  2322.         db 'EDX='
  2323.         db 'ESI='
  2324.         db 'EDI='
  2325.         db 'EBP='
  2326.         db 'ESP='
  2327.         db 'EIP='
  2328.         db 'EFLAGS='
  2329.  
  2330. sse_strs:
  2331.         db '-XMM0-'
  2332.         db '-XMM1-'
  2333.         db '-XMM2-'
  2334.         db '-XMM3-'
  2335.         db '-XMM4-'
  2336.         db '-XMM5-'
  2337.         db '-XMM6-'
  2338.         db '-XMM7-'
  2339. avx_strs:
  2340.         db '-YMM0-'
  2341.         db '-YMM1-'
  2342.         db '-YMM2-'
  2343.         db '-YMM3-'
  2344.         db '-YMM4-'
  2345.         db '-YMM5-'
  2346.         db '-YMM6-'
  2347.         db '-YMM7-'
  2348.  
  2349. align 4
  2350. n_digits        dd 10000000
  2351.  
  2352. btn2_tab        dd aFPU
  2353.                 dd aMMX
  2354. btn3_tab        dd aSSE2
  2355.                 dd aMMX128
  2356.                 dd aSSE
  2357.  
  2358. reg_mode        db 1
  2359.  
  2360.  
  2361. include 'disasm_tbl.inc'
  2362.  
  2363. reg_table:
  2364.         db 2,'al',0
  2365.         db 2,'cl',1
  2366.         db 2,'dl',2
  2367.         db 2,'bl',3
  2368.         db 2,'ah',4
  2369.         db 2,'ch',5
  2370.         db 2,'dh',6
  2371.         db 2,'bh',7
  2372.         db 2,'ax',8
  2373.         db 2,'cx',9
  2374.         db 2,'dx',10
  2375.         db 2,'bx',11
  2376.         db 2,'sp',12
  2377.         db 2,'bp',13
  2378.         db 2,'si',14
  2379.         db 2,'di',15
  2380.         db 3,'eax',16
  2381.         db 3,'ecx',17
  2382.         db 3,'edx',18
  2383.         db 3,'ebx',19
  2384.         db 3,'esp',20
  2385.         db 3,'ebp',21
  2386.         db 3,'esi',22
  2387.         db 3,'edi',23
  2388.         db 3,'eip',24
  2389.         db 0
  2390.  
  2391. IncludeIGlobals
  2392.  
  2393. fn70_read_block:
  2394.         dd 0
  2395.         dq 0
  2396.         dd ?
  2397.         dd ?
  2398.         db 0
  2399.         dd ?
  2400.  
  2401. fn70_attr_block:
  2402.         dd 5
  2403.         dd 0,0,0
  2404.         dd fileattr
  2405.         db 0
  2406.         dd ?
  2407.  
  2408. fn70_load_block:
  2409.         dd 7
  2410.         dd 1
  2411. load_params dd 0
  2412.         dd 0
  2413.         dd 0
  2414. i_end:
  2415. loadname:
  2416.         db 0
  2417.         rb 255
  2418.  
  2419. symbolsfile     rb 260
  2420.  
  2421. prgname_ptr dd ?
  2422. prgname_len dd ?
  2423.  
  2424. IncludeUGlobals
  2425.  
  2426. align 4
  2427. debuggee_pid    dd ?
  2428. dbgwnd          dd ?
  2429. temp_break      dd ?
  2430.  
  2431.  
  2432. fpu_mode        db ?
  2433. sse_mode        db ?
  2434. bSuspended      db ?
  2435. bAfterGo        db ?
  2436.  
  2437.  
  2438. messages_pos    dd ?
  2439. messages        rb messages_height*messages_width
  2440.  
  2441. cmdline         rb cmdline_width+1
  2442. cmdline_len     dd ?
  2443. cmdline_pos     dd ?
  2444. curarg          dd ?
  2445.  
  2446. cmdline_prev    rb cmdline_width+1
  2447.  
  2448. was_temp_break  db ?
  2449. symbol_section  db ?
  2450.  
  2451. dbgbufsize      dd ?
  2452. dbgbuflen       dd ?
  2453. dbgbuf          rb 256
  2454.  
  2455. fileattr        rb 40
  2456.  
  2457. ;keep aligned !!!
  2458. ;do not change layout !!!
  2459.  
  2460. align 16
  2461. needzerostart:
  2462. context:
  2463. _eip    dd ?
  2464. _eflags dd ?
  2465. _eax    dd ?
  2466. _ecx    dd ?
  2467. _edx    dd ?
  2468. _ebx    dd ?
  2469. _esp    dd ?
  2470. _ebp    dd ?
  2471. _esi    dd ?
  2472. _edi    dd ?
  2473.  
  2474. _ctx_flags:
  2475.         dd ?
  2476.         dd ?
  2477.  
  2478. _sse_flags:
  2479.  
  2480.  
  2481. _fcw    dw ?
  2482. _fsw    dw ?
  2483. _ftw    db ?
  2484.         db ?
  2485. _fop    dw ?
  2486. _fpu_ip dd ?
  2487.         dw ?
  2488.         dw ?
  2489.  
  2490.         dq ?
  2491.         dq ?
  2492.  
  2493. fpu_context:
  2494. mmx_context:
  2495. _st0:
  2496. _mm0:   rq 2
  2497. _st1:
  2498. _mm1:   rq 2
  2499. _st2:
  2500. _mm2:   rq 2
  2501. _st3:
  2502. _mm3:   rq 2
  2503. _st4:
  2504. _mm4:   rq 2
  2505. _st5:
  2506. _mm5:   rq 2
  2507. _st6:
  2508. _mm6:   rq 2
  2509. _st7:
  2510. _mm7:   rq 2
  2511.  
  2512. sse_context:
  2513. _xmm0   rq 2
  2514. _xmm1   rq 2
  2515. _xmm2   rq 2
  2516. _xmm3   rq 2
  2517. _xmm4   rq 2
  2518. _xmm5   rq 2
  2519. _xmm6   rq 2
  2520. _xmm7   rq 2
  2521. ctx_end:
  2522.  
  2523. oldcontext rb _ctx_flags-context
  2524.         rb 32
  2525.  
  2526. oldfpucontext:
  2527. oldmmxcontext: rb sse_context-fpu_context
  2528. oldssecontext: rb ctx_end-sse_context
  2529.  
  2530. if 0
  2531. avx_context:
  2532. _ymm0   dq 4 dup ?
  2533. _ymm1   dq 4 dup ?
  2534. _ymm2   dq 4 dup ?
  2535. _ymm3   dq 4 dup ?
  2536. _ymm4   dq 4 dup ?
  2537. _ymm5   dq 4 dup ?
  2538. _ymm6   dq 4 dup ?
  2539. _ymm7   dq 4 dup ?
  2540. oldavxcontext rb $-avx_context
  2541. end if
  2542.  
  2543.  
  2544. step_num dd ?
  2545. proc_num dd ?
  2546. dumpread dd ?
  2547. dumppos  dd ?
  2548. dumpdata rb dump_height*10h
  2549.  
  2550. ; breakpoint structure:
  2551. ; dword +0: address
  2552. ; byte +4: flags
  2553. ; bit 0: 1 <=> breakpoint valid
  2554. ; bit 1: 1 <=> breakpoint disabled
  2555. ; bit 2: 1 <=> one-shot breakpoint
  2556. ; bit 3: 1 <=> DRx breakpoint
  2557. ; byte +5: overwritten byte
  2558. ;          for DRx breaks: flags + (index shl 6)
  2559. breakpoints_n = 256
  2560. breakpoints     rb breakpoints_n*6
  2561. drx_break       rd 4
  2562.  
  2563. disasm_buf_size dd ?
  2564.  
  2565. symbols         dd ?
  2566. num_symbols     dd ?
  2567.  
  2568. bReload         db ?
  2569.  
  2570. needzeroend:
  2571.  
  2572. disasm_buffer           rb 256
  2573. disasm_start_pos        dd ?
  2574. disasm_cur_pos          dd ?
  2575. disasm_cur_str          dd ?
  2576. disasm_string           rb 256
  2577.  
  2578. thread_info             process_information
  2579.  
  2580. ;-----------------------------------------------------------------------------
  2581. ;                  Coordinates and sizes for GUI
  2582.  
  2583. data_x_size_dd          dd ?, ?
  2584. messages_x_size_dd      dd ?, ?
  2585. registers_x_pos_dd      dd ?, ?
  2586.  
  2587. disasm_height_dd        dd ?
  2588. disasm_y_size_dd        dd ?, ?
  2589. messages_y_pos_dd       dd ?, ?
  2590. cmdline_y_pos_dd        dd ?, ?
  2591. registers_y_size_dd     dd ?, ?
  2592.  
  2593. i_param         rb 256
  2594.  
  2595. ; stack
  2596.         align   400h
  2597.         rb      400h
  2598. used_mem:
  2599.  
  2600. ; vim: ft=fasm tabstop=4
  2601.  
  2602.