source: git/Singular/sdb.cc @ 60763e

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