debugger abort a running execution fixed
[scilab.git] / scilab / modules / ast / src / cpp / ast / debuggervisitor.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 #include "debuggervisitor.hxx"
17 #include "debugmanager.hxx"
18 #include "printvisitor.hxx"
19 #include "execvisitor.hxx"
20 #include "threadId.hxx"
21 #include "macrofile.hxx"
22 #include "commentexp.hxx"
23 #include "UTF8.hxx"
24
25 extern "C"
26 {
27 #include "filemanager_interface.h"
28 #include "FileExist.h"
29 }
30
31 static bool getMacroSourceFile(std::string* filename = nullptr);
32
33 namespace ast
34 {
35 void DebuggerVisitor::visit(const SeqExp  &e)
36 {
37     std::list<Exp *>::const_iterator itExp;
38
39     debugger::DebuggerManager* manager = debugger::DebuggerManager::getInstance();
40     if(manager->isAborted())
41     {
42         // abort a running execution
43         throw ast::InternalAbort();
44     }
45
46     for (const auto & exp : e.getExps())
47     {
48         if (exp->isCommentExp())
49         {
50             continue;
51         }
52
53         if (e.isBreakable())
54         {
55             exp->resetBreak();
56             exp->setBreakable();
57         }
58
59         if (e.isContinuable())
60         {
61             exp->resetContinue();
62             exp->setContinuable();
63         }
64
65         if (e.isReturnable())
66         {
67             exp->setReturnable();
68         }
69
70         //debugger check !
71         int iBreakPoint = -1;
72         if (ConfigVariable::getEnableDebug())
73         {
74             bool stopExecution = false;
75             if (manager->isStepIn())
76             {
77                 manager->resetStepIn();
78                 stopExecution = true;
79                 if(getMacroSourceFile() == false)
80                 {
81                     stopExecution = false;
82                     manager->setStepIn();
83                 }
84             }
85             else if (manager->isStepNext())
86             {
87                 manager->resetStepNext();
88                 stopExecution = true;
89                 if(getMacroSourceFile() == false)
90                 {
91                     stopExecution = false;
92                     manager->setStepOut();
93                 }
94             }
95             else
96             {
97                 const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
98                 //set information from debugger commands
99                 if (lWhereAmI.size() != 0 && manager->getBreakPointCount() != 0)
100                 {
101                     debugger::Breakpoints& bps = manager->getAllBreakPoint();
102
103                     int i = -1;
104                     for (const auto & bp : bps)
105                     {
106                         ++i;
107                         if (bp->isEnable() == false)
108                         {
109                             continue;
110                         }
111
112                         // look for a breakpoint on this line and update breakpoint information when possible
113                         int iLine = exp->getLocation().first_line - ConfigVariable::getMacroFirstLines();
114                         if (bp->hasMacro())
115                         {
116                             char* functionName = wide_string_to_UTF8(lWhereAmI.back().call->getName().data());
117                             if (bp->getFunctioName().compare(functionName) == 0)
118                             {
119                                 if (bp->getMacroLine() == 0)
120                                 {
121                                     //first pass in macro.
122                                     //update first line with real value
123                                     bp->setMacroLine(iLine);
124                                 }
125
126                                 stopExecution = bp->getMacroLine() == iLine;
127                             }
128
129                             FREE(functionName);
130                         }
131                         else if (bp->hasFile())
132                         {
133                             if (bp->getFileLine() == exp->getLocation().first_line)
134                             {
135                                 std::string pFileName;
136                                 bool hasSource = getMacroSourceFile(&pFileName);
137                                 if (hasSource && bp->getFileName() == pFileName)
138                                 {
139                                     stopExecution = true;
140                                     // set function information
141                                     if (lWhereAmI.back().call->getFirstLine())
142                                     {
143                                         bp->setFunctionName(scilab::UTF8::toUTF8(lWhereAmI.back().call->getName()));
144                                         bp->setMacroLine(iLine);
145                                     }
146                                 }
147                             }
148                         }
149
150                         if(stopExecution == false)
151                         {
152                             // no breakpoint for this line
153                             continue;
154                         }
155
156                         // Set the begin of line if not yet done
157                         if(bp->getBeginLine() == 0)
158                         {
159                             bp->setBeginLine(exp->getLocation().first_column);
160                         }
161                         // check if this exp is at the begin of the breakpoint line
162                         else if(bp->getBeginLine() != exp->getLocation().first_column)
163                         {
164                             // stop only if we are at the line begins
165                             stopExecution = false;
166                             continue;
167                         }
168
169                         //check condition
170                         if (bp->getConditionExp() != NULL)
171                         {
172                             //do not use debuggervisitor !
173                             symbol::Context* pCtx = symbol::Context::getInstance();
174                             try
175                             {
176                                 ExecVisitor execCond;
177                                 //protect current env during condition execution
178                                 pCtx->scope_begin();
179                                 bp->getConditionExp()->accept(execCond);
180                                 types::InternalType* pIT = pCtx->getCurrentLevel(symbol::Symbol(L"ans"));
181                                 if (pIT == NULL ||
182                                         pIT->isBool() == false ||
183                                         ((types::Bool*)pIT)->isScalar() == false ||
184                                         ((types::Bool*)pIT)->get(0) == 0)
185                                 {
186                                     pCtx->scope_end();
187                                     //not a boolean, not scalar or false
188                                     stopExecution = false;
189                                     continue;
190                                 }
191
192                                 pCtx->scope_end();
193                                 //ok condition is valid and true
194                             }
195                             catch (ast::ScilabException &/*e*/)
196                             {
197                                 pCtx->scope_end();
198                                 stopExecution = false;
199                                 //not work !
200                                 //invalid breakpoint
201                                 continue;
202                             }
203                         }
204
205                         //we have a breakpoint !
206                         //stop execution and wait signal from debugger to restart
207                         iBreakPoint = i;
208
209                         //only one breakpoint can be "call" on same exp
210                         break;
211                     }
212                 }
213             }
214
215             if(stopExecution)
216             {
217                 manager->stop(exp, iBreakPoint);
218                 if (manager->isAborted())
219                 {
220                     throw ast::InternalAbort();
221                 }
222             }
223         }
224
225         //copy from runvisitor::seqexp
226         try
227         {
228             //reset default values
229             setResult(NULL);
230             int iExpectedSize = getExpectedSize();
231             setExpectedSize(-1);
232             exp->accept(*this);
233             setExpectedSize(iExpectedSize);
234             types::InternalType * pIT = getResult();
235
236             // In case of exec file, set the file name in the Macro to store where it is defined.
237             int iFileID = ConfigVariable::getExecutedFileID();
238             if (iFileID && exp->isFunctionDec())
239             {
240                 types::InternalType* pITMacro = symbol::Context::getInstance()->get(exp->getAs<ast::FunctionDec>()->getSymbol());
241                 if (pITMacro)
242                 {
243                     types::Macro* pMacro = pITMacro->getAs<types::Macro>();
244                     const wchar_t* filename = getfile_filename(iFileID);
245                     // scilab.quit is not open with mopen
246                     // in this case filename is NULL because FileManager have not been filled.
247                     if (filename)
248                     {
249                         pMacro->setFileName(filename);
250                     }
251                 }
252             }
253
254             if (pIT != NULL)
255             {
256                 bool bImplicitCall = false;
257                 if (pIT->isCallable()) //to manage call without ()
258                 {
259                     types::Callable *pCall = pIT->getAs<types::Callable>();
260                     types::typed_list out;
261                     types::typed_list in;
262                     types::optional_list opt;
263
264                     try
265                     {
266                         //in this case of calling, we can return only one value
267                         int iSaveExpectedSize = getExpectedSize();
268                         setExpectedSize(1);
269
270                         pCall->invoke(in, opt, getExpectedSize(), out, *exp);
271                         setExpectedSize(iSaveExpectedSize);
272
273                         if (out.size() == 0)
274                         {
275                             setResult(NULL);
276                         }
277                         else
278                         {
279                             setResult(out[0]);
280                         }
281
282                         bImplicitCall = true;
283                     }
284                     catch (const InternalError& ie)
285                     {
286                         if (ConfigVariable::getLastErrorFunction() == L"")
287                         {
288                             ConfigVariable::setLastErrorFunction(pCall->getName());
289                             ConfigVariable::setLastErrorLine(e.getLocation().first_line);
290                         }
291
292                         throw ie;
293                     }
294                 }
295
296                 //don't output Simplevar and empty result
297                 if (getResult() != NULL && (!exp->isSimpleVar() || bImplicitCall))
298                 {
299                     //symbol::Context::getInstance()->put(symbol::Symbol(L"ans"), *execMe.getResult());
300                     types::InternalType* pITAns = getResult();
301                     symbol::Context::getInstance()->put(m_pAns, pITAns);
302                     if (exp->isVerbose() && ConfigVariable::isPrintOutput())
303                     {
304                         //TODO manage multiple returns
305                         scilabWriteW(L" ans  =\n\n");
306                         std::wostringstream ostrName;
307                         ostrName << L"ans";
308                         types::VariableToString(pITAns, ostrName.str().c_str());
309                     }
310                 }
311
312                 pIT->killMe();
313             }
314
315             if ((&e)->isBreakable() && exp->isBreak())
316             {
317                 const_cast<SeqExp *>(&e)->setBreak();
318                 exp->resetBreak();
319                 break;
320             }
321
322             if ((&e)->isContinuable() && exp->isContinue())
323             {
324                 const_cast<SeqExp *>(&e)->setContinue();
325                 exp->resetContinue();
326                 break;
327             }
328
329             if ((&e)->isReturnable() && exp->isReturn())
330             {
331                 const_cast<SeqExp *>(&e)->setReturn();
332                 exp->resetReturn();
333                 break;
334             }
335
336             // Stop execution at the end of the seqexp of the caller
337             // Do it at the end of the seqexp will make the debugger stop
338             // even if the caller is at the last line
339             // ie: the caller is followed by endfunction
340             if(manager->isStepOut())
341             {
342                 manager->resetStepOut();
343                 if(getMacroSourceFile() == false)
344                 {
345                     // no sources
346                     manager->setStepOut();
347                 }
348                 else
349                 {
350                     manager->stop(exp, iBreakPoint);
351                 }
352
353                 if (manager->isAborted())
354                 {
355                     throw ast::InternalAbort();
356                 }
357             }
358
359         }
360         catch (const InternalError& ie)
361         {
362             ConfigVariable::fillWhereError(ie.GetErrorLocation().first_line);
363
364             const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
365
366             //where can be empty on last stepout, on first calling expression
367             if (lWhereAmI.size())
368             {
369                 const std::wstring* filename = lWhereAmI.back().m_file_name;
370
371                 if (filename == nullptr)
372                 {
373                     //error in a console script
374                     std::wstring functionName = lWhereAmI.back().call->getName();
375                     manager->errorInScript(functionName, exp);
376                 }
377                 else
378                 {
379                     manager->errorInFile(*filename, exp);
380                 }
381
382                 // Debugger just restart after been stopped on an error.
383                 if (manager->isAborted())
384                 {
385                     throw ast::InternalAbort();
386                 }
387             }
388
389             throw ie;
390         }
391
392         // If something other than NULL is given to setResult, then that would imply
393         // to make a cleanup in visit(ForExp) for example (e.getBody().accept(*this);)
394         setResult(NULL);
395
396     }
397
398     if (e.getParent() == NULL && e.getExecFrom() == SeqExp::SCRIPT && (manager->isStepNext() || manager->isStepIn()))
399     {
400         const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
401         if (lWhereAmI.size())
402         {
403             std::wstring functionName = lWhereAmI.back().call->getName();
404             types::InternalType* pIT = symbol::Context::getInstance()->get(symbol::Symbol(functionName));
405             if (pIT && (pIT->isMacro() || pIT->isMacroFile()))
406             {
407                 types::Macro* m = nullptr;
408                 if (pIT->isMacroFile())
409                 {
410                     types::MacroFile* mf = pIT->getAs<types::MacroFile>();
411                     m = mf->getMacro();
412                 }
413                 else
414                 {
415                     m = pIT->getAs<types::Macro>();
416                 }
417
418                 //create a fake exp to represente end/enfunction
419
420                 //will be deleted by CommentExp
421                 std::wstring* comment = new std::wstring(L"end of function");
422                 Location loc(m->getLastLine(), m->getLastLine(), 0, 0);
423                 CommentExp fakeExp(loc, comment);
424                 manager->stop(&fakeExp, -1);
425
426                 if (manager->isAborted())
427                 {
428                     throw ast::InternalAbort();
429                 }
430
431                 //transform stepnext after endfunction as a stepout to show line marker on current statement
432                 if (manager->isStepNext())
433                 {
434                     manager->resetStepNext();
435                     manager->setStepOut();
436                 }
437                 else if (manager->isStepIn())
438                 {
439                     manager->resetStepIn();
440                     manager->setStepOut();
441                 }
442             }
443         }
444     }
445 }
446 }
447
448 // return false if a file .sci of a file .bin doesn't exists
449 // return true for others files or existing .sci
450 bool getMacroSourceFile(std::string* filename)
451 {
452     const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
453     std::string file = scilab::UTF8::toUTF8(*lWhereAmI.back().m_file_name);
454     if (file.rfind(".bin") != std::string::npos)
455     {
456         file.replace(file.size() - 4, 4, ".sci");
457         // stop on bp only if the file exist
458         if (!FileExist(file.data()))
459         {
460             return false;
461         }
462     }
463
464     if(filename != nullptr)
465     {
466         filename->assign(file);
467     }
468
469     return true;
470 }