/*static char *SCCSID = "@(#)ptrace.c   6.5 92/03/29";*/
#define SCCSID "@(#)ptrace.c    6.5 92/03/29"
/***    PTRACE.C - DosDebug/PTrace Conversion Routines
 *
 *      SCCSID = @(#)ptrace.c   6.5 92/03/29
 *
 *      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 contains the API interfaces for the PTrace function, with
 *      all conversions of the PTrace structure to the DosDebug structure.
 *
 *      This will enable any 16-bit PTrace calls to be used on OS/2 2.0,
 *      and use the enhanced API calls of DosDebug.
 *
 *****************************************************************************
 *  Change                                                               Chg *
 *  Activity      Date    BY   Reason                                    Num *
 *  --------      ----    ---  -------                                   --- *
 *  DCR 1372    10/19/91  ARJ  Added for DOSPTRACE Support NEW           NEW *
 *  PTR B729735 11/14/91  ARJ  Correct IFDEF STATEMENTS (No CHG NUM)         *
 *  PTR B730997 02/07/92  Paul Change sequence on include files              *
 *  PTR B735295 03/29/92  CB   reissue orig. cmd if 32-bit notification rcvd *
 *****************************************************************************
 *****************************************************************************/

#define INCL_TYPES                  /* DRV_\H\basedef.h typedefs            */
#define INCL_ERROR_H                /* DRV_\H\bseerr.h  OS/2 error numbers  */
#define INCL_ERROR2_H               /* DRV_\H\bseerr.h  OS/2 error numbers  */
#define FOR_EXEHDR     1            /*                                      */
#define FNO_NUM       FNO_PTRACE_C  /* DRV_\SRC\DOS\DOSINC\fnotask.h        */

#include <fnotask.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <osconst.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <ostypes.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <osmac.h>                  /*                                      */
#include <basedef.h>                /* DRV_\H\                              */
#include <error.h>                  /*                                      */
#include <error2.h>                 /*                                      */
#include <infoseg.h>                /* DRV_\H\                              */
#include <vmowner.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <dossymc.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <sigdef.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <except.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <bsexcpt.h>                /* DRV_\H\                              */
#include <mi.h>                     /* DRV_\H\                              */
#include <pgexport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <vmexport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <selxport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <seldesc.h>                /* DRV_\H\                              */
#include <tkexport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <curdir.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <dirent.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <find.h>                   /* DRV_\SRC\DOS\DOSINC\                 */
#include <screen.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <sem.h>                    /* DRV_\SRC\DOS\DOSINC\                 */
#include <tkptda.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <bseerr.h>                 /* DRV_\H\                              */
#include <bsetib.h>                 /* DRV_\H\                              */
#include <tklocal.h>                /* DRV_\SRC\DOS\TASK\                   */
#include <proc.h>                   /* DRV_\SRC\DOS\DOSINC\                 */
#include <newexe.h>
#include <exe386.h>
#include <kmode.h>
#include <ldrxport.h>               /* DRV_\SRC\DOS\DOSINC\       b730997   */
#include <ldrvars.h>                /* DRV_\SRC\DOS\DOSINC\       b730997   */
#include <ptrace.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <sched.h>                  /* DRV_\SRC\DOS\DOSINC\                 */
#include <string.h>                 /* DRV_\H\  and  DRV_\H386\             */
#include <dbgapi.h>                 //d57053
#include <dbgex.h>                  //d57053
#include <dbgin.h>                  //d57053


#ifdef PTSTRICT
/***LD   PTBugBug - Global Variable for debugging
 * DosPtrace DeBug Flags (bit settings) are defined in PTRACE.H
 * NOTE - DosPtrace is thunked to DosDebug
 * DosDebug DeBug Flags (bit settings) are defined in DBGAPI.H
 */
    ULONG  PTBugBug = 0;
#endif

ULONG   PTraceCmd(kPTB *);

// now referenced via System Anchor Block - f122427
extern  DCB_t  *pdcbHead;

extern  USHORT PT_CvtOUTBuff(kPTB *);
extern  ULONG  PT_CvtINBuff(kPTB *);
extern  USHORT PTCheck(kPDB_t *);
// tw
#pragma code_seg("DEBUGSWAP_TEXT", "CODE")      /* swap PTRACE code */

ULONG   PTraceCmd(kPTB *kPTBcmd)
{
    ULONG       rc = 0L;
    VMHOB       dbgee_MTE;
    PTDA       *pdbgee_PTDA;
    ldrmte_t   *pmte;
    uPTB_t     *PTB32;

#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** PTraceCmd        ");   }
#endif

    PTB32 = (uPTB_t *)kPTBcmd->ulPTBAddr;

    if (PTB32->Cmd > TRC_C_MAXCMD) {
#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** cmd > TRC_C_MaxCmd");   }
#endif
        return(ERROR_INVALID_PARAMETER);
    } /* endif */

    rc = PTPidToPTDA(PTB32->PID, SSToDS(&pdbgee_PTDA));              //d57053

    if (rc != NO_ERROR)
        {
#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** PTPidToPTDA Failed");   }               //d57053
#endif
        return(rc);
        } /* if end */

    dbgee_MTE = (VMHOB)pdbgee_PTDA->ptda_module;

    pmte = ldrValidateMteHandle(dbgee_MTE);

    if ((!pmte) || !ldrIsNE(pmte)) {
        PTB32->Cmd = TRC_C_ERR_ret;
        PTB32->Value = trace_child_untraceable;
#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** This Is not a 16 bit EXE ");   }
#endif
        return(0);
    } /* endif */

    kPTBcmd->command = PTB32->Cmd;

    if (PTB32->Cmd == TRC_C_GetLibName && PTB32->Value == 0)
       {  PTB32->Value = dbgee_MTE;  }

    if (PTB32->MTE == 0)
       {   PTB32->MTE = dbgee_MTE;   }
retry:  /* b735295 */
    rc = PT_CvtINBuff(kPTBcmd);

#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** RC from PT_CvtInBuff: %4x",rc);   }
#endif

    if (rc == NO_ERROR && kPTBcmd->command != TRC_C_GetLibName)
    {
        rc = PTCheck(&(kPTBcmd->wrkPDB));

        if /* rc == NO_ERROR &&  if no error and notify is for a 32-bit event */
           (kPTBcmd->wrkPDB.wCmd < TRC_C_AFR_ret) goto retry;

#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** RC from PT_Check:     %4x",rc);   }
#endif

        rc = PT_CvtOUTBuff(kPTBcmd);

#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {  printf("\nPTRACE.c ** RC from PT_CvtOutBuff %4d",rc);  }
#endif

    } /* endif */

#ifdef PTSTRICT
    if (PTBugBug & PTRACEc)
    {printf("\nPTRACE.c ** Exit RC: %4d, CMD %4d",rc,PTB32->Cmd);}
#endif

    return(rc);
}

//d57053
int PTPidToPTDA (ulong_t pid, PPTDA *pPTDA) {
   register DCB_t *pdcb = pdcbHead;

   while (pdcb != NULL && pdcb->lDbgeePid != pid)
       pdcb = pdcb->pdcbNext;

   if (pdcb == NULL || (pdcb->pptdaDbgee->ptda_ProcStatus & PS_EMBRYO) ||
       (pdcb->flState & DBGDCBS_DBGEEDEAD))
       return(ERROR_INVALID_DATA);

   *pPTDA = pdcb->pptdaDbgee;

   return (NO_ERROR);
}
