source: git/Singular/sdb.cc @ 4e08f55

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