/*static char *SCCSID = "@(#)dbgex.h    6.2 91/11/17";*/
/***    DBGEX.H - DosDebug Exported Definitions
 *
 *      SCCSID = @(#)dbgex.h    6.2 91/11/17
 *
 *      THIS IS AN OS/2 VERSION 2.x SPECIFIC FILE
 *
 *       IBM/Microsoft Confidential
 *
 *       Copyright (c) IBM Corporation 1987, 1989
 *       Copyright (c) Microsoft Corporation 1987, 1989
 *
 *       All Rights Reserved
 *
 *      This file defines the Exported (inter-component) interfaces
 *      for the DosDebug function.
 *
 */

/***EK+ DBGP - DosDebug Per-Process Equates
 *
 *      These equates define the possible values of the pptda-> flDbg
 *      flags.  These flags control per-process debugging status.
 *
 *      DBGP_WPHIT :
 *              Set if there may be any remaining Watchpoints hit.
 *
 *      DBGP_EXELOAD :
 *              Set if the debuggee should perform a load notification
 *              for the exe's module.
 *
 *      DBGP_ISADBGER
 *              Set if the current process has ever linked to a debuggee.
 *
 *      DBGP_LINKED
 *              Set if the debuggee has a debugger linked to it.
 *
 *      DBGP_TREE
 *              Set if ExecPgm exec's future children as Debugged.
 *
 *      DBGP_INIT
 *              Set if the debuggee is to initialize itself.
 *
 *      DBGP_HOLD
 *              Set if a thread is currently in DbgHandler - tells
 *              other threads not to execute user code.
 *
 *      DBGP_ASYNC
 *              Set if an Async Stop command was requested.  Thread
 *              should return DBG_N_AsyncStop, with possible Override.
 *
 *      DBGP_PANIC
 *              Set if a Panic Stop is requested.  Thread should
 *              return DBG_N_Success, with a possible Override.
 *
 *      DBGP_MANUALTERM
 *              Set if debuggee was killed explicitly by DosDebug
 *              either via a Terminate command or a timeout.
 *
 *      DBGP_ABORTALL
 *              Set if the debuggee may not return any information
 *              back to the debugger.
 *
 *      DBGP_SSTEP
 *              Set if debugee is single stepping.
 *
 *      DBGP_RANGESTEP
 *              Set if debuggee is performing a range step.
 *              (DBGP_SSTEP will also be set).
 *
 *      DBGP_XCPTPENDING
 *              Indicates an exception report has been sent but not
 *              acknowledged via DBG_C_CONTINUE.
 *
 *      DBGP_DEBUG                                                               // B729833
 *              Prevents DOSPTRACE from being used with DOSDEBUG.                // B729833
 *
 *      DBGP_PTRACE                                                              // B729833
 *              Indicates that the debugger is using DOSPTRACE therefore         // B729833
 *              any breakpoints or single/range steps in 32 Bit code are         // B729833
 *              to be ignored. Also prevents DOSDEBUG from being used            // B729833
 *              with DOSPTRACE.                                                  // B729833
 */

#define DBGP_WPHIT      0x0001L /* A watchpoint was hit                 */
#define DBGP_EXELOAD    0x0002L /* Do a load notification for the exe   */

#define DBGP_ISADBGER   0x0010L /* Process is a debugger                */
#define DBGP_LINKED     0x0020L /* Debuggee is linked to a debugger     */
#define DBGP_TREE       0x0040L /* Exec children as Debuggees           */

#define DBGP_INIT       0x0100L /* Initialization Event                 */
#define DBGP_ASYNC      0x0200L /* Async Stop requested.                */
#define DBGP_PANIC      0x0400L /* Panic Stop requested.                */
#define DBGP_HOLD       0x0800L /* A Debuggee Thread is is DbgHandler   */

#define DBGP_MANUALTERM 0x1000L /* Termination due to Dbg situation     */
#define DBGP_ABORTALL   0x2000L /* Abort all remaining notifications    */

#define DBGP_SSTEP      0x4000L /* Single step debuggee                 */
#define DBGP_RANGESTEP  0x8000L /* Performing a range step              */

#define DBGP_XCPTPENDING  0x10000L

#define DBGP_DEBUG        0x40000000L /* DOSDEBUG being used */                  // B729833
#define DBGP_PTRACE       0x80000000L /* DOSPTRACE being used */                 // B729833

#define DBGP_ALLDONE    (DBGP_MANUALTERM | DBGP_ABORTALL) /* Abort Combo */

#define DBGP_ASYNC_MASK (~(DBGP_LINKED | DBGP_HOLD | DBGP_DEBUG | DBGP_PTRACE))

/*end*/

/***EK+ DBGT - DosDebug Per-Thread Equates
 *
 *      These equates define the possible values of the ptcb-> fDbg
 *      flags.  These flags control each individual thread of the
 *      debuggee process.
 *
 *      DBGT_LIBLOAD
 *              Set if a DLL Load event occured for the current thread.
 *
 *      DBGT_FROZEN
 *              Set/Cleared by Freeze/Resume command to freeze threads.
 *              Thread may not execute user code.
 *
 *      DBGT_FREEZING
 *              Thread is blocked somewhere in the Dbg component, and
 *              the thread status command should return DBG_T_Runnable.
 *
 *      DBGT_SSTEP
 *              This thread's most recent execution was a single step.
 *
 *      DBGT_DEAD
 *              This thread can no longer execute user code.
 *
 *      DBGT_CREATE
 *              This thread needs to perform a Thread Creation notification
 *
 *      DBGT_LIBFREE
 *              Set if a DLL Free event occured for the current thread.
 *
 *      DBGT_XCHGOP
 *              This thread needs to perform an Exchange Opcode command.
 *
 *      DBGT_XCPTHANDLED
 *              Set if an Exception/Breakpoint/SingleStep is considered
 *              HANDLED, otherwise it is considered NOT_HANDLED.
 *
 *      DBGT_XCPTSTOP
 *              Set if the debugger has acknowledged the exception and
 *              does not want the user to receive it. This differs from
 *              DBGT_XCPTHANDLED because no user context is restored via
 *              this flag.
 *
 *      DBGT_NOTIFIED
 *              This thread performed a notification.  If a
 *              SingleStep occurs while this flag is set, this
 *              flag should be cleared, and the single step
 *              should be ignored.  This bit is cleared in
 *              DbgValidateTF if the debuggee thread is going
 *              to return to user space with an IRETD rather
 *              than a RETFD N.  See DCR 2073 for further
 *              explanation.
 *
 *      DBGT_SEMOWNER
 *              This thread owns a semaphore(s) in the semList's
 *              registered by different subsystems. Allow the thread
 *              to continue execution until it releases these
 *              semaphores. When this flag is set the debuggee has
 *              done a DbgEnterCrit() and has pending notifications.
 *
 */

#define DBGT_LIBLOAD            0x0001L
#define DBGT_FROZEN             0x0002L
#define DBGT_FREEZING           0x0004L
#define DBGT_SSTEP              0x0008L
#define DBGT_DEAD               0x0010L
#define DBGT_CREATE             0x0020L
#define DBGT_LIBFREE            0x0040L
#define DBGT_XCHGOP             0x0080L
#define DBGT_NOTIFIED           0x0100L
#define DBGT_XCPTHANDLED        0x0200L
#define DBGT_XCPTSTOP           0x0400L
#define DBGT_SEMOWNER           0x0800L

/* List of indirect events */
#define DBGT_INDIRECT (DBGT_LIBLOAD | DBGT_FROZEN | DBGT_SSTEP | DBGT_CREATE \
                       | DBGT_LIBFREE)

/*end*/

/***EK+ DBGS - DosDebug SeverConnection Equates
 *
 *      This section was added by defect 70301 (APAR PJ09240)
 *
 *      These equates define the possible values for flSever flags.
 *      These flags control severing a connection if a system resource
 *      is being held by the debuggee process.
 *
 *      DBGS_SEVER
 *              This is the default, Sever The Connection,
 *              as we do today.
 *
 *      DBGS_NO_SEVER
 *              Notify the user, a system resource is being held by the
 *              debuggee process.  Do not Sever The Connection.
 *
 *
 */

#define DBGS_SEVER              0x0000
#define DBGS_NO_SEVER           0x0001
#define DBGS_NOSEVER_MSG_OFF    0x0000
#define DBGS_NOSEVER_MSG_ON     0x0001

/*end*/

/***ET+ DCB - Debug Control Block structure
 *
 *      A single Debug Control Block is attached to the PTDA of
 *      each debuggee process.  The fact that a DCB is attached
 *      identifies that process as being a debuggee process.
 *
 *      The DCBs are also on a singly linked list. The debugger
 *      traverses this list to locate the correct debuggee
 *      process.  The head pointer to this list is pdcbHead.
 *
 *      The Debug Control Block contains information that is used
 *      to control communications between the debugger and the
 *      debuggee.  The DCB is used for timing and identification,
 *      but it is NOT used to pass data.
 *
 */

// XLATON

typedef struct DCB_s {
        struct  DCB_s *pdcbNext;        /* pointer to next active DCB      */
        PTDA    *pptdaDbgee;            /* pointer to debuggee PTDA        */
        struct kDB_s *pkDB;             /* Address of active kDB buffer    */
        ulong_t lDbgeePid;              /* PID of debuggee                 */
        ulong_t lDbgerPid;              /* PID of debugger                 */
        ushort_t sActiveTid;            /* TID of 'active' debuggee thread */
        ushort_t sRefer;                /* Number of references to DCB     */
        ulong_t flState;                /* DCB State Bits                  */
        PKSEMMTX hKSEMdbger;            /* Debugger RAM semaphore          */

        ushort_t sFrozen;               /* Number of frozen threads        */

        tcb_t   *sDbgerSStepTCB;        /* Thread to single step           */
        tcb_t   *sDbgeeSStepTCB;        /* Thread to single step           */

        ulong_t dcb_clwpcb;             /* Count of local watchpoints      */
        ulong_t dcb_lDR7local;          /* DR7 bits for local watchpoints  */

        ulong_t dcb_alDRladdr[DR_COUNT]; /* Local DR laddrs                */
        struct WPCB_s *dcb_aplwpcb[DR_COUNT];  /* Local DR control blocks  */
        ushort_t  flSever;              /* SeverConnection Option    70301 */
        ushort_t  flNoSever_Rpt;        /* SeverConnection Option    70301 */
} DCB_t;

// ASM  dcb_alDRladdr0  equ     dcb_alDRladdr+00h
// ASM  dcb_alDRladdr1  equ     dcb_alDRladdr+04h
// ASM  dcb_alDRladdr2  equ     dcb_alDRladdr+08h
// ASM  dcb_alDRladdr3  equ     dcb_alDRladdr+0ch

/*end*/

/***    DBGDR6IMAGE_ENTERKMODE
 */

#define DBGDR6IMAGE_ENTERKMODE  0x80000000L

/***    DLL Load/Free Event flags
 */

#define DBG_LOADDLL     0
#define DBG_FREEDLL     1


/***ET+ dbgvdm_t - VDM watchpoint data block (DPMI)
 *
 *      When a VDM process allocates its first DPMI watchpoint,
 *      one of these blocks is allocated from the kernel resident
 *      heap and attached to the PTDA via ptda_pdcb.  The only
 *      reason the resident heap is used is because the blocks
 *      are small.  This block contains all the information
 *      about a VDM's watchpoints.  See dbgwp.c for more details.
 */

typedef struct dbgvdm_s {
        ulong_t dbgv_cdr;               /* Count of debug registers in use */
        ulong_t dbgv_adr[DR_COUNT];     /* DR0-DR3 images */
        ulong_t dbgv_dr6;               /* DR6 image */
        ulong_t dbgv_dr7;               /* DR7 image */
        uchar_t dbgv_mpiwpidr[DR_COUNT];/* Logical to physical mapping */
} dbgvdm_t;
/*end*/

// XLATON

#define f_ENTERCRIT 0x0001             /* Flags for DbgProcessSemList */
#define f_EXITCRIT  0x0002
#define f_SEMWAKE   0x0004

// XLATOFF

/***    DosDebug Exported Procedures List       */

/***    DbgAliasFree - Notify the debugger that an alias needs to be freed
 */

extern  void    DbgAliasFree (
                        char    *       pchAlias,
                        ushort_t        hptda );

/***    DbgDisable - Disable DosDebug on a Process
 */

extern  void    DbgDisable (
                        PTDA *  pptda );

/***    DbgDRFree - Free a debug register
 */

extern  int     DbgDRFree (
                        ulong_t         lDRid );

/***    DbgDRAlloc - Request a debug register
 */

extern  int     DbgDRAlloc ();

/***    DbgEnable - Enable DosDebug on a Process
 */

extern  int     DbgEnable (
                        PTDA *  pptda );

/***    DbgEvent - Handle an asyncronous debug event via FF_TRC
 */

extern  void    DbgEvent ();

/***    DbgException - Notify debugger of an exception
 */

extern int DbgException (struct _EXCEPTIONREPORTRECORD *pR0ERepRec,
                         struct _EXCEPTIONREPORTRECORD *pR3ERepRec,
                         struct _CONTEXT *pR0Context,
                         struct _CONTEXT *pR3Context,
                         ulong_t ulChance);

/***    DbgKillDebuggees - Kill All Debuggee processes of current process
 */

extern  void    DbgKillDebuggees ();

/***    DbgNewProc - Notify the debugger of a new process
 */

extern  void    DbgNewProc (
                        ulong_t         lPid );

/***    DbgProcTerm - Notify the debugger that the process is terminating
 */

extern  void    DbgProcTerm ();

/***    DbgSignal - Notify debugger of a signal
 */

extern  void    DbgSignal (unsigned long flSigPend);

/***    DbgTrap - Notify debugger of a trap (single step or breakpoint)
 */

extern  int     DbgTrap (
                        ulong_t         lTrapNum );

/***    DbgThreadTerm - Notify the debugger that a thread is terminating
 */

extern  void    DbgThreadTerm (
                        ulong_t         lExitCode );

/***    DbgWPScan - Scan the CPU Debug Registers for a possible hit
 */

extern  int     DbgWPScan ();

/***    DbgLibLoadEvent - Routine to notify debugger of a module load
 */

extern  void    DbgDLLEvent(
                        ulong_t         flEvent,
                        tcb_t *         ptcb );

/***    DbgValidateTF - Set the user TF bit to the correct value
 */

extern  void    DbgValidateTF();

/***    NPXReadContext - Read floating point context
 */

extern int NPXReadContext(tcb_t *, char *, ulong_t *, ulong_t);

/***    NPXWriteContext - Write floating point context
 */

extern int NPXWriteContext(tcb_t *, char *, ulong_t *, ulong_t);

/***    DBGDRDisableVDM - disable VDM watchpoints
 */

extern void DBGDRDisableVDM(struct dbgvdm_s *pdbgv);

/***    DBGDREnableVDM - enable VDM watchpoints
 */

extern void DBGDREnableVDM(struct dbgvdm_s *pdbgv);

/***    DBGWPAllocVDM - allocate a watchpoint for a VDM
 */

extern int DBGWPAllocVDM(ulong_t laddr,ulong_t cb,ulong_t type,ulong_t *piwp);

/***    DBGWPFreeVDM - free a watchpoint for a VDM
 */

extern int DBGWPFreeVDM(ulong_t iwp);

/***    DBGDR6QueryVDM - query/update DR6 for a VDM process
 */

extern int DBGDR6QueryVDM(ulong_t flquery,ulong_t flreset,ulong_t *pfl);
