/**************************************** * Computer Algebra System SINGULAR * ****************************************/ /* $Id: sdb.cc,v 1.8 1999-06-10 15:12:04 Singular Exp $ */ /* * ABSTRACT: Singular debugger */ #include // for unlink,fork,execlp,getpid #include // for wait #include "mod2.h" #include "tok.h" #include "mmemory.h" #include "febase.h" #include "ipshell.h" #include "ipid.h" #include "sdb.h" int sdb_lines[]={-1,-1,-1,-1,-1,-1,-1,-1}; char * sdb_files[6]; int sdb_flags=0; int sdb_checkline(char f) { int i; char ff=f>>1; for(i=0;i<7;i++) { if((ff & 1) && (yylineno==sdb_lines[i])) return i+1; ff>>=1; if (ff==0) return 0; } return 0; } void sdb_edit(procinfo *pi) { char * filename = mstrdup("/tmp/sd000000"); sprintf(filename+7,"%d",getpid()); FILE *fp=fopen(filename,"w"); if (fp==NULL) { Print("cannot open %s\n",filename); FreeL(filename); return; } if (pi->language!= LANG_SINGULAR) { Print("cannot edit type %d\n",pi->language); fclose(fp); fp=NULL; } else { char *editor=getenv("EDITOR"); if (editor==NULL) editor=getenv("VISUAL"); if (editor==NULL) editor="vi"; editor=mstrdup(editor); if (pi->data.s.body==NULL) { iiGetLibProcBuffer(pi); if (pi->data.s.body==NULL) { PrintS("cannot get the procedure body\n"); fclose(fp); unlink(filename); FreeL(filename); return; } } fwrite(pi->data.s.body,1,strlen(pi->data.s.body),fp); fclose(fp); int pid=fork(); if (pid!=0) { wait(&pid); } else if(pid==0) { if (strchr(editor,' ')==NULL) { execlp(editor,editor,filename,NULL); Print("cannot exec %s\n",editor); } else { char *p=(char *)Alloc(strlen(editor)+strlen(filename)+2); sprintf(s,"%s %s",editor,filename); system(s); } exit(0); } else { PrintS("cannot fork\n"); } fp=fopen(filename,"r"); if (fp==NULL) { Print("cannot read from %s\n",filename); } else { fseek(fp,0L,SEEK_END); long len=ftell(fp); fseek(fp,0L,SEEK_SET); FreeL((ADDRESS)pi->data.s.body); pi->data.s.body=(char *)AllocL((int)len+1); myfread( pi->data.s.body, len, 1, fp); pi->data.s.body[len]='\0'; fclose(fp); } } unlink(filename); FreeL(filename); } static char *sdb_find_arg(char *p) { p++; while (*p==' ') p++; char *pp=p; while (*pp>' ') pp++; *pp='\0'; return p; } static char sdb_lastcmd='c'; void sdb(Voice * currentVoice, const char * currLine, int len) { int bp=0; if ((len>1) && ((currentVoice->pi->trace_flag & 1) || (bp=sdb_checkline(currentVoice->pi->trace_flag))) ) { loop { char gdb[80]; char *p=(char *)currLine+len-1; while ((*p<=' ') && (p!=currLine)) { p--; len--; } if (p==currLine) return; currentVoice->pi->trace_flag&= ~1; // delete flag for "all lines" Print("(%s,%d) >>",currentVoice->filename,yylineno); fwrite(currLine,1,len,stdout); Print("<<\nbreakpoint %d (press ? for list of commands)\n",bp); p=fe_fgets_stdin(">>",gdb,80); while (*p==' ') p++; if (*p >' ') { sdb_lastcmd=*p; } Print("command:%c\n",sdb_lastcmd); switch(sdb_lastcmd) { case '?': case 'h': { PrintS( "b - print backtrace of calling stack\n" "c - continue\n" "d - delete current breakpoint\n" "e - edit the current procedure (current call will be aborted)\n" "h,? - display this help screen\n" "n - execute current line, break at next line\n" "p - display type and value of the variable \n" "q - quit debugger, set debugger flags(0,1,2)\n" "Q - quit Singular\n"); int i; for(i=0;i<7;i++) { if (sdb_lines[i] != -1) Print("breakpoint %d at line %d in %s\n", i,sdb_lines[i],sdb_files[i]); } break; } case 'd': { Print("delete break point %d\n",bp); currentVoice->pi->trace_flag &= (~Sy_bit(bp)); if (bp!=0) { sdb_lines[bp-1]=-1; } break; } case 'n': currentVoice->pi->trace_flag|= 1; return; case 'e': { sdb_edit(currentVoice->pi); sdb_flags=2; return; } case 'p': { p=sdb_find_arg(p); Print("request `%s`",p); idhdl h=ggetid(p,TRUE); if (h==NULL) PrintS("NULL\n"); else { sleftv tmp; memset(&tmp,0,sizeof(tmp)); tmp.rtyp=IDHDL; tmp.data=h; Print("(type %s):",Tok2Cmdname(tmp.Typ())); tmp.Print(); } break; } case 'b': VoiceBackTrack(); break; case 'q': { p=sdb_find_arg(p); if (*p!='\0') { sdb_flags=atoi(p); Print("new sdb_flags:%d\n",sdb_flags); } return; } case 'Q': m2_end(999); case 'c': default: return; } } } }