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

📄 main.c

📁 Linux下的类似softice的调试工具
💻 C
📖 第 1 页 / 共 3 页
字号:
		}    }    if(link != (-1))    {        // find global string table        pSHdr = pSHdrOrig;        for(i=0;i<num;i++,pSHdr++)        {            //int sh_name = pSHdr->sh_name;            //printf("LOADER: [%u] %32s %8x %8x %8x %8x %8x\n",i,pStr,pSHdr->sh_offset,pSHdr->sh_size,pSHdr->sh_addr,pSHdr->sh_type,pSHdr->sh_link);		    if(pSHdr->sh_type == SHT_STRTAB && i==link)		    {			    *ppStr = (LPSTR)((int)p+pSHdr->sh_offset);				*pnSymStrLen = pSHdr->sh_size;		    }	    }	    if(*ppStr && pSym)	    {		    LPSTR pStr = *ppStr;			pSymOrig = pSym;		    for(i=0;i<ulSymTabEntries/sizeof(Elf32_Sym);i++)		    {			    pName = &pStr[pSym->st_name];			    //printf("LOADER: [%u] %32s %x %x %x %x\n",i,pName,pSym->st_name,pSym->st_value,pSym->st_info,pSym->st_other);			    pSym++;		    }	    }	    *pLen = ulSymTabEntries;    }    else    {        pSymOrig= NULL;    }    //printf("LOADER: leave find_symtab()\n");	return pSymOrig;}///////////////////////////////////////////////////////////////////////////////////// process_elf()/////////////////////////////////////////////////////////////////////////////////////int process_elf(char* filename,int file,void* p,int len){	Elf32_Ehdr* pEHdr =(Elf32_Ehdr*)p;	Elf32_Shdr* pSHdr;	char* pStr;	PSTAB_ENTRY pStab;	int nStabLen,nSym;	LPSTR pStrTab;	Elf32_Sym* pSymTab;	char szSymName[2048];	int fileout;	int nSymStrLen,nStabStrLen;    int iRetVal = 0;    //printf("LOADER: enter process_elf()\n");    if(strncmp(pEHdr->e_ident,"\177ELF",4) == 0) // is ELF binary magic    {        pSHdr = (Elf32_Shdr*)((int)p+pEHdr->e_shoff);        //printf("LOADER: Section header @ %x (offset %x)\n",pSHdr,pEHdr->e_shoff);        //printf("LOADER: %u entries\n",pEHdr->e_shnum);        //printf("LOADER: string table index %u\n",pEHdr->e_shstrndx);		if((pSymTab = find_symtab(p,pSHdr,pEHdr->e_shnum,pEHdr->e_shstrndx,&nSym,&pStrTab,&nSymStrLen)) != NULL )		{			find_stab_sections(p,pSHdr,&pStab,&nStabLen,&pStr,&nStabStrLen,pEHdr->e_shnum,pEHdr->e_shstrndx);			if(pStab && nStabLen && pStr && nStabStrLen)			{				LPSTR pDot;				strcpy(szSymName,filename);				//printf("LOADER: file name = %s\n",szSymName);				if((pDot = strrchr(szSymName,'.')))				{					*pDot = 0;					strcat(pDot,".sym");				}				else				{					strcat(szSymName,".sym");				}				//printf("LOADER: symbol file name = %s\n",szSymName);                printf("LOADER: creating symbol file %s for %s\n",szSymName,filename);                fileout = creat(szSymName,S_IRUSR|S_IWUSR);     // make r/w for owner                if(fileout != -1)								{					process_stabs(szSymName,								  fileout,								  pSHdr,								  pEHdr->e_shnum*sizeof(Elf32_Shdr),								  p,								  pStab,								  nStabLen,								  pStr,								  nStabStrLen,								  (LPSTR)pSymTab,								  nSym,								  pStrTab,								  nSymStrLen);					close(fileout);				}                else                {                    printf("LOADER: creation of symbol file %s failed\n",szSymName);					iRetVal = 2;                }			}            else                                                                      {                                                                             printf("LOADER: file %s has no data inside symbol tables\n",filename);				if( ulGlobalVerbose )				{                    if( !pStab || !nStabLen )                                                     printf("LOADER: - symbol table is empty or not present\n");                    if( !pStr  || !nStabStrLen )                                                  printf("LOADER: - string table is empty or not present\n");				}                iRetVal = 2;                                                      }                                                                     }                                                                             else                                                                          {                                                                                 printf("LOADER: file %s does not have a symbol table\n",filename);                         iRetVal = 2;        }    }    else                                                                              {                                                                                     printf("LOADER: file %s is not an ELF binary\n",filename);                            iRetVal = 1;                                                              }	       //printf("LOADER: leave process_elf()\n");    return iRetVal;}///////////////////////////////////////////////////////////////////////////////////// process_file()/////////////////////////////////////////////////////////////////////////////////////int process_file(char* filename){	int file;	void* p;	off_t len;	int iRetVal=0;    //printf("LOADER: enter process_file()\n");    file = open(filename,O_RDONLY);    if(file>0)    {        //printf("LOADER: opened %s as FD %x\n",filename,file);        len = lseek(file,0,SEEK_END);        printf("LOADER: file %s is %u bytes\n",filename,(int)len);                lseek(file,0,SEEK_SET);        p = malloc(len+16);        if(p)        {            //printf("LOADER: malloc'd @ %x\n",p);            memset(p,0,len+16);            if(len == read(file,p,len))            {                //printf("LOADER: trying ELF format\n");                iRetVal = process_elf(filename,file,p,len);            }        }        close(file);    }    else    {        printf("LOADER: file %s could not be opened\n",filename);		iRetVal = 1;    }    //printf("LOADER: leave process_file()\n");    return iRetVal;}///////////////////////////////////////////////////////////////////////////////////// open_debugger()/////////////////////////////////////////////////////////////////////////////////////int	open_debugger(void){    debugger_file = open("/dev/pice0",O_RDONLY);	if(debugger_file<0)	{		printf("LOADER: debugger is not loaded\n");	}	return debugger_file;}///////////////////////////////////////////////////////////////////////////////////// close_debugger()/////////////////////////////////////////////////////////////////////////////////////void close_debugger(void){	close(debugger_file);}///////////////////////////////////////////////////////////////////////////////////// banner()/////////////////////////////////////////////////////////////////////////////////////void banner(void){    printf("#########################################################\n");    printf("####       Symbols LOADER/TRANSLATOR for PICE        ####\n");    printf("#########################################################\n");}#define ACTION_NONE             0#define ACTION_LOAD             1#define ACTION_UNLOAD           2#define ACTION_TRANS            3#define ACTION_RELOAD           4#define ACTION_INSTALL          5#define ACTION_UNINSTALL        6#define ACTION_STATUS           7#define ACTION_BREAK            8#define ACTION_TERMINAL         9#define ACTION_STRESSTEST       10#define ACTION_USERBREAK		11///////////////////////////////////////////////////////////////////////////////////// change_symbols()/////////////////////////////////////////////////////////////////////////////////////void change_symbols(int action,char* pfilename){    int iRetVal = 0;		switch(action)	{		case ACTION_LOAD:			printf("LOADER: loading symbols from %s\n",pfilename);			if(open_debugger()>=0)			{				iRetVal = ioctl(debugger_file,PICE_IOCTL_LOAD,pfilename);				close_debugger();			}			break;		case ACTION_UNLOAD:			printf("LOADER: unloading symbols from %s\n",pfilename);			if(open_debugger()>=0)			{				iRetVal = ioctl(debugger_file,PICE_IOCTL_UNLOAD,pfilename);				close_debugger();			}			break;		case ACTION_RELOAD:			printf("LOADER: reloading all symbols\n");			if(open_debugger()>=0)			{				ioctl(debugger_file,PICE_IOCTL_RELOAD,NULL);				close_debugger();    			printf("LOADER: reloading DONE!\n");			}			break;        default :            printf("LOADER: an internal error has occurred at change_symbols\n");	}	    switch( iRetVal )	{	    case -EINVAL :			printf("LOADER: debugger return value = -EINVAL, operation has failed\n");			break;		case 0 :			// success - silently proceed			break;		default :			printf("LOADER: debugger return value = %i, operation possibly failed\n",iRetVal);	}}///////////////////////////////////////////////////////////////////////////////////// tryinstall()/////////////////////////////////////////////////////////////////////////////////////int tryinstall(void){    char *argv[]={"/sbin/insmod","pice.o",NULL};    int err = 0;    int pid,status;        banner();    printf("LOADER: trying to install debugger...\n");    if(!(open_debugger() < 0) )    {        printf("LOADER: debugger already installed...\n");        close_debugger();        return 0;    }    // create a separate thread    pid = fork();    switch(pid)    {        case -1:            // error when forking, i.e. out E_NOMEM            err = errno;            printf("LOADER: fork failed for execution of '%s' (errno = %u).\n",argv[0],err);            break;        case 0:            // child process handler             execve(argv[0],argv,NULL);            // returns only on error, with return value -1, errno is set            printf("LOADER: couldn't execute '%s' (errno = %u)\n",argv[0],errno);            exit(255);            break;        default:            // parent process handler            printf("LOADER: waiting for debugger to load...\n");            pid = waitpid(pid, &status, 0); // suspend until child is done            if( (pid>0) && WIFEXITED(status) && (WEXITSTATUS(status) == 0) )                printf("LOADER: debugger loaded!\n");            else if( pid<=0 )            {                printf("LOADER: Error on loading debugger! (waitpid() = %i)\n",pid);                err = -1;            }            else if( !WIFEXITED(status) )            {                printf("LOADER: Error on loading debugger! (ifexited = %i)\n",WIFEXITED(status));                err = -1;            }            else            {                printf("LOADER: Error on loading debugger! (exitstatus = %u)\n",WEXITSTATUS(status));                err = WEXITSTATUS(status);            }            break;    }        return err;}///////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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