[dd9b83] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[dfd346] | 4 | /* $Id: sdb.cc,v 1.8 1999-06-10 15:12:04 Singular Exp $ */ |
---|
[dd9b83] | 5 | /* |
---|
| 6 | * ABSTRACT: Singular debugger |
---|
| 7 | */ |
---|
| 8 | |
---|
[ad4bc9] | 9 | #include <unistd.h> // for unlink,fork,execlp,getpid |
---|
| 10 | #include <sys/wait.h> // for wait |
---|
[dd9b83] | 11 | #include "mod2.h" |
---|
| 12 | #include "tok.h" |
---|
[8dd3ac] | 13 | #include "mmemory.h" |
---|
[dd9b83] | 14 | #include "febase.h" |
---|
| 15 | #include "ipshell.h" |
---|
[8dd3ac] | 16 | #include "ipid.h" |
---|
[dd9b83] | 17 | #include "sdb.h" |
---|
| 18 | |
---|
[743c32] | 19 | int sdb_lines[]={-1,-1,-1,-1,-1,-1,-1,-1}; |
---|
[ad4bc9] | 20 | char * sdb_files[6]; |
---|
[45ce793] | 21 | int sdb_flags=0; |
---|
[8dd3ac] | 22 | |
---|
| 23 | int sdb_checkline(char f) |
---|
| 24 | { |
---|
| 25 | int i; |
---|
| 26 | char ff=f>>1; |
---|
| 27 | for(i=0;i<7;i++) |
---|
| 28 | { |
---|
| 29 | if((ff & 1) && (yylineno==sdb_lines[i])) |
---|
| 30 | return i+1; |
---|
| 31 | ff>>=1; |
---|
| 32 | if (ff==0) return 0; |
---|
| 33 | } |
---|
| 34 | return 0; |
---|
| 35 | } |
---|
| 36 | |
---|
[ad4bc9] | 37 | void sdb_edit(procinfo *pi) |
---|
| 38 | { |
---|
[4e08f55] | 39 | char * filename = mstrdup("/tmp/sd000000"); |
---|
[ad4bc9] | 40 | sprintf(filename+7,"%d",getpid()); |
---|
| 41 | FILE *fp=fopen(filename,"w"); |
---|
| 42 | if (fp==NULL) |
---|
| 43 | { |
---|
| 44 | Print("cannot open %s\n",filename); |
---|
[4e08f55] | 45 | FreeL(filename); |
---|
[ad4bc9] | 46 | return; |
---|
| 47 | } |
---|
| 48 | if (pi->language!= LANG_SINGULAR) |
---|
| 49 | { |
---|
| 50 | Print("cannot edit type %d\n",pi->language); |
---|
| 51 | fclose(fp); |
---|
| 52 | fp=NULL; |
---|
| 53 | } |
---|
| 54 | else |
---|
| 55 | { |
---|
| 56 | char *editor=getenv("EDITOR"); |
---|
| 57 | if (editor==NULL) |
---|
| 58 | editor=getenv("VISUAL"); |
---|
| 59 | if (editor==NULL) |
---|
| 60 | editor="vi"; |
---|
[3375e2] | 61 | editor=mstrdup(editor); |
---|
| 62 | |
---|
[ad4bc9] | 63 | if (pi->data.s.body==NULL) |
---|
| 64 | { |
---|
| 65 | iiGetLibProcBuffer(pi); |
---|
| 66 | if (pi->data.s.body==NULL) |
---|
| 67 | { |
---|
| 68 | PrintS("cannot get the procedure body\n"); |
---|
| 69 | fclose(fp); |
---|
| 70 | unlink(filename); |
---|
[4e08f55] | 71 | FreeL(filename); |
---|
[ad4bc9] | 72 | return; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | fwrite(pi->data.s.body,1,strlen(pi->data.s.body),fp); |
---|
| 77 | fclose(fp); |
---|
| 78 | |
---|
| 79 | int pid=fork(); |
---|
| 80 | if (pid!=0) |
---|
| 81 | { |
---|
| 82 | wait(&pid); |
---|
| 83 | } |
---|
| 84 | else if(pid==0) |
---|
| 85 | { |
---|
[dfd346] | 86 | if (strchr(editor,' ')==NULL) |
---|
| 87 | { |
---|
| 88 | execlp(editor,editor,filename,NULL); |
---|
| 89 | Print("cannot exec %s\n",editor); |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | char *p=(char *)Alloc(strlen(editor)+strlen(filename)+2); |
---|
| 94 | sprintf(s,"%s %s",editor,filename); |
---|
| 95 | system(s); |
---|
| 96 | } |
---|
[3375e2] | 97 | exit(0); |
---|
[ad4bc9] | 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
| 101 | PrintS("cannot fork\n"); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | fp=fopen(filename,"r"); |
---|
| 105 | if (fp==NULL) |
---|
| 106 | { |
---|
| 107 | Print("cannot read from %s\n",filename); |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | fseek(fp,0L,SEEK_END); |
---|
| 112 | long len=ftell(fp); |
---|
| 113 | fseek(fp,0L,SEEK_SET); |
---|
| 114 | |
---|
| 115 | FreeL((ADDRESS)pi->data.s.body); |
---|
| 116 | pi->data.s.body=(char *)AllocL((int)len+1); |
---|
| 117 | myfread( pi->data.s.body, len, 1, fp); |
---|
| 118 | pi->data.s.body[len]='\0'; |
---|
| 119 | fclose(fp); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | unlink(filename); |
---|
[4e08f55] | 123 | FreeL(filename); |
---|
[ad4bc9] | 124 | } |
---|
| 125 | |
---|
| 126 | static char *sdb_find_arg(char *p) |
---|
| 127 | { |
---|
| 128 | p++; |
---|
| 129 | while (*p==' ') p++; |
---|
| 130 | char *pp=p; |
---|
| 131 | while (*pp>' ') pp++; |
---|
| 132 | *pp='\0'; |
---|
| 133 | return p; |
---|
| 134 | } |
---|
| 135 | |
---|
[8dd3ac] | 136 | static char sdb_lastcmd='c'; |
---|
| 137 | |
---|
[ad4bc9] | 138 | void sdb(Voice * currentVoice, const char * currLine, int len) |
---|
[dd9b83] | 139 | { |
---|
[8dd3ac] | 140 | int bp=0; |
---|
| 141 | if ((len>1) |
---|
| 142 | && ((currentVoice->pi->trace_flag & 1) |
---|
| 143 | || (bp=sdb_checkline(currentVoice->pi->trace_flag))) |
---|
| 144 | ) |
---|
[dd9b83] | 145 | { |
---|
[8dd3ac] | 146 | loop |
---|
[dd9b83] | 147 | { |
---|
[8dd3ac] | 148 | char gdb[80]; |
---|
[ad4bc9] | 149 | char *p=(char *)currLine+len-1; |
---|
[8dd3ac] | 150 | while ((*p<=' ') && (p!=currLine)) |
---|
| 151 | { |
---|
| 152 | p--; len--; |
---|
| 153 | } |
---|
| 154 | if (p==currLine) return; |
---|
| 155 | |
---|
| 156 | currentVoice->pi->trace_flag&= ~1; // delete flag for "all lines" |
---|
[ad4bc9] | 157 | Print("(%s,%d) >>",currentVoice->filename,yylineno); |
---|
[dd9b83] | 158 | fwrite(currLine,1,len,stdout); |
---|
[ad4bc9] | 159 | Print("<<\nbreakpoint %d (press ? for list of commands)\n",bp); |
---|
[3375e2] | 160 | p=fe_fgets_stdin(">>",gdb,80); |
---|
[dd9b83] | 161 | while (*p==' ') p++; |
---|
| 162 | if (*p >' ') |
---|
| 163 | { |
---|
[8dd3ac] | 164 | sdb_lastcmd=*p; |
---|
| 165 | } |
---|
[ad4bc9] | 166 | Print("command:%c\n",sdb_lastcmd); |
---|
[8dd3ac] | 167 | switch(sdb_lastcmd) |
---|
| 168 | { |
---|
[ad4bc9] | 169 | case '?': |
---|
| 170 | case 'h': |
---|
| 171 | { |
---|
| 172 | PrintS( |
---|
| 173 | "b - print backtrace of calling stack\n" |
---|
| 174 | "c - continue\n" |
---|
| 175 | "d - delete current breakpoint\n" |
---|
| 176 | "e - edit the current procedure (current call will be aborted)\n" |
---|
| 177 | "h,? - display this help screen\n" |
---|
| 178 | "n - execute current line, break at next line\n" |
---|
| 179 | "p <var> - display type and value of the variable <var>\n" |
---|
| 180 | "q <flags> - quit debugger, set debugger flags(0,1,2)\n" |
---|
| 181 | "Q - quit Singular\n"); |
---|
| 182 | int i; |
---|
| 183 | for(i=0;i<7;i++) |
---|
| 184 | { |
---|
| 185 | if (sdb_lines[i] != -1) |
---|
| 186 | Print("breakpoint %d at line %d in %s\n", |
---|
| 187 | i,sdb_lines[i],sdb_files[i]); |
---|
| 188 | } |
---|
| 189 | break; |
---|
| 190 | } |
---|
[8dd3ac] | 191 | case 'd': |
---|
[743c32] | 192 | { |
---|
[ad4bc9] | 193 | Print("delete break point %d\n",bp); |
---|
[8dd3ac] | 194 | currentVoice->pi->trace_flag &= (~Sy_bit(bp)); |
---|
[743c32] | 195 | if (bp!=0) |
---|
| 196 | { |
---|
| 197 | sdb_lines[bp-1]=-1; |
---|
| 198 | } |
---|
[8dd3ac] | 199 | break; |
---|
[743c32] | 200 | } |
---|
[8dd3ac] | 201 | case 'n': |
---|
| 202 | currentVoice->pi->trace_flag|= 1; |
---|
[dd9b83] | 203 | return; |
---|
[743c32] | 204 | case 'e': |
---|
| 205 | { |
---|
[ad4bc9] | 206 | sdb_edit(currentVoice->pi); |
---|
| 207 | sdb_flags=2; |
---|
[743c32] | 208 | return; |
---|
| 209 | } |
---|
[dd9b83] | 210 | case 'p': |
---|
[8dd3ac] | 211 | { |
---|
[ad4bc9] | 212 | p=sdb_find_arg(p); |
---|
| 213 | Print("request `%s`",p); |
---|
[8dd3ac] | 214 | idhdl h=ggetid(p,TRUE); |
---|
[ad4bc9] | 215 | if (h==NULL) |
---|
| 216 | PrintS("NULL\n"); |
---|
[8dd3ac] | 217 | else |
---|
| 218 | { |
---|
| 219 | sleftv tmp; |
---|
| 220 | memset(&tmp,0,sizeof(tmp)); |
---|
| 221 | tmp.rtyp=IDHDL; |
---|
| 222 | tmp.data=h; |
---|
[ad4bc9] | 223 | Print("(type %s):",Tok2Cmdname(tmp.Typ())); |
---|
[8dd3ac] | 224 | tmp.Print(); |
---|
| 225 | } |
---|
[dd9b83] | 226 | break; |
---|
[8dd3ac] | 227 | } |
---|
[dd9b83] | 228 | case 'b': |
---|
| 229 | VoiceBackTrack(); |
---|
[8dd3ac] | 230 | break; |
---|
[dd9b83] | 231 | case 'q': |
---|
[ad4bc9] | 232 | { |
---|
| 233 | p=sdb_find_arg(p); |
---|
| 234 | if (*p!='\0') |
---|
| 235 | { |
---|
| 236 | sdb_flags=atoi(p); |
---|
| 237 | Print("new sdb_flags:%d\n",sdb_flags); |
---|
| 238 | } |
---|
| 239 | return; |
---|
| 240 | } |
---|
| 241 | case 'Q': |
---|
[dd9b83] | 242 | m2_end(999); |
---|
[8dd3ac] | 243 | case 'c': |
---|
[dd9b83] | 244 | default: |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|