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