⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ntoacc.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Trap file for QNX Neutrino interactive debugging.
*
****************************************************************************/


#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <spawn.h>
#include <confname.h>
#include <sys/stat.h>
#include <sys/netmgr.h>
#include <sys/neutrino.h>
#include <sys/resource.h>
#include "trpimp.h"
#include "trperr.h"
#include "mad.h"
#include "madregs.h"
#include "ntocomm.h"
#include "x86cpu.h"
#include "misc7386.h"


typedef unsigned short  USHORT;
typedef unsigned long   ULONG;

static pid_t            OrigPGrp;

process_info        ProcInfo;

//#define MAX_WP  32
//struct _watch_struct    WatchPoints[ MAX_WP ];
short               WatchCount = 0;


unsigned nto_node( void )
{
    unsigned    node;
    
    if( ND_NODE_CMP( ProcInfo.node, ND_LOCAL_NODE ) == 0 ) {
        return( ND_LOCAL_NODE );
    }
    node = netmgr_strtond( ProcInfo.procfs_path, 0 );
    if( node == -1 ) {
        dbg_print(( "QNX node lost!\n" ));
    }
    return( node );
}


static bool get_nto_version( unsigned_8 *major, unsigned_8 *minor )
{
    char    buf[32];

    *major = *minor = 0;
	if( confstr( _CS_RELEASE, buf, sizeof( buf ) ) ) {
        char    *s = buf;

        *major = atoi( s );
        s = strchr( s, '.' );
        if( s ) {
            *minor = atoi( ++s );
            return( TRUE );
        }
    }
    return( FALSE );
}


unsigned ReqGet_sys_config( void )
{
    get_sys_config_ret  *ret;

    ret = GetOutPtr( 0 );
    ret->sys.os = OS_QNX;

    get_nto_version( &ret->sys.osmajor, &ret->sys.osminor );

    ret->sys.cpu = X86CPUType();
    if( HAVE_EMU ) {
        ret->sys.fpu = X86_EMU;
    } else {
        ret->sys.fpu = ret->sys.cpu & X86_CPU_MASK;
    }
    ret->sys.huge_shift = 3;
    ret->sys.mad = MAD_X86;
    return( sizeof( *ret ) );
}


unsigned ReqChecksum_mem( void )
{
    char                buf[256];
    addr_off            offv;
    USHORT              length;
    USHORT              size;
    int                 i;
    USHORT              amount;
    ULONG               sum;
    checksum_mem_req    *acc;
    checksum_mem_ret    *ret;

    acc = GetInPtr( 0 );
    ret = GetOutPtr( 0 );
    dbg_print(( "mem checksum at %08x, %d bytes\n", 
                (unsigned)acc->in_addr.offset, acc->len ));
    sum = 0;
    if( ProcInfo.pid && !ProcInfo.at_end ) {
        length = acc->len;
        offv = acc->in_addr.offset;
        for( ;; ) {
            if( length == 0 )
                break;
            size = (length > sizeof( buf )) ? sizeof( buf ) : length;
            amount = ReadMem( ProcInfo.procfd, buf, offv, size );
            for( i = amount; i != 0; --i )
                sum += buf[ i - 1 ];
            offv += amount;
            length -= amount;
            if( amount != size ) break;
        }
    }
    ret->result = sum;
    return( sizeof( *ret ) );
}


unsigned ReqRead_mem( void )
{
    read_mem_req    *acc;
    unsigned        len = 0;

    acc = GetInPtr( 0 );
    CONV_LE_32( acc->mem_addr.offset );
    CONV_LE_16( acc->mem_addr.segment );
    CONV_LE_16( acc->len );
    if( ProcInfo.pid && !ProcInfo.at_end ) {
#ifdef DEBUG_MEM
        dbg_print(( "mem read at %08x, %d bytes\n", (unsigned)acc->mem_addr.offset, acc->len ));
#endif
        len = ReadMem( ProcInfo.procfd, GetOutPtr( 0 ), acc->mem_addr.offset, acc->len );
    }
    return( len );
}


unsigned ReqWrite_mem( void )
{
    write_mem_req   *acc;
    write_mem_ret   *ret;
    unsigned        len;

    acc = GetInPtr( 0 );
    CONV_LE_32( acc->mem_addr.offset );
    CONV_LE_16( acc->mem_addr.segment );
    ret = GetOutPtr( 0 );
    len = GetTotalSize() - sizeof( *acc );
    ret->len = 0;
    if( ProcInfo.pid && !ProcInfo.at_end ) {
#ifdef DEBUG_MEM
        dbg_print(( "mem write at %08x, %d bytes\n", (unsigned)acc->mem_addr.offset, len ));
#endif
        ret->len = WriteMem( ProcInfo.procfd, GetInPtr( sizeof( *acc ) ), acc->mem_addr.offset, len );
    }
    CONV_LE_16( ret->len );
    return( sizeof( *ret ) );}


unsigned ReqRead_io( void )
{
    read_io_req *acc;
    void        *ret;
    unsigned    len;

// TODO: implement I/O port reads (if possible)
    acc = GetInPtr( 0 );
    ret = GetOutPtr( 0 );
    len = 0;
    return( len );
}


unsigned ReqWrite_io( void )
{
    write_io_req    *acc;
    write_io_ret    *ret;
    void            *data;
    unsigned        len;

// TODO: implement I/O port writes (if possible)
    acc  = GetInPtr( 0 );
    data = GetInPtr( sizeof( *acc ) );
    len  = GetTotalSize() - sizeof( *acc );
    ret  = GetOutPtr( 0 );
    ret->len = 0;
    return( sizeof( *ret ) );
}


static void ReadCPU( struct x86_cpu *r )
{
    debug_greg_t    qnx_regs;
    int             regsize;

    memset( r, 0, sizeof( *r ) );
    if( devctl( ProcInfo.procfd, DCMD_PROC_GETGREG, &qnx_regs, sizeof( *r ), &regsize ) == EOK ) {
        r->eax = qnx_regs.x86.eax;
        r->ebx = qnx_regs.x86.ebx;
        r->ecx = qnx_regs.x86.ecx;
        r->edx = qnx_regs.x86.edx;
        r->esi = qnx_regs.x86.esi;
        r->edi = qnx_regs.x86.edi;
        r->ebp = qnx_regs.x86.ebp;
        r->esp = qnx_regs.x86.esp;
        r->eip = qnx_regs.x86.eip;
        r->efl = qnx_regs.x86.efl;
        r->cs  = qnx_regs.x86.cs;
        r->ss  = qnx_regs.x86.ss;
#ifdef __SEGMENTS__
        r->ds  = qnx_regs.x86.ds;
        r->es  = qnx_regs.x86.es;
        r->fs  = qnx_regs.x86.fs;
        r->gs  = qnx_regs.x86.gs;
#endif
    }
}

static void ReadFPU( struct x86_fpu *r )
{
    unsigned    regsize;

    memset( r, 0, sizeof( *r ) );
    regsize = sizeof( *r );
    devctl( ProcInfo.procfd, DCMD_PROC_GETFPREG, r, regsize, &regsize );
}

unsigned ReqRead_cpu( void )
{
    ReadCPU( GetOutPtr( 0 ) );
    return( sizeof( struct x86_cpu ) );
}

unsigned ReqRead_fpu( void )
{
    ReadFPU( GetOutPtr( 0 ) );
    return( sizeof( struct x86_fpu ) );
}

unsigned ReqRead_regs( void )
{
    mad_registers       *mr;

    mr = GetOutPtr( 0 );

    ReadCPU( &mr->x86.cpu );
    ReadFPU( &mr->x86.fpu );
    return( sizeof( mr->x86 ) );
}

static void WriteCPU( struct x86_cpu *r )
{
    debug_greg_t    qnx_regs;

    qnx_regs.x86.eax = r->eax;
    qnx_regs.x86.ebx = r->ebx;
    qnx_regs.x86.ecx = r->ecx;
    qnx_regs.x86.edx = r->edx;
    qnx_regs.x86.esi = r->esi;
    qnx_regs.x86.edi = r->edi;
    qnx_regs.x86.ebp = r->ebp;
    qnx_regs.x86.esp = r->esp;
    qnx_regs.x86.eip = r->eip;
    qnx_regs.x86.efl = r->efl;
    qnx_regs.x86.cs = r->cs;
    qnx_regs.x86.ss = r->ss;
#ifdef __SEGMENTS__
    qnx_regs.x86.ds = r->ds;
    qnx_regs.x86.es = r->es;
    qnx_regs.x86.fs = r->fs;
    qnx_regs.x86.gs = r->gs;
#endif
    devctl( ProcInfo.procfd, DCMD_PROC_SETGREG, &qnx_regs, sizeof( qnx_regs ), 0 );
}

static void WriteFPU( struct x86_fpu *r )
{
    if( ProcInfo.pid != 0 ) {
        devctl( ProcInfo.procfd, DCMD_PROC_SETFPREG, r, sizeof( X86_FSAVE_REGISTERS ), 0 );
    }
}

unsigned ReqWrite_cpu( void )
{
    WriteCPU( GetInPtr( sizeof( write_cpu_req ) ) );
    return( 0 );
}

unsigned ReqWrite_fpu( void )
{
    WriteFPU( GetInPtr( sizeof( write_fpu_req ) ) );
    return( 0 );
}

unsigned ReqWrite_regs( void )
{
    mad_registers       *mr;

    mr = GetInPtr( sizeof( write_regs_req ) );
    WriteCPU( &mr->x86.cpu );
    WriteFPU( &mr->x86.fpu );
    return( 0 );
}

static int SplitParms( char *p, char *args[], unsigned len )
{
    int     i;
    char    endc;

    i = 0;
    if( len == 1 ) goto done;
    for( ;; ) {
        for( ;; ) {
            if( len == 0 ) goto done;
            if( *p != ' ' && *p != '\t' ) break;
            ++p;
            --len;
        }
        if( len == 0 ) goto done;
        if( *p == '"' ) {
            --len;
            ++p;
            endc = '"';
        } else {
            endc = ' ';
        }
        if( args != NULL ) {
            args[i] = p;
        }
        ++i;
        for( ;; ) {
            if( len == 0 ) goto done;
            if( *p == endc
             || *p == '\0'
             || (endc == ' ' && *p == '\t' ) ) {
                if( args != NULL ) {
                    *p = '\0';  //NYI: not a good idea, should make a copy
                }
                ++p;
                --len;
                if( len == 0 ) goto done;
                break;
            }
            ++p;
            --len;
        }
    }
done:
    return( i );
}

static pid_t proc_attach( pid_t pid, int *pfd, pthread_t *ptid )
{
    procfs_status       status;
    struct sigevent     event;
    char                path[PATH_MAX];
    int                 ctl_fd;

    snprintf( path, sizeof( path ) - 1, "%s/%d/as", ProcInfo.procfs_path, pid );
    dbg_print(( "attaching to pid %d, procfs path '%s'\n", pid, path ));
    ctl_fd = open( path, O_RDWR );
    if( ctl_fd == -1 ) {
        dbg_print(("failed to open proc file '%s'\n", path));
        return( 0 );
    }
    
    if( devctl( ctl_fd, DCMD_PROC_STOP, &status, sizeof( status ), 0 ) != EOK ) {
        dbg_print(("failed to stop process\n"));
        return( 0 );
    }
    
    /* Define a sigevent for process stopped notification. The OS will deliver
     * SIGUSR1 to us whenever the debuggee stops. We block SIGUSR1 and only
     * unblock it temporarily when expecting a notification.
     */
    event.sigev_notify = SIGEV_SIGNAL_THREAD;
    event.sigev_signo  = SIGUSR1;
    event.sigev_code   = 0;
    event.sigev_value.sival_ptr = NULL;
    event.sigev_priority = -1;
    devctl( ctl_fd, DCMD_PROC_EVENT, &event, sizeof( event ), 0 );
    
    if( devctl( ctl_fd, DCMD_PROC_STATUS, &status, sizeof( status ), 0 ) == EOK
      && status.flags & _DEBUG_FLAG_STOPPED ) {
        dbg_print(( "debuggee stopped - sending SIGCONT to pid %d\n", pid ));
        SignalKill( nto_node(), pid, 0, SIGCONT, 0, 0 );
    }
    *pfd = ctl_fd;
    *ptid = status.tid;
    return( pid );
}

static void proc_detach( char *args )
{
    int     sig = 0;

    dbg_print(( "detaching from current process (pid %d)\n", ProcInfo.pid ));
    if( args ) {
        sig = atoi( args );
    }
    
    if( sig ) {
        SignalKill( nto_node(), ProcInfo.pid, 0, sig, 0, 0 );
    }
    close( ProcInfo.procfd );
    ProcInfo.procfd = -1;
    ProcInfo.pid    = 0;
}

static pid_t RunningProc( char *name, char **name_ret )
{
    pid_t       pid;
    char        ch;
    char        *start;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -