roxiedebug.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "jexcept.hpp"
  15. #include "roxiehelper.hpp"
  16. #include "roxiedebug.hpp"
  17. #include "portlist.h"
  18. #include "eclrtl.hpp"
  19. #include "deftype.hpp"
  20. #include "eclhelper.hpp"
  21. #include "thorcommon.hpp"
  22. //=======================================================================================
  23. bool CDebugCommandHandler::checkCommand(IXmlWriter &out, const char *&supplied, const char *expected)
  24. {
  25. unsigned i = 0;
  26. loop
  27. {
  28. if (!supplied[i])
  29. {
  30. out.outputBeginNested(expected, true);
  31. supplied = expected;
  32. return true;
  33. }
  34. if (!expected[i])
  35. return false;
  36. if (supplied[i] != expected[i])
  37. return false;
  38. i++;
  39. }
  40. }
  41. void CDebugCommandHandler::doDebugCommand(IPropertyTree *query, IDebuggerContext *debugContext, FlushingStringBuffer &output)
  42. {
  43. const char *commandName = query->queryName();
  44. CommonXmlWriter out(0, 1);
  45. if (strnicmp(commandName, "b", 1)==0 && checkCommand(out, commandName, "breakpoint"))
  46. {
  47. const char *mode = query->queryProp("@mode");
  48. const char *id = query->queryProp("@id");
  49. const char *action = query->queryProp("@action");
  50. const char *fieldName = query->queryProp("@fieldName");
  51. const char *condition = query->queryProp("@condition");
  52. bool caseSensitive = query->getPropBool("@caseSensitive", false);
  53. const char *value = query->queryProp("@value");
  54. unsigned rowCount = query->getPropInt("@rowCount", 0);
  55. const char *rowCountMode = query->queryProp("@rowCountMode");
  56. debugContext->addBreakpoint(&out, mode, id, action, fieldName, condition, value, caseSensitive, rowCount, rowCountMode);
  57. }
  58. else if (strnicmp(commandName, "c", 1)==0 && checkCommand(out, commandName, "continue"))
  59. {
  60. const char *mode = query->queryProp("@mode");
  61. const char *id = query->queryProp("@id");
  62. debugContext->debugContinue(&out, mode, id);
  63. }
  64. else if (strnicmp(commandName, "c", 1)==0 && checkCommand(out, commandName, "changes"))
  65. {
  66. unsigned sequence = query->getPropInt("@sequence", -1);
  67. debugContext->debugChanges(&out, sequence);
  68. }
  69. else if (strnicmp(commandName, "c", 1)==0 && checkCommand(out, commandName, "counts"))
  70. {
  71. // MORE It could be argued that this is really more a "changes" with a "global" vs "active" option....
  72. unsigned sequence = query->getPropInt("@sequence", -1);
  73. debugContext->debugCounts(&out, sequence, false);
  74. }
  75. else if (strnicmp(commandName, "d", 1)==0 && checkCommand(out, commandName, "delete"))
  76. {
  77. const char *idx = query->queryProp("@idx");
  78. if (!idx)
  79. throw MakeStringException(ROXIE_DEBUG_ERROR, "delete must specify breakpoint index, or all");
  80. if (stricmp(idx, "all")==0)
  81. debugContext->removeAllBreakpoints(&out);
  82. else
  83. {
  84. char *ep;
  85. unsigned bpIdx = strtoul(idx, &ep, 10);
  86. if (ep != idx && *ep==0)
  87. {
  88. if (bpIdx)
  89. debugContext->removeBreakpoint(&out, bpIdx);
  90. else
  91. debugContext->removeAllBreakpoints(&out);
  92. }
  93. else
  94. throw MakeStringException(ROXIE_DEBUG_ERROR, "Invalid value for idx - expected number or all");
  95. }
  96. }
  97. else if (strnicmp(commandName, "g", 1)==0 && checkCommand(out, commandName, "graph"))
  98. {
  99. const char *graphName = query->queryProp("@name");
  100. if (!graphName)
  101. {
  102. bool original = query->getPropBool("@original", true);
  103. debugContext->getCurrentGraphXGMML(&out, original);
  104. }
  105. else if (stricmp(graphName, "all")==0)
  106. debugContext->getQueryXGMML(&out);
  107. else
  108. debugContext->getGraphXGMML(&out, graphName);
  109. }
  110. else if (strnicmp(commandName, "g", 1)==0 && checkCommand(out, commandName, "get"))
  111. {
  112. const char *name = query->queryProp("@name");
  113. const char *id = query->queryProp("@id");
  114. debugContext->debugGetConfig(&out, name, id);
  115. }
  116. else if (strnicmp(commandName, "i", 1)==0 && checkCommand(out, commandName, "interrupt"))
  117. {
  118. debugContext->debugInterrupt(&out);
  119. }
  120. else if (strnicmp(commandName, "l", 1)==0 && checkCommand(out, commandName, "list"))
  121. {
  122. const char *idx = query->queryProp("@idx");
  123. if (!idx || strcmp(idx, "all")==0)
  124. debugContext->listAllBreakpoints(&out);
  125. else
  126. {
  127. char *ep;
  128. unsigned bpIdx = strtoul(idx, &ep, 10);
  129. if (ep != idx && *ep==0)
  130. debugContext->listBreakpoint(&out, bpIdx);
  131. else
  132. throw MakeStringException(ROXIE_DEBUG_ERROR, "Invalid value for idx - expected number or all");
  133. }
  134. }
  135. else if (strnicmp(commandName, "n", 1)==0 && checkCommand(out, commandName, "next"))
  136. {
  137. debugContext->debugNext(&out);
  138. }
  139. else if (strnicmp(commandName, "o", 1)==0 && checkCommand(out, commandName, "over"))
  140. {
  141. debugContext->debugOver(&out);
  142. }
  143. else if (strnicmp(commandName, "p", 1)==0 && checkCommand(out, commandName, "print"))
  144. {
  145. const char *edgeId = query->queryProp("@edgeId");
  146. unsigned numRows = query->getPropInt("@numRows", 1);
  147. unsigned startRow= query->getPropInt("@startRow", 0);
  148. debugContext->debugPrint(&out, edgeId, startRow, numRows);
  149. }
  150. else if (strnicmp(commandName, "q", 1)==0 && checkCommand(out, commandName, "quit"))
  151. {
  152. debugContext->debugQuit(&out);
  153. }
  154. else if (strnicmp(commandName, "r", 1)==0 && checkCommand(out, commandName, "run"))
  155. {
  156. debugContext->debugRun(&out);
  157. }
  158. else if (strnicmp(commandName, "s", 1)==0 && checkCommand(out, commandName, "step"))
  159. {
  160. const char *mode = query->queryProp("@mode");
  161. debugContext->debugStep(&out, mode);
  162. }
  163. else if (strnicmp(commandName, "s", 1)==0 && checkCommand(out, commandName, "status"))
  164. {
  165. debugContext->debugStatus(&out);
  166. }
  167. else if (strnicmp(commandName, "s", 1)==0 && checkCommand(out, commandName, "skip"))
  168. {
  169. debugContext->debugSkip(&out);
  170. }
  171. else if (strnicmp(commandName, "s", 1)==0 && checkCommand(out, commandName, "search"))
  172. {
  173. const char *fieldName = query->queryProp("@fieldName");
  174. const char *condition = query->queryProp("@condition");
  175. if (!condition)
  176. condition="contains";
  177. const char *value = query->queryProp("@value");
  178. bool caseSensitive = query->getPropBool("@caseSensitive", false);
  179. bool fullRows = query->getPropBool("@fullRows", false);
  180. debugContext->debugSearch(&out, fieldName, condition, value, caseSensitive, fullRows);
  181. }
  182. else if (strnicmp(commandName, "s", 1)==0 && checkCommand(out, commandName, "set"))
  183. {
  184. const char *name = query->queryProp("@name");
  185. const char *value = query->queryProp("@value");
  186. const char *id = query->queryProp("@id");
  187. debugContext->debugSetConfig(&out, name, value, id);
  188. }
  189. else if (strnicmp(commandName, "v", 1)==0 && checkCommand(out, commandName, "variable"))
  190. {
  191. const char *name = query->queryProp("@name");
  192. const char *type = query->queryProp("@type");
  193. debugContext->debugPrintVariable(&out, name, type);
  194. }
  195. else if (strnicmp(commandName, "w", 1)==0 && checkCommand(out, commandName, "where"))
  196. {
  197. debugContext->debugWhere(&out);
  198. }
  199. else
  200. throw MakeStringException(ROXIE_DEBUG_ERROR, "Unknown command %s", commandName);
  201. out.outputEndNested(commandName);
  202. output.append(out.str());
  203. }
  204. //=======================================================================================
  205. MAKEValueArray(const RtlTypeInfo *, RtlTypeInfoArray);
  206. class SimpleFieldSearcher : public CInterface, implements IRowMatcher
  207. {
  208. protected:
  209. unsigned searchStringLength;
  210. const char *searchString;
  211. StringAttr expression;
  212. StringAttr searchFieldName;
  213. MemoryBuffer searchQString;
  214. bool matchSeen;
  215. bool boolValueSet;
  216. bool intValueSet;
  217. bool uintValueSet;
  218. bool realValueSet;
  219. bool decimalValueSet;
  220. bool udecimalValueSet;
  221. bool qstringValueSet;
  222. bool boolValue;
  223. __int64 intValue;
  224. double realValue;
  225. BreakpointConditionMode mode;
  226. bool caseSensitive;
  227. RtlTypeInfoArray recordTypesCanMatch;
  228. RtlTypeInfoArray recordTypesCannotMatch;
  229. bool checkCondition(int diff)
  230. {
  231. switch (mode)
  232. {
  233. case BreakpointConditionLess:
  234. return (diff < 0);
  235. case BreakpointConditionLessEqual:
  236. return (diff <= 0);
  237. case BreakpointConditionGreater:
  238. return (diff > 0);
  239. case BreakpointConditionGreaterEqual:
  240. return (diff >= 0);
  241. case BreakpointConditionNotEqual:
  242. return (diff != 0);
  243. default:
  244. return (diff == 0);
  245. }
  246. }
  247. inline bool checkFieldName(const char *fieldname)
  248. {
  249. return searchFieldName.isEmpty() || stricmp(searchFieldName, fieldname)==0;
  250. }
  251. public:
  252. SimpleFieldSearcher(const char *_searchFieldName, const char *_expression, BreakpointConditionMode _mode, bool _caseSensitive)
  253. : searchFieldName(_searchFieldName), expression(_expression), mode(_mode), caseSensitive(_caseSensitive)
  254. {
  255. searchString = expression.get();
  256. searchStringLength = expression.length();
  257. matchSeen = false;
  258. boolValueSet = false;
  259. intValueSet = false;
  260. uintValueSet = false;
  261. realValueSet = false;
  262. decimalValueSet = false;
  263. udecimalValueSet = false;
  264. qstringValueSet = false;
  265. if (searchString)
  266. {
  267. if (stricmp(searchString, "true")==0)
  268. boolValueSet = boolValue = true;
  269. else if (stricmp(searchString, "false")==0)
  270. {
  271. boolValueSet = true; boolValue = false;
  272. }
  273. else
  274. {
  275. // MORE - should really support decimal, real, and perhaps esoteric forms of integer (0x.... etc) here....
  276. const char *v = searchString;
  277. bool isNegative = false;
  278. while (isspace(*v))
  279. v++;
  280. if (*v=='-')
  281. {
  282. isNegative = true;
  283. v++;
  284. }
  285. if (isdigit(*v))
  286. {
  287. intValue = 0;
  288. char c;
  289. loop
  290. {
  291. c = *v++;
  292. if ((c >= '0') && (c <= '9'))
  293. intValue = intValue * 10 + (c-'0');
  294. else
  295. break;
  296. }
  297. switch (c)
  298. {
  299. case ' ':
  300. while (isspace(c = *v))
  301. v++;
  302. if (c)
  303. break;
  304. // fall into...
  305. case '\0':
  306. intValueSet = true;
  307. if (isNegative)
  308. intValue = -intValue;
  309. else
  310. uintValueSet = true;
  311. break;
  312. // MORE - want something like...
  313. // case '.':
  314. // TempDecimal.setString().getDecimal()
  315. }
  316. }
  317. }
  318. }
  319. }
  320. virtual bool matched() const { return matchSeen; }
  321. virtual void reset() { matchSeen = false; }
  322. virtual const char *queryFieldName() const { return searchFieldName; }
  323. virtual const char *queryValue() const { return expression; }
  324. virtual bool queryCaseSensitive() const { return caseSensitive; }
  325. virtual void serialize(MemoryBuffer &out) const
  326. {
  327. out.append(searchFieldName).append((char) mode).append(expression).append(caseSensitive);
  328. }
  329. bool canMatchAny(const RtlTypeInfo *recordType)
  330. {
  331. bool sawMatch = false;
  332. ForEachItemIn (idx1, recordTypesCanMatch)
  333. {
  334. if (recordTypesCanMatch.item(idx1) == recordType)
  335. {
  336. if (idx1)
  337. recordTypesCanMatch.swap(0, idx1);
  338. return true;
  339. }
  340. }
  341. ForEachItemIn (idx2, recordTypesCannotMatch)
  342. {
  343. if (recordTypesCannotMatch.item(idx2) == recordType)
  344. {
  345. if (idx2)
  346. recordTypesCannotMatch.swap(0, idx2);
  347. return false;
  348. }
  349. }
  350. const RtlFieldInfo * const * fields = recordType->queryFields();
  351. assertex(fields);
  352. while (*fields)
  353. {
  354. const RtlTypeInfo *childType = fields[0]->type->queryChildType();
  355. if (childType)
  356. {
  357. // MORE - get funky with fieldname.fieldname ? Possible future feature....
  358. if (canMatchAny(childType))
  359. sawMatch = true;
  360. }
  361. if (stricmp(fields[0]->name->getAtomNamePtr(), searchFieldName)==0)
  362. sawMatch = true;
  363. fields++;
  364. }
  365. if (sawMatch)
  366. recordTypesCanMatch.add(recordType, 0);
  367. else
  368. recordTypesCannotMatch.add(recordType, 0);
  369. return sawMatch;
  370. }
  371. virtual bool canMatchAny(IOutputMetaData *meta)
  372. {
  373. bool canMatch = true; // safest assumption...
  374. if (searchFieldName.length())
  375. {
  376. const RtlTypeInfo *typeInfo = meta->queryTypeInfo();
  377. assertex(typeInfo);
  378. if (!canMatchAny(typeInfo))
  379. canMatch = false;
  380. }
  381. return canMatch;
  382. }
  383. IMPLEMENT_IINTERFACE;
  384. virtual void outputQuoted(const char *text) { }
  385. virtual void outputString(unsigned len, const char *field, const char *fieldname)
  386. {
  387. if (!matchSeen && checkFieldName(fieldname) && (len >= searchStringLength))
  388. {
  389. int diff = caseSensitive ? memcmp(field,searchString,searchStringLength) : memicmp(field,searchString,searchStringLength);
  390. if (!diff) // little bit suboptimal as some modes don't care about difference between equal and gt - but who cares!
  391. {
  392. // if any remaining char is not space, it's gt
  393. unsigned i = searchStringLength;
  394. while (i < len)
  395. {
  396. if (field[i] != ' ')
  397. {
  398. diff++;
  399. break;
  400. }
  401. i++;
  402. }
  403. }
  404. matchSeen = checkCondition(diff);
  405. }
  406. }
  407. virtual void outputBool(bool field, const char *fieldname)
  408. {
  409. if (boolValueSet && !matchSeen && checkFieldName(fieldname))
  410. {
  411. matchSeen = checkCondition((int) field - (int) boolValue);
  412. }
  413. }
  414. virtual void outputData(unsigned len, const void *field, const char *fieldname)
  415. {
  416. // MORE
  417. }
  418. virtual void outputInt(__int64 field, const char *fieldname)
  419. {
  420. if (intValueSet && !matchSeen && checkFieldName(fieldname))
  421. {
  422. matchSeen = checkCondition((field == intValue) ? 0 : ((field > intValue) ? 1 : -1));
  423. }
  424. }
  425. virtual void outputUInt(unsigned __int64 field, const char *fieldname)
  426. {
  427. // NOTE - "contains" is interpreted as "equals" on numeric fields
  428. if (uintValueSet && !matchSeen && checkFieldName(fieldname))
  429. {
  430. matchSeen = checkCondition((field == intValue) ? 0 : ((field > (unsigned __int64) intValue) ? 1 : -1));
  431. }
  432. }
  433. virtual void outputReal(double field, const char *fieldname)
  434. {
  435. // NOTE - "contains" is interpreted as "equals" on numeric fields
  436. if (realValueSet && !matchSeen && checkFieldName(fieldname))
  437. {
  438. matchSeen = checkCondition((field == realValue) ? 0 : ((field > realValue) ? 1 : -1));
  439. }
  440. }
  441. virtual void outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  442. {
  443. // Searching/breaking on decimal not supported at the moment
  444. }
  445. virtual void outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  446. {
  447. // Searching/breaking on decimal not supported at the moment
  448. }
  449. virtual void outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  450. {
  451. // Searching/breaking on unicode not supported at the moment
  452. }
  453. virtual void outputQString(unsigned len, const char *field, const char *fieldname)
  454. {
  455. if (!matchSeen && checkFieldName(fieldname))
  456. {
  457. if (!qstringValueSet)
  458. {
  459. size32_t outlen;
  460. char *out;
  461. rtlStrToQStrX(outlen, out, searchStringLength, searchString);
  462. searchQString.setBuffer(outlen, out, true);
  463. }
  464. matchSeen = checkCondition(rtlCompareQStrQStr(len, field, searchQString.length(), searchQString.toByteArray()));
  465. }
  466. }
  467. virtual void outputUtf8(unsigned len, const char *field, const char *fieldname)
  468. {
  469. // Searching/breaking on unicode not supported at the moment
  470. }
  471. virtual void outputBeginNested(const char *fieldname, bool nestChildren)
  472. {
  473. // nothing for now
  474. }
  475. virtual void outputEndNested(const char *fieldname)
  476. {
  477. // nothing for now
  478. }
  479. virtual void outputSetAll()
  480. {
  481. // nothing for now
  482. }
  483. };
  484. class ContainsFieldSearcher : public SimpleFieldSearcher
  485. {
  486. // For most field types, just use equals version. Only meaningful for string types (and only implemented so far for plain ascii)
  487. public:
  488. ContainsFieldSearcher(const char *_searchFieldName, const char *_expression, bool _caseSensitive=true) : SimpleFieldSearcher(_searchFieldName, _expression, BreakpointConditionContains, _caseSensitive)
  489. {
  490. // This is where you set up Boyer-Moore tables...
  491. }
  492. virtual void outputString(unsigned len, const char *field, const char *fieldname)
  493. {
  494. if (!matchSeen && checkFieldName(fieldname) && (len >= searchStringLength)) // could swap those last two tests - last is faster but filters less out...
  495. {
  496. if (searchStringLength==1)
  497. {
  498. if (memchr(field, searchString[0], len))
  499. matchSeen = true;
  500. }
  501. else
  502. {
  503. unsigned steps = len-searchStringLength+1;
  504. for ( unsigned i = 0; i < steps; i++ )
  505. {
  506. if ( !memcmp((char *)field+i,searchString,searchStringLength) ) // MORE - Should I use Boyer-Moore? Probably. Is it worth it in (typically) small search targets?
  507. {
  508. matchSeen = true;
  509. return;
  510. }
  511. }
  512. }
  513. }
  514. }
  515. };
  516. class ContainsNoCaseFieldSearcher : public ContainsFieldSearcher
  517. {
  518. // For most field types, just use equals version. Only meaningful for string types (and only implemented so far for plain ascii)
  519. public:
  520. ContainsNoCaseFieldSearcher(const char *_searchFieldName, const char *_expression) : ContainsFieldSearcher(_searchFieldName, _expression, false)
  521. {
  522. }
  523. virtual void outputString(unsigned len, const char *field, const char *fieldname)
  524. {
  525. if (!matchSeen && checkFieldName(fieldname) && (len >= searchStringLength))
  526. {
  527. unsigned steps = len-searchStringLength+1;
  528. for ( unsigned i = 0; i < steps; i++ )
  529. {
  530. if ( !memicmp((char *)field+i,searchString,searchStringLength) )
  531. {
  532. matchSeen = true;
  533. return;
  534. }
  535. }
  536. }
  537. }
  538. };
  539. class StartsWithFieldSearcher : public SimpleFieldSearcher
  540. {
  541. // For most field types, just use equals version. Only meaningful for string types (and only implemented so far for plain ascii)
  542. public:
  543. StartsWithFieldSearcher(const char *_searchFieldName, const char *_expression, bool _caseSensitive) : SimpleFieldSearcher(_searchFieldName, _expression, BreakpointConditionStartsWith, _caseSensitive)
  544. {
  545. }
  546. virtual void outputString(unsigned len, const char *field, const char *fieldname)
  547. {
  548. if (!matchSeen && checkFieldName(fieldname) && (len >= searchStringLength))
  549. {
  550. int diff = caseSensitive ? memcmp(field,searchString,searchStringLength) : memicmp(field,searchString,searchStringLength);
  551. if (diff == 0)
  552. matchSeen = true;
  553. }
  554. }
  555. };
  556. extern IRowMatcher *createRowMatcher(const char *fieldName, BreakpointConditionMode condition, const char *value, bool caseSensitive)
  557. {
  558. switch (condition)
  559. {
  560. case BreakpointConditionNone:
  561. case BreakpointConditionEOG:
  562. case BreakpointConditionEOF:
  563. return NULL;
  564. case BreakpointConditionLess:
  565. case BreakpointConditionLessEqual:
  566. case BreakpointConditionGreater:
  567. case BreakpointConditionGreaterEqual:
  568. case BreakpointConditionNotEqual:
  569. case BreakpointConditionEquals:
  570. return new SimpleFieldSearcher(fieldName, value, condition, caseSensitive);
  571. case BreakpointConditionContains:
  572. if (caseSensitive)
  573. return new ContainsFieldSearcher(fieldName, value);
  574. else
  575. return new ContainsNoCaseFieldSearcher(fieldName, value);
  576. case BreakpointConditionStartsWith:
  577. return new StartsWithFieldSearcher(fieldName, value, caseSensitive);
  578. default:
  579. throwUnexpected();
  580. }
  581. }
  582. extern IRowMatcher *createRowMatcher(MemoryBuffer &serialized)
  583. {
  584. StringAttr fieldName;
  585. char condition;
  586. StringAttr value;
  587. bool caseSensitive;
  588. serialized.read(fieldName).read(condition).read(value).read(caseSensitive);
  589. return createRowMatcher(fieldName, (BreakpointConditionMode) condition, value, caseSensitive);
  590. }
  591. //===============================================================================================
  592. CBreakpointInfo::CBreakpointInfo(BreakpointMode _mode, const char *_id, BreakpointActionMode _action,
  593. const char *_fieldName, BreakpointConditionMode _condition, const char *_value, bool _caseSensitive,
  594. unsigned _rowCount, BreakpointCountMode _rowCountMode)
  595. : mode(_mode), id(_id), action(_action), condition(_condition),
  596. rowCount(_rowCount), rowCountMode(_rowCountMode)
  597. {
  598. uid = nextUID();
  599. rowMatcher.setown(createRowMatcher(_fieldName, _condition, _value, _caseSensitive));
  600. }
  601. CBreakpointInfo::CBreakpointInfo(BreakpointMode _mode) : mode(_mode)
  602. {
  603. uid = 0; // reserved for the dummy breakpoint
  604. action = BreakpointActionContinue;
  605. rowCount = 0;
  606. rowCountMode = BreakpointCountNone;
  607. condition = BreakpointConditionNone;
  608. }
  609. CBreakpointInfo::CBreakpointInfo(MemoryBuffer &from)
  610. {
  611. from.read(uid);
  612. from.read(id);
  613. char temp;
  614. from.read(temp); mode = (BreakpointMode) temp;
  615. from.read(temp); action = (BreakpointActionMode) temp;
  616. from.read(rowCount);
  617. from.read(temp); rowCountMode = (BreakpointCountMode) temp;
  618. from.read(temp); condition = (BreakpointConditionMode) temp;
  619. switch (condition)
  620. {
  621. case BreakpointConditionNone:
  622. case BreakpointConditionEOG:
  623. case BreakpointConditionEOF:
  624. break;
  625. default:
  626. StringAttr fieldName, value;
  627. bool caseSensitive;
  628. from.read(fieldName);
  629. from.read(value);
  630. from.read(caseSensitive);
  631. rowMatcher.setown(createRowMatcher(fieldName, condition, value, caseSensitive));
  632. }
  633. }
  634. void CBreakpointInfo::serialize(MemoryBuffer &to) const
  635. {
  636. to.append(uid);
  637. to.append(id);
  638. to.append((char) mode);
  639. to.append((char) action);
  640. to.append(rowCount);
  641. to.append((char) rowCountMode);
  642. to.append((char) condition);
  643. if (rowMatcher)
  644. {
  645. to.append(rowMatcher->queryFieldName());
  646. to.append(rowMatcher->queryValue());
  647. to.append(rowMatcher->queryCaseSensitive());
  648. }
  649. }
  650. SpinLock CBreakpointInfo::UIDlock;
  651. unsigned CBreakpointInfo::nextUIDvalue;
  652. unsigned CBreakpointInfo::queryUID() const
  653. {
  654. return uid;
  655. }
  656. void CBreakpointInfo::noteEdge(IActivityDebugContext &edge)
  657. {
  658. activeEdges.append(edge);
  659. }
  660. void CBreakpointInfo::removeEdge(IActivityDebugContext &edge)
  661. {
  662. activeEdges.zap(edge);
  663. }
  664. bool CBreakpointInfo::equals(IBreakpointInfo &other) const
  665. {
  666. CBreakpointInfo *bp = QUERYINTERFACE(&other, CBreakpointInfo);
  667. if (bp)
  668. {
  669. // NOTE - not very efficient but that's not an issue here!
  670. CommonXmlWriter me(0,0);
  671. CommonXmlWriter him(0,0);
  672. toXML(&me);
  673. bp->toXML(&him);
  674. return strcmp(me.str(), him.str())==0;
  675. }
  676. else
  677. return false;
  678. }
  679. bool CBreakpointInfo::canMatchAny(IOutputMetaData *meta)
  680. {
  681. if (rowMatcher)
  682. return rowMatcher->canMatchAny(meta);
  683. else
  684. return true;
  685. }
  686. bool CBreakpointInfo::matches(const void *row, bool isEOF, unsigned edgeRowCount, IOutputMetaData *meta) const
  687. {
  688. // First check the conditions
  689. switch (condition)
  690. {
  691. case BreakpointConditionNone:
  692. break;
  693. case BreakpointConditionEOG:
  694. if (row)
  695. return false;
  696. break;
  697. case BreakpointConditionEOF:
  698. if (row || !(meta->isGrouped() || isEOF))
  699. return false;
  700. break;
  701. default:
  702. if (rowMatcher)
  703. {
  704. if (!row)
  705. return false;
  706. rowMatcher->reset();
  707. meta->toXML((const byte *) row, *rowMatcher);
  708. if (!rowMatcher->matched())
  709. return false; // expression failed
  710. }
  711. else
  712. throwUnexpected();
  713. }
  714. // ok, we've been hit.
  715. if (rowCount)
  716. {
  717. switch (rowCountMode)
  718. {
  719. case BreakpointCountEquals:
  720. return (edgeRowCount == rowCount);
  721. case BreakpointCountAtleast:
  722. return (edgeRowCount >= rowCount);
  723. default:
  724. throwUnexpected();
  725. }
  726. }
  727. else
  728. return true;
  729. }
  730. bool CBreakpointInfo::idMatch(BreakpointMode _mode, const char *_id) const
  731. {
  732. if (mode==_mode)
  733. {
  734. if (mode==BreakpointModeGraph && !id.length())
  735. return true;
  736. if (strcmp(id.get(), _id)==0)
  737. return true;
  738. // See if it matches up to the first .
  739. const char *dotpos = strchr(_id, '.');
  740. if (dotpos)
  741. {
  742. unsigned len = dotpos-_id;
  743. if (len == id.length() && strncmp(id.get(), _id, len)==0)
  744. return true;
  745. }
  746. }
  747. return false;
  748. }
  749. BreakpointMode CBreakpointInfo::queryMode() const
  750. {
  751. return mode;
  752. }
  753. BreakpointActionMode CBreakpointInfo::queryAction() const
  754. {
  755. return action;
  756. }
  757. void CBreakpointInfo::toXML(IXmlWriter *output) const
  758. {
  759. output->outputCString(BreakpointModes[mode], "@mode");
  760. if (id) output->outputCString(id, "@id");
  761. output->outputCString(BreakpointActionModes[action], "@action");
  762. if (condition != BreakpointConditionNone)
  763. {
  764. output->outputCString(BreakpointConditionModes[condition], "@condition");
  765. if (rowMatcher)
  766. {
  767. if (rowMatcher->queryFieldName()) output->outputCString(rowMatcher->queryFieldName(), "@fieldName");
  768. if (rowMatcher->queryValue()) output->outputCString(rowMatcher->queryValue(), "@value");
  769. output->outputBool(rowMatcher->queryCaseSensitive(), "@caseSensitive");
  770. }
  771. }
  772. if (rowCount)
  773. {
  774. output->outputInt(rowCount, "@rowCount");
  775. output->outputCString(BreakpointCountModes[rowCountMode], "@rowCountMode");
  776. }
  777. ForEachItemIn(edgeIdx, activeEdges)
  778. {
  779. IActivityDebugContext &edge = activeEdges.item(edgeIdx);
  780. output->outputBeginNested("edge", false);
  781. output->outputCString(edge.queryEdgeId(), "@edgeId");
  782. output->outputEndNested("edge");
  783. }
  784. }
  785. //=======================================================================================
  786. DebugActivityRecord::DebugActivityRecord (IActivityBase *_activity, unsigned _iteration, unsigned _channel, unsigned _sequence)
  787. : activity(_activity),iteration(_iteration),channel(_channel),sequence(_sequence)
  788. {
  789. localCycles = 0;
  790. totalCycles = 0;
  791. idText.append(activity->queryId());
  792. if (iteration || channel)
  793. idText.appendf(".%d", iteration);
  794. if (channel)
  795. idText.appendf("#%d", channel);
  796. }
  797. void DebugActivityRecord::outputId(IXmlWriter *output, const char *fieldName)
  798. {
  799. output->outputString(idText.length(), idText.str(), fieldName);
  800. }
  801. const char *DebugActivityRecord::queryIdString()
  802. {
  803. return idText;
  804. }
  805. void DebugActivityRecord::outputProperties(IXmlWriter *output)
  806. {
  807. if (properties)
  808. {
  809. Owned<IPropertyIterator> iterator = properties->getIterator();
  810. iterator->first();
  811. while (iterator->isValid())
  812. {
  813. const char *propName = iterator->getPropKey();
  814. const char *propValue = properties->queryProp(propName);
  815. output->outputBeginNested("att", false); output->outputCString(propName, "@name"); output->outputCString(propValue, "@value"); output->outputEndNested("att");
  816. iterator->next();
  817. }
  818. }
  819. if (localCycles)
  820. {
  821. output->outputBeginNested("att", false);
  822. output->outputCString("localTime", "@name");
  823. output->outputUInt((unsigned) (cycle_to_nanosec(localCycles)/1000), "@value");
  824. output->outputEndNested("att");
  825. }
  826. if (totalCycles)
  827. {
  828. output->outputBeginNested("att", false);
  829. output->outputCString("totalTime", "@name");
  830. output->outputUInt((unsigned) (cycle_to_nanosec(totalCycles)/1000), "@value");
  831. output->outputEndNested("att");
  832. }
  833. }
  834. void DebugActivityRecord::setProperty(const char *propName, const char *propValue, unsigned _sequence)
  835. {
  836. if (!properties)
  837. properties.setown(createProperties(false));
  838. properties->setProp(propName, propValue);
  839. sequence = _sequence;
  840. }
  841. void DebugActivityRecord::updateTimes(unsigned _sequence)
  842. {
  843. unsigned __int64 newTotal = activity->queryTotalCycles();
  844. unsigned __int64 newLocal = activity->queryLocalCycles();
  845. if (localCycles != newLocal || totalCycles != newTotal)
  846. {
  847. localCycles = newLocal;
  848. totalCycles = newTotal;
  849. sequence = _sequence;
  850. }
  851. }
  852. //=======================================================================================
  853. const char * CBaseDebugContext::queryStateString(DebugState state)
  854. {
  855. switch (state)
  856. {
  857. case DebugStateCreated: return "created";
  858. case DebugStateLoading: return "loading";
  859. case DebugStateRunning: return "running";
  860. case DebugStateEdge: return "edge";
  861. case DebugStateBreakpoint: return "breakpoint";
  862. case DebugStateReady: return "created";
  863. case DebugStateGraphCreate: return "graph create";
  864. case DebugStateGraphStart: return "graph start";
  865. case DebugStateGraphEnd: return "graph end";
  866. case DebugStateGraphAbort: return "graph abort";
  867. case DebugStateException: return "exception";
  868. case DebugStateFailed: return "failed";
  869. case DebugStateFinished: return "finished";
  870. case DebugStateUnloaded: return "unloaded";
  871. case DebugStateQuit: return "quit";
  872. case DebugStateDetached: return "detached";
  873. case DebugStateLimit: return "limit hit";
  874. case DebugStateGraphFinished: return "graph finished";
  875. default: throwUnexpected();
  876. }
  877. }
  878. bool CBaseDebugContext::_checkPendingBreakpoints(DebugState state, const char *graphName)
  879. {
  880. bool stop = false;
  881. ForEachItemIn(idx, breakpoints)
  882. {
  883. IBreakpointInfo &bp = breakpoints.item(idx);
  884. if (bp.queryMode()==BreakpointModeGraph)
  885. {
  886. stop = stop || bp.idMatch(BreakpointModeGraph, graphName);
  887. }
  888. else
  889. {
  890. if (state==DebugStateGraphStart)
  891. currentGraph->setBreakpoint(bp);
  892. }
  893. }
  894. return stop;
  895. }
  896. CBaseDebugContext::CBaseDebugContext(const IContextLogger &_logctx) : logctx(_logctx)
  897. {
  898. currentGraph = NULL;
  899. graphChangeSequence = 0;
  900. prevGraphChangeSequence = 0;
  901. pendingBreakpointsDone = false;
  902. sequence = 1;
  903. defaultHistoryCapacity = 10;
  904. executeSequentially = true;
  905. running = true;
  906. detached = false;
  907. watchState = WatchStateContinue;
  908. currentBreakpointUID = (unsigned) -1;
  909. debuggerActive = 0;
  910. currentState = DebugStateCreated;
  911. skipRequested = false;
  912. stopOnLimits = true;
  913. debugCyclesAdjust = 0;
  914. }
  915. unsigned __int64 CBaseDebugContext::getCyclesAdjustment() const
  916. {
  917. return debugCyclesAdjust;
  918. }
  919. void CBaseDebugContext::noteGraphChanged()
  920. {
  921. graphChangeSequence++;
  922. }
  923. IGlobalEdgeRecord *CBaseDebugContext::getEdgeRecord(const char *edgeId)
  924. {
  925. Linked<IGlobalEdgeRecord> edgeRecord = globalCounts.getValue(edgeId);
  926. if (!edgeRecord)
  927. {
  928. edgeRecord.setown(new DebugEdgeRecord(sequence));
  929. globalCounts.setValue(edgeId, edgeRecord);
  930. }
  931. return edgeRecord.getClear();
  932. }
  933. void CBaseDebugContext::addBreakpoint(IBreakpointInfo &bp)
  934. {
  935. if (currentGraph)
  936. currentGraph->setBreakpoint(bp);
  937. breakpoints.append(bp);
  938. }
  939. void CBaseDebugContext::removeBreakpoint(IBreakpointInfo &bp)
  940. {
  941. ForEachItemIn(idx, breakpoints)
  942. {
  943. if (breakpoints.item(idx).queryUID()==bp.queryUID())
  944. {
  945. if (currentGraph)
  946. currentGraph->removeBreakpoint(breakpoints.item(idx));
  947. breakpoints.remove(idx);
  948. break;
  949. }
  950. }
  951. }
  952. unsigned CBaseDebugContext::queryChannel() const
  953. {
  954. return 0;
  955. }
  956. void CBaseDebugContext::debugInitialize(const char *id, const char *_queryName, bool _breakAtStart)
  957. {
  958. throwUnexpected(); // Should only happen on server
  959. }
  960. void CBaseDebugContext::debugTerminate()
  961. {
  962. throwUnexpected(); // Should only happen on server
  963. }
  964. IBreakpointInfo *CBaseDebugContext::findBreakpoint(unsigned uid) const
  965. {
  966. ForEachItemIn(idx, breakpoints)
  967. {
  968. if (breakpoints.item(idx).queryUID()==uid)
  969. return &breakpoints.item(idx);
  970. }
  971. // we don't really expect to get here...
  972. throwUnexpected();
  973. return NULL;
  974. }
  975. BreakpointActionMode CBaseDebugContext::checkBreakpoint(DebugState state, IActivityDebugContext *probe, const void *extra)
  976. {
  977. if (detached)
  978. return BreakpointActionContinue;
  979. class ActivityTimer
  980. {
  981. unsigned __int64 startCycles;
  982. unsigned __int64 &accumulator;
  983. public:
  984. inline ActivityTimer(unsigned __int64 &_accumulator) : accumulator(_accumulator)
  985. {
  986. startCycles = get_cycles_now();
  987. }
  988. inline ~ActivityTimer()
  989. {
  990. unsigned __int64 elapsed = get_cycles_now() - startCycles;
  991. accumulator += elapsed;
  992. }
  993. } timer(debugCyclesAdjust);
  994. CriticalBlock b(breakCrit); // Note - this may block for a while if program is suspended!
  995. assertex(running);
  996. bool stop = false;
  997. currentNode.clear();
  998. if (watchState == WatchStateQuit)
  999. throw MakeStringException(ROXIE_DEBUG_ERROR, "Query aborted by debugger");
  1000. if (state==DebugStateEdge)
  1001. {
  1002. // Check whether there is a breakpoint etc active for this activityId
  1003. IBreakpointInfo *bp;
  1004. if (currentBreakpointUID != (unsigned) -1)
  1005. bp = findBreakpoint(currentBreakpointUID);
  1006. else if (probe)
  1007. bp = probe->debuggerCallback(sequence, extra);
  1008. else
  1009. bp = NULL;
  1010. if (bp)
  1011. {
  1012. BreakpointActionMode action = bp->queryAction();
  1013. if (action != BreakpointActionBreak)
  1014. return action;
  1015. stop = true;
  1016. currentBreakpointUID = bp->queryUID();
  1017. }
  1018. else
  1019. {
  1020. switch (watchState)
  1021. {
  1022. case WatchStateStep:
  1023. stop = true;
  1024. break;
  1025. case WatchStateNext:
  1026. stop = (probe==nextActivity); // MORE - proxy activities need to be compared more carefully
  1027. break;
  1028. case WatchStateOver:
  1029. stop = (probe->queryInputActivity()==nextActivity); // MORE - doesn't work where multiple inputs or when debugging slave graph - think about proxy too
  1030. break;
  1031. case WatchStateGraph:
  1032. case WatchStateContinue:
  1033. stop = false;
  1034. break;
  1035. default:
  1036. throwUnexpected();
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. else if (state==DebugStateGraphCreate)
  1042. pendingBreakpointsDone = false;
  1043. else if (state==DebugStateGraphStart || state==DebugStateGraphEnd || state==DebugStateGraphAbort)
  1044. {
  1045. stop = _checkPendingBreakpoints(state, (const char *) extra);
  1046. pendingBreakpointsDone = true;
  1047. switch (watchState)
  1048. {
  1049. case WatchStateGraph:
  1050. stop = true;
  1051. break;
  1052. case WatchStateStep:
  1053. case WatchStateNext:
  1054. stop = true;
  1055. break;
  1056. case WatchStateContinue:
  1057. // stop already set above...
  1058. break;
  1059. default:
  1060. throwUnexpected();
  1061. break;
  1062. }
  1063. #if 0
  1064. // I don't think this works, and the global count stuff will supercede it
  1065. if (state==DebugStateGraphEnd || state==DebugStateGraphAbort)
  1066. {
  1067. CommonXmlWriter finalGraphXML(0, 1);
  1068. currentGraph->getXGMML(&finalGraphXML, 0, false);
  1069. if (!completedGraphs)
  1070. completedGraphs.setown(createProperties(false));
  1071. completedGraphs->setProp(currentGraph->queryGraphName(), finalGraphXML.str());
  1072. }
  1073. #endif
  1074. }
  1075. else if (state==DebugStateFinished)
  1076. {
  1077. currentGraph = NULL;
  1078. graphChangeSequence++;
  1079. stop = true;
  1080. }
  1081. else if (state==DebugStateException)
  1082. {
  1083. IException *E = (IException *) extra;
  1084. if (E != currentException)
  1085. {
  1086. currentException.set(E);
  1087. stop = true;
  1088. }
  1089. else
  1090. stop = false;
  1091. }
  1092. else if (state==DebugStateLimit)
  1093. {
  1094. if (stopOnLimits)
  1095. {
  1096. stop = true;
  1097. IActivityBase *activity = (IActivityBase *) extra;
  1098. assertex(currentGraph);
  1099. currentNode.setown(currentGraph->getNodeByActivityBase(activity));
  1100. }
  1101. }
  1102. else
  1103. stop = true;
  1104. if (!stop)
  1105. return BreakpointActionContinue;
  1106. {
  1107. CriticalBlock b(debugCrit);
  1108. currentActivity.set(probe);
  1109. running = false;
  1110. currentState = state;
  1111. if (currentActivity)
  1112. logctx.CTXLOG("DEBUG: paused waiting for debugger, state %s, edge %s", queryStateString(state), currentActivity->queryEdgeId());
  1113. else
  1114. logctx.CTXLOG("DEBUG: paused waiting for debugger, state %s, edge '(none)'", queryStateString(state));
  1115. if (debuggerActive) // Was the debugger actively waiting for the program to hit a breakpoint?
  1116. {
  1117. debuggerSem.signal(debuggerActive);
  1118. debuggerActive = 0;
  1119. }
  1120. }
  1121. waitForDebugger(state, probe);
  1122. {
  1123. CriticalBlock b(debugCrit);
  1124. running = true;
  1125. BreakpointActionMode ret = skipRequested ? BreakpointActionSkip : BreakpointActionContinue;
  1126. skipRequested = false;
  1127. currentBreakpointUID = (unsigned) -1;
  1128. return ret;
  1129. }
  1130. }
  1131. void CBaseDebugContext::noteManager(IDebugGraphManager *mgr)
  1132. {
  1133. currentGraph = mgr;
  1134. graphChangeSequence++;
  1135. }
  1136. void CBaseDebugContext::releaseManager(IDebugGraphManager *mgr)
  1137. {
  1138. if(currentGraph==mgr)
  1139. currentGraph = NULL;
  1140. graphChangeSequence++;
  1141. }
  1142. unsigned CBaseDebugContext::querySequence()
  1143. {
  1144. return ++sequence;
  1145. }
  1146. unsigned CBaseDebugContext::getDefaultHistoryCapacity() const
  1147. {
  1148. return defaultHistoryCapacity;
  1149. }
  1150. bool CBaseDebugContext::getExecuteSequentially() const
  1151. {
  1152. return executeSequentially;
  1153. }
  1154. void CBaseDebugContext::checkDelayedBreakpoints(IActivityDebugContext *edge)
  1155. {
  1156. if (pendingBreakpointsDone) // Don't bother if the graph is still being created.... we can do them all more efficiently.
  1157. {
  1158. // This code is for edges created AFTER the bulk of the graph is created, which missed out on the normal delayed breakpoint setting...
  1159. graphChangeSequence++;
  1160. ForEachItemIn(bpIdx, breakpoints)
  1161. {
  1162. IBreakpointInfo &breakpoint = breakpoints.item(bpIdx);
  1163. if (breakpoint.idMatch(BreakpointModeEdge, edge->queryEdgeId()))
  1164. edge->setBreakpoint(breakpoint);
  1165. }
  1166. }
  1167. }
  1168. void CBaseDebugContext::serialize(MemoryBuffer &buff) const
  1169. {
  1170. buff.append((unsigned short) breakpoints.length());
  1171. ForEachItemIn(idx, breakpoints)
  1172. {
  1173. breakpoints.item(idx).serialize(buff);
  1174. }
  1175. buff.append(running).append(detached).append((unsigned char) watchState).append(sequence).append(skipRequested).append(stopOnLimits);
  1176. }
  1177. void CBaseDebugContext::deserialize(MemoryBuffer &buff)
  1178. {
  1179. // MORE - this is rather inefficient - we remove all breakpoints then reapply. But may be good enough...
  1180. if (currentGraph)
  1181. {
  1182. ForEachItemIn(idx, breakpoints)
  1183. {
  1184. IBreakpointInfo &bp = breakpoints.item(idx);
  1185. currentGraph->removeBreakpoint(bp);
  1186. }
  1187. }
  1188. breakpoints.kill();
  1189. unsigned short numBreaks;
  1190. buff.read(numBreaks);
  1191. while (numBreaks--)
  1192. {
  1193. IBreakpointInfo &bp = *new CBreakpointInfo(buff);
  1194. breakpoints.append(bp);
  1195. if (currentGraph)
  1196. currentGraph->setBreakpoint(bp);
  1197. }
  1198. pendingBreakpointsDone = true;
  1199. unsigned char watchStateChar;
  1200. buff.read(running).read(detached).read(watchStateChar).read(sequence).read(skipRequested).read(stopOnLimits);
  1201. watchState = (WatchState) watchStateChar;
  1202. }
  1203. //=======================================================================================
  1204. void CBaseServerDebugContext::doStandardResult(IXmlWriter *output) const
  1205. {
  1206. const char *stateString = running ? "running" : queryStateString(currentState);
  1207. output->outputInt(sequence, "@sequence");
  1208. output->outputString(strlen(stateString), stateString, "@state");
  1209. if (currentNode)
  1210. currentNode->outputId(output, "@nodeId");
  1211. if (currentActivity)
  1212. output->outputCString(currentActivity->queryEdgeId(), "@edgeId");
  1213. if (skipRequested)
  1214. output->outputBool(true, "@skip");
  1215. if (currentGraph)
  1216. output->outputCString(currentGraph->queryGraphName(), "@graphId");
  1217. output->outputInt(graphChangeSequence, "@graphSequenceNum");
  1218. if (graphChangeSequence != prevGraphChangeSequence)
  1219. output->outputBool(true, "@graphChanged");
  1220. if (currentBreakpointUID != (unsigned) -1)
  1221. {
  1222. IBreakpointInfo *bp = findBreakpoint(currentBreakpointUID);
  1223. _listBreakpoint(output, *bp, breakpoints.find(*bp));
  1224. }
  1225. if (currentException)
  1226. {
  1227. output->outputBeginNested("Exception", true);
  1228. output->outputCString("Roxie", "Source");
  1229. output->outputInt(currentException->errorCode(), "Code");
  1230. StringBuffer s;
  1231. currentException->errorMessage(s);
  1232. output->outputString(s.length(), s.str(), "Message");
  1233. output->outputEndNested("Exception");
  1234. }
  1235. }
  1236. void CBaseServerDebugContext::_listBreakpoint(IXmlWriter *output, IBreakpointInfo &bp, unsigned idx) const
  1237. {
  1238. if (bp.queryMode() != BreakpointModeNone)
  1239. {
  1240. output->outputBeginNested("break", true);
  1241. output->outputInt(idx, "@idx");
  1242. bp.toXML(output);
  1243. output->outputEndNested("break");
  1244. }
  1245. }
  1246. void CBaseServerDebugContext::_continue(WatchState watch)
  1247. {
  1248. if (running)
  1249. throw MakeStringException(ROXIE_INTERNAL_ERROR, "Query already running");
  1250. if (watch==WatchStateNext || watch==WatchStateOver)
  1251. {
  1252. if (!currentActivity)
  1253. throw MakeStringException(ROXIE_INTERNAL_ERROR, "next: no current activity");
  1254. nextActivity.set(currentActivity);
  1255. }
  1256. else
  1257. nextActivity.clear();
  1258. watchState = watch;
  1259. previousSequence = sequence;
  1260. prevGraphChangeSequence = graphChangeSequence;
  1261. debuggerActive++;
  1262. debugeeSem.signal();
  1263. {
  1264. CriticalUnblock b(debugCrit);
  1265. debuggerSem.wait(); // MORE - is that actually correct, to release the crit while waiting? Think...
  1266. }
  1267. }
  1268. unsigned CBaseServerDebugContext::checkOption(const char *supplied, const char *name, const char *accepted[])
  1269. {
  1270. if (supplied)
  1271. {
  1272. unsigned idx = 0;
  1273. while (accepted[idx])
  1274. {
  1275. if (strcmp(accepted[idx], supplied)==0)
  1276. return idx;
  1277. idx++;
  1278. }
  1279. throw MakeStringException(ROXIE_DEBUG_ERROR, "Invalid parameter value %s='%s'", name, supplied);
  1280. }
  1281. else
  1282. return 0; // first is default
  1283. }
  1284. IBreakpointInfo *CBaseServerDebugContext::createDummyBreakpoint()
  1285. {
  1286. return new CBreakpointInfo(BreakpointModeNone);
  1287. }
  1288. IBreakpointInfo *CBaseServerDebugContext::_createBreakpoint(const char *modeString, const char *id, const char *action,
  1289. const char *fieldName, const char *condition, const char *value, bool caseSensitive,
  1290. unsigned hitCount, const char *hitCountMode)
  1291. {
  1292. BreakpointMode mode = (BreakpointMode) checkOption(modeString, "mode", BreakpointModes);
  1293. if (!id)
  1294. {
  1295. if (mode==BreakpointModeEdge)
  1296. {
  1297. if (!currentActivity)
  1298. throw MakeStringException(ROXIE_DEBUG_ERROR, "No current activity");
  1299. id = currentActivity->queryEdgeId();
  1300. }
  1301. else if (mode==BreakpointModeNode)
  1302. {
  1303. if (!currentActivity)
  1304. throw MakeStringException(ROXIE_DEBUG_ERROR, "No current activity");
  1305. id = currentActivity->querySourceId();
  1306. }
  1307. }
  1308. return new CBreakpointInfo(mode, id, (BreakpointActionMode) checkOption(action, "action", BreakpointActionModes),
  1309. fieldName, (BreakpointConditionMode) checkOption(condition, "condition", BreakpointConditionModes), value, caseSensitive,
  1310. hitCount, (BreakpointCountMode) checkOption(hitCountMode, "hitCountMode", BreakpointCountModes));
  1311. }
  1312. void CBaseServerDebugContext::waitForDebugger(DebugState state, IActivityDebugContext *probe)
  1313. {
  1314. sequence++;
  1315. while (!debugeeSem.wait(DEBUGEE_TIMEOUT))
  1316. {
  1317. if (onDebuggerTimeout())
  1318. break;
  1319. }
  1320. sequence++;
  1321. }
  1322. CBaseServerDebugContext::CBaseServerDebugContext(const IContextLogger &_logctx, IPropertyTree *_queryXGMML, SafeSocket &_client)
  1323. : CBaseDebugContext(_logctx), queryXGMML(_queryXGMML), client(_client)
  1324. {
  1325. sequence = 0;
  1326. previousSequence = 0;
  1327. breakpoints.append(*createDummyBreakpoint()); // temporary breakpoint always present
  1328. }
  1329. void CBaseServerDebugContext::serializeBreakpoints(MemoryBuffer &to)
  1330. {
  1331. to.append(breakpoints.length());
  1332. ForEachItemIn(idx, breakpoints)
  1333. breakpoints.item(idx).serialize(to);
  1334. }
  1335. void CBaseServerDebugContext::debugInitialize(const char *id, const char *_queryName, bool _breakAtStart)
  1336. {
  1337. if (!_breakAtStart)
  1338. detached = true;
  1339. debugId.set(id);
  1340. queryName.set(_queryName);
  1341. currentState = DebugStateLoading;
  1342. }
  1343. void CBaseServerDebugContext::debugTerminate()
  1344. {
  1345. CriticalBlock b(debugCrit);
  1346. assertex(running);
  1347. currentState = DebugStateUnloaded;
  1348. running = false;
  1349. if (debuggerActive)
  1350. {
  1351. debuggerSem.signal(debuggerActive);
  1352. debuggerActive = 0;
  1353. }
  1354. }
  1355. void CBaseServerDebugContext::addBreakpoint(IXmlWriter *output, const char *modeString, const char *id, const char *action,
  1356. const char *fieldName, const char *condition, const char *value, bool caseSensitive,
  1357. unsigned hitCount, const char *hitCountMode)
  1358. {
  1359. CriticalBlock b(debugCrit);
  1360. if (running)
  1361. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1362. Owned<IBreakpointInfo> newBreakpoint = _createBreakpoint(modeString, id, action, fieldName, condition, value, caseSensitive, hitCount, hitCountMode);
  1363. // For breakpoints to be efficient we need to set them in the probe, though because the activity probe may not exist yet, we should also
  1364. // keep a central list (also makes it easier to do things like list breakpoint info...)
  1365. ForEachItemIn(idx, breakpoints)
  1366. {
  1367. IBreakpointInfo &bp = breakpoints.item(idx);
  1368. if (bp.equals(*newBreakpoint))
  1369. throw MakeStringException(ROXIE_DEBUG_ERROR, "Breakpoint already exists");
  1370. }
  1371. _listBreakpoint(output, *newBreakpoint, breakpoints.ordinality());
  1372. CBaseDebugContext::addBreakpoint(*newBreakpoint.getClear());
  1373. }
  1374. void CBaseServerDebugContext::removeBreakpoint(IXmlWriter *output, unsigned removeIdx)
  1375. {
  1376. CriticalBlock b(debugCrit);
  1377. if (running)
  1378. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1379. assertex(removeIdx);
  1380. if (!breakpoints.isItem(removeIdx))
  1381. throw MakeStringException(ROXIE_DEBUG_ERROR, "Breakpoint %d does not exist", removeIdx);
  1382. IBreakpointInfo &bp = breakpoints.item(removeIdx);
  1383. _listBreakpoint(output, bp, removeIdx);
  1384. if (currentGraph)
  1385. currentGraph->removeBreakpoint(bp);
  1386. breakpoints.replace(*createDummyBreakpoint(), removeIdx);
  1387. }
  1388. void CBaseServerDebugContext::removeAllBreakpoints(IXmlWriter *output)
  1389. {
  1390. ForEachItemIn(idx, breakpoints)
  1391. {
  1392. IBreakpointInfo &bp = breakpoints.item(idx);
  1393. _listBreakpoint(output, bp, idx);
  1394. if (currentGraph)
  1395. currentGraph->removeBreakpoint(bp);
  1396. breakpoints.replace(*createDummyBreakpoint(), idx);
  1397. }
  1398. }
  1399. void CBaseServerDebugContext::listBreakpoint(IXmlWriter *output, unsigned listIdx) const
  1400. {
  1401. // MORE - fair amount could be commoned up with remove...
  1402. CriticalBlock b(debugCrit);
  1403. if (running)
  1404. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1405. assertex(listIdx);
  1406. if (!breakpoints.isItem(listIdx))
  1407. throw MakeStringException(ROXIE_DEBUG_ERROR, "Breakpoint %d does not exist", listIdx);
  1408. IBreakpointInfo &bp = breakpoints.item(listIdx);
  1409. _listBreakpoint(output, bp, listIdx);
  1410. }
  1411. void CBaseServerDebugContext::listAllBreakpoints(IXmlWriter *output) const
  1412. {
  1413. ForEachItemIn(idx, breakpoints)
  1414. {
  1415. IBreakpointInfo &bp = breakpoints.item(idx);
  1416. _listBreakpoint(output, bp, idx);
  1417. }
  1418. }
  1419. void CBaseServerDebugContext::debugInterrupt(IXmlWriter *output)
  1420. {
  1421. CriticalBlock b(debugCrit);
  1422. if (!running)
  1423. throw MakeStringException(ROXIE_DEBUG_ERROR, "Query is already paused"); // MORE - or has terminated?
  1424. detached = false;
  1425. nextActivity.clear();
  1426. watchState = WatchStateStep;
  1427. debuggerActive++;
  1428. {
  1429. CriticalUnblock b(debugCrit);
  1430. debuggerSem.wait(); // MORE - should this be inside critsec? Should it be there at all?
  1431. }
  1432. doStandardResult(output);
  1433. }
  1434. void CBaseServerDebugContext::debugContinue(IXmlWriter *output, const char *modeString, const char *id)
  1435. {
  1436. CriticalBlock b(debugCrit);
  1437. Owned<IBreakpointInfo> tempBreakpoint;
  1438. if (id)
  1439. {
  1440. tempBreakpoint.setown(_createBreakpoint(modeString, id));
  1441. if (currentGraph)
  1442. currentGraph->setBreakpoint(*tempBreakpoint);
  1443. breakpoints.replace(*LINK(tempBreakpoint), 0);
  1444. }
  1445. _continue(WatchStateContinue);
  1446. doStandardResult(output);
  1447. if (tempBreakpoint)
  1448. {
  1449. if (currentGraph)
  1450. currentGraph->removeBreakpoint(*tempBreakpoint);
  1451. breakpoints.replace(*createDummyBreakpoint(), 0);
  1452. }
  1453. }
  1454. void CBaseServerDebugContext::debugRun(IXmlWriter *output)
  1455. {
  1456. CriticalBlock b(debugCrit);
  1457. if (running)
  1458. throw MakeStringException(ROXIE_INTERNAL_ERROR, "Query already running");
  1459. watchState = WatchStateContinue;
  1460. detached = true;
  1461. if (currentGraph)
  1462. currentGraph->clearHistories();
  1463. currentState = DebugStateDetached;
  1464. currentActivity.clear();
  1465. currentBreakpointUID = (unsigned) -1;
  1466. previousSequence = sequence;
  1467. debugeeSem.signal();
  1468. doStandardResult(output);
  1469. }
  1470. void CBaseServerDebugContext::debugQuit(IXmlWriter *output)
  1471. {
  1472. CriticalBlock b(debugCrit);
  1473. detached = false;
  1474. nextActivity.clear();
  1475. watchState = WatchStateQuit;
  1476. currentState = DebugStateQuit;
  1477. currentActivity.clear();
  1478. currentBreakpointUID = (unsigned) -1;
  1479. if (!running)
  1480. {
  1481. debugeeSem.signal();
  1482. }
  1483. doStandardResult(output);
  1484. }
  1485. void CBaseServerDebugContext::debugSkip(IXmlWriter *output)
  1486. {
  1487. CriticalBlock b(debugCrit);
  1488. if (running)
  1489. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1490. if (!currentActivity)
  1491. throw MakeStringException(ROXIE_DEBUG_ERROR, "No current activity");
  1492. skipRequested = true;
  1493. doStandardResult(output);
  1494. }
  1495. void CBaseServerDebugContext::debugStatus(IXmlWriter *output) const
  1496. {
  1497. CriticalBlock b(debugCrit);
  1498. doStandardResult(output);
  1499. }
  1500. void CBaseServerDebugContext::debugStep(IXmlWriter *output, const char *modeString)
  1501. {
  1502. CriticalBlock b(debugCrit);
  1503. WatchState state;
  1504. if (modeString)
  1505. {
  1506. if (strcmp(modeString, "graph")==0)
  1507. state = WatchStateGraph;
  1508. else if (strcmp(modeString, "edge")==0)
  1509. state = WatchStateStep;
  1510. else
  1511. throw MakeStringException(ROXIE_DEBUG_ERROR, "Step mode should be edge or graph");
  1512. }
  1513. else
  1514. state = WatchStateStep;
  1515. _continue(state);
  1516. doStandardResult(output);
  1517. }
  1518. void CBaseServerDebugContext::debugNext(IXmlWriter *output)
  1519. {
  1520. CriticalBlock b(debugCrit);
  1521. _continue(WatchStateNext);
  1522. doStandardResult(output);
  1523. }
  1524. void CBaseServerDebugContext::debugOver(IXmlWriter *output)
  1525. {
  1526. CriticalBlock b(debugCrit);
  1527. _continue(WatchStateOver);
  1528. doStandardResult(output);
  1529. }
  1530. void CBaseServerDebugContext::debugChanges(IXmlWriter *output, unsigned sinceSequence) const
  1531. {
  1532. CriticalBlock b(debugCrit);
  1533. if (running)
  1534. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1535. if (sinceSequence == (unsigned) -1)
  1536. sinceSequence = previousSequence;
  1537. if (!currentGraph)
  1538. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available when no graph active");
  1539. // MORE - if current graph has changed since specified sequence, then ??
  1540. currentGraph->getXGMML(output, sinceSequence, true);
  1541. }
  1542. void CBaseServerDebugContext::debugCounts(IXmlWriter *output, unsigned sinceSequence, bool reset)
  1543. {
  1544. CriticalBlock b(debugCrit);
  1545. if (running)
  1546. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1547. if (sinceSequence == (unsigned) -1)
  1548. sinceSequence = previousSequence;
  1549. HashIterator edges(globalCounts);
  1550. ForEach(edges)
  1551. {
  1552. IGlobalEdgeRecord *edge = globalCounts.mapToValue(&edges.query());
  1553. if (!sinceSequence || edge->queryLastSequence() > sinceSequence)
  1554. {
  1555. output->outputBeginNested("edge", true);
  1556. output->outputCString((const char *) edges.query().getKey(), "@edgeId");
  1557. output->outputUInt(edge->queryCount(), "@count");
  1558. output->outputEndNested("edge");
  1559. }
  1560. if (reset)
  1561. edge->reset();
  1562. }
  1563. }
  1564. void CBaseServerDebugContext::debugWhere(IXmlWriter *output) const
  1565. {
  1566. CriticalBlock b(debugCrit);
  1567. if (running)
  1568. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1569. if (!currentActivity)
  1570. throw MakeStringException(ROXIE_DEBUG_ERROR, "No current activity");
  1571. output->outputCString(currentGraph->queryGraphName(), "@graphId");
  1572. output->outputCString(currentActivity->queryEdgeId(), "@edgeId");
  1573. IActivityDebugContext *activityCtx = currentActivity;
  1574. while (activityCtx)
  1575. {
  1576. activityCtx->printEdge(output, 0, 1);
  1577. output->outputEndNested("Edge");
  1578. activityCtx = activityCtx->queryInputActivity();
  1579. }
  1580. }
  1581. void CBaseServerDebugContext::debugPrintVariable(IXmlWriter *output, const char *name, const char *type) const
  1582. {
  1583. throwUnexpected(); // must be implemented by platform-specific derived class
  1584. }
  1585. void CBaseServerDebugContext::debugSearch(IXmlWriter *output, const char *fieldName, const char *condition, const char *value, bool caseSensitive, bool fullRows) const
  1586. {
  1587. CriticalBlock b(debugCrit);
  1588. if (running)
  1589. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1590. if (!currentGraph)
  1591. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available when no graph active");
  1592. Owned<IRowMatcher> searcher = createRowMatcher(fieldName, (BreakpointConditionMode) checkOption(condition, "condition", BreakpointConditionModes), value, caseSensitive);
  1593. currentGraph->searchHistories(output, searcher, fullRows);
  1594. }
  1595. void CBaseServerDebugContext::debugPrint(IXmlWriter *output, const char *edgeId, unsigned startRow, unsigned numRows) const
  1596. {
  1597. CriticalBlock b(debugCrit);
  1598. if (running)
  1599. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1600. if (!currentGraph)
  1601. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available when no graph active");
  1602. IActivityDebugContext *activityCtx;
  1603. if (edgeId)
  1604. {
  1605. activityCtx = currentGraph->lookupActivityByEdgeId(edgeId);
  1606. if (!activityCtx)
  1607. throw MakeStringException(ROXIE_DEBUG_ERROR, "Edge %s not found in current graph", edgeId);
  1608. }
  1609. else if (currentActivity)
  1610. activityCtx = currentActivity;
  1611. else
  1612. throw MakeStringException(ROXIE_DEBUG_ERROR, "An edge id must be specified if there is no current edge");
  1613. output->outputCString(currentGraph->queryGraphName(), "@graphId");
  1614. #if 1
  1615. CommonXmlWriter dummyOutput(0, 0);
  1616. activityCtx->printEdge(&dummyOutput, startRow, numRows); // MORE - need to suppress the <Edge> if we want backward compatibility!
  1617. Owned<IPropertyTree> result = createPTreeFromXMLString(dummyOutput.str());
  1618. if (result)
  1619. {
  1620. Owned<IAttributeIterator> attributes = result->getAttributes();
  1621. ForEach(*attributes)
  1622. {
  1623. output->outputCString(attributes->queryValue(),attributes->queryName());
  1624. }
  1625. output->outputString(0, NULL, NULL);
  1626. Owned<IPropertyTreeIterator> elems = result->getElements("*");
  1627. ForEach(*elems)
  1628. {
  1629. StringBuffer e;
  1630. IPropertyTree &elem = elems->query();
  1631. toXML(&elem, e);
  1632. output->outputQuoted(e.str());
  1633. }
  1634. }
  1635. #else
  1636. activityCtx->printEdge(output, startRow, numRows); // MORE - need to suppress the <Edge> if we want backward compatibility!
  1637. #endif
  1638. }
  1639. void CBaseServerDebugContext::debugGetConfig(IXmlWriter *output, const char *name, const char *id) const
  1640. {
  1641. CriticalBlock b(debugCrit);
  1642. if (running)
  1643. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1644. if (!name)
  1645. throw MakeStringException(ROXIE_DEBUG_ERROR, "configuration setting name must be supplied");
  1646. if (stricmp(name, "executeSequentially")==0)
  1647. {
  1648. output->outputBool(executeSequentially, "@value");
  1649. }
  1650. else if (stricmp(name, "stopOnLimits")==0)
  1651. {
  1652. output->outputInt(stopOnLimits, "@value");
  1653. }
  1654. else if (stricmp(name, "historySize")==0)
  1655. {
  1656. if (id)
  1657. {
  1658. if (!currentGraph)
  1659. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available when no graph active");
  1660. IActivityDebugContext *activityCtx = currentGraph->lookupActivityByEdgeId(id);
  1661. if (!activityCtx)
  1662. throw MakeStringException(ROXIE_DEBUG_ERROR, "Edge %s not found in current graph", id);
  1663. output->outputInt(activityCtx->queryHistoryCapacity(), "@value");
  1664. }
  1665. else
  1666. {
  1667. output->outputInt(defaultHistoryCapacity, "@value");
  1668. }
  1669. }
  1670. else
  1671. throw MakeStringException(ROXIE_DEBUG_ERROR, "Unknown configuration setting '%s'", name);
  1672. output->outputCString(name, "@name");
  1673. if (id) output->outputCString(id, "@id");
  1674. }
  1675. void CBaseServerDebugContext::debugSetConfig(IXmlWriter *output, const char *name, const char *value, const char *id)
  1676. {
  1677. CriticalBlock b(debugCrit);
  1678. if (running)
  1679. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1680. if (!name)
  1681. throw MakeStringException(ROXIE_DEBUG_ERROR, "configuration setting name must be supplied");
  1682. unsigned intval = value ? atoi(value) : 0;
  1683. if (stricmp(name, "executeSequentially")==0)
  1684. {
  1685. if (id)
  1686. throw MakeStringException(ROXIE_DEBUG_ERROR, "id not supported here");
  1687. if (!value || clipStrToBool(value))
  1688. executeSequentially = true;
  1689. else
  1690. executeSequentially = false;
  1691. }
  1692. else if (stricmp(name, "stopOnLimits")==0)
  1693. {
  1694. if (id)
  1695. throw MakeStringException(ROXIE_DEBUG_ERROR, "id not supported here");
  1696. if (!value || clipStrToBool(value))
  1697. stopOnLimits = true;
  1698. else
  1699. stopOnLimits = false;
  1700. }
  1701. else if (stricmp(name, "historySize")==0)
  1702. {
  1703. if (!value)
  1704. throw MakeStringException(ROXIE_DEBUG_ERROR, "No value supplied");
  1705. if (id)
  1706. {
  1707. if (!currentGraph)
  1708. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available when no graph active");
  1709. IActivityDebugContext *activityCtx = currentGraph->lookupActivityByEdgeId(id);
  1710. if (!activityCtx)
  1711. throw MakeStringException(ROXIE_DEBUG_ERROR, "Edge %s not found in current graph", id);
  1712. activityCtx->setHistoryCapacity(intval);
  1713. }
  1714. else
  1715. {
  1716. defaultHistoryCapacity = intval;
  1717. if (currentGraph)
  1718. currentGraph->setHistoryCapacity(intval);
  1719. }
  1720. }
  1721. else
  1722. throw MakeStringException(ROXIE_DEBUG_ERROR, "Unknown configuration setting '%s'", name);
  1723. if (name) output->outputCString(name, "@name");
  1724. if (value) output->outputCString(value, "@value");
  1725. if (id) output->outputCString(id, "@id");
  1726. }
  1727. void CBaseServerDebugContext::getCurrentGraphXGMML(IXmlWriter *output, bool original) const
  1728. {
  1729. CriticalBlock b(debugCrit);
  1730. if (running)
  1731. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1732. if (!currentGraph)
  1733. throw MakeStringException(ROXIE_DEBUG_ERROR, "No current graph");
  1734. if (original)
  1735. getGraphXGMML(output, currentGraph->queryGraphName());
  1736. else
  1737. {
  1738. #if 0
  1739. if (completedGraphs)
  1740. {
  1741. Owned<IPropertyIterator> iterator = completedGraphs->getIterator();
  1742. iterator->first();
  1743. while (iterator->isValid())
  1744. {
  1745. const char *graphName = iterator->getPropKey();
  1746. const char *graphXML = completedGraphs->queryProp(graphName);
  1747. output->outputString(0, 0, NULL);
  1748. output->outputQuoted(graphXML);
  1749. iterator->next();
  1750. }
  1751. }
  1752. #endif
  1753. currentGraph->getXGMML(output, 0, true);
  1754. }
  1755. }
  1756. void CBaseServerDebugContext::getQueryXGMML(IXmlWriter *output) const
  1757. {
  1758. CriticalBlock b(debugCrit);
  1759. if (running)
  1760. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1761. StringBuffer s;
  1762. toXML(queryXGMML, s, 2);
  1763. output->outputString(0, NULL, NULL); // closes any open tags....
  1764. output->outputQuoted(s.str());
  1765. }
  1766. void CBaseServerDebugContext::getGraphXGMML(IXmlWriter *output, const char *graphName) const
  1767. {
  1768. CriticalBlock b(debugCrit);
  1769. if (running)
  1770. throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
  1771. StringBuffer xpath;
  1772. xpath.appendf("Graph[@id='%s']", graphName);
  1773. Owned<IPropertyTree> graph = queryXGMML->getPropTree(xpath.str());
  1774. if (!graph)
  1775. throw MakeStringException(ROXIE_DEBUG_ERROR, "Graph %s not found", graphName);
  1776. StringBuffer s;
  1777. toXML(graph, s, 2);
  1778. output->outputString(0, NULL, NULL); // closes any open tags....
  1779. output->outputQuoted(s.str());
  1780. }
  1781. const char *CBaseServerDebugContext::queryQueryName() const
  1782. {
  1783. return queryName.get();
  1784. }
  1785. const char *CBaseServerDebugContext::queryDebugId() const
  1786. {
  1787. return debugId.get();
  1788. }
  1789. //=======================================================================================
  1790. DebugActivityRecord *CBaseDebugGraphManager::noteActivity(IActivityBase *activity, unsigned iteration, unsigned channel, unsigned sequence)
  1791. {
  1792. Linked<DebugActivityRecord> node = allActivities.getValue(activity);
  1793. if (!node)
  1794. {
  1795. node.setown(new DebugActivityRecord(activity, iteration, channel, sequence));
  1796. allActivities.setValue(activity, node);
  1797. }
  1798. else
  1799. {
  1800. node->iteration = iteration;
  1801. node->channel = channel;
  1802. node->sequence = sequence;
  1803. }
  1804. return node; // note - does not link
  1805. }
  1806. IDebuggableContext *CBaseDebugGraphManager::queryContext() const
  1807. {
  1808. return debugContext;
  1809. }
  1810. DebugActivityRecord *CBaseDebugGraphManager::getNodeByActivityBase(IActivityBase *activity) const
  1811. {
  1812. Linked<DebugActivityRecord> node = allActivities.getValue(activity);
  1813. if (!node)
  1814. {
  1815. ForEachItemIn(idx, childGraphs)
  1816. {
  1817. node.setown(childGraphs.item(idx).getNodeByActivityBase(activity));
  1818. if (node)
  1819. break;
  1820. }
  1821. }
  1822. return node.getClear();
  1823. }
  1824. void CBaseDebugGraphManager::outputLinksForChildGraph(IXmlWriter *output, const char *parentId)
  1825. {
  1826. ForEachItemIn(idx, sinks)
  1827. {
  1828. Linked<DebugActivityRecord> childNode = allActivities.getValue(&sinks.item(idx));
  1829. output->outputBeginNested("edge", true);
  1830. output->outputCString("Child", "@label");
  1831. StringBuffer idText;
  1832. idText.append(childNode->idText).append('_').append(parentId);
  1833. output->outputCString(idText.str(), "@id"); // MORE - is this guaranteed to be unique?
  1834. output->outputBeginNested("att", false); output->outputCString("_childGraph", "@name"); output->outputInt(1, "@value"); output->outputEndNested("att");
  1835. output->outputBeginNested("att", false); output->outputCString("_sourceActivity", "@name"); childNode->outputId(output, "@value"); output->outputEndNested("att"); // MORE!!!
  1836. output->outputBeginNested("att", false); output->outputCString("_targetActivity", "@name"); output->outputCString(parentId, "@value"); output->outputEndNested("att");
  1837. output->outputEndNested("edge");
  1838. }
  1839. }
  1840. void CBaseDebugGraphManager::outputChildGraph(IXmlWriter *output, unsigned sequence)
  1841. {
  1842. if (allActivities.count())
  1843. {
  1844. output->outputBeginNested("graph", true);
  1845. HashIterator edges(allProbes);
  1846. ForEach(edges)
  1847. {
  1848. IActivityDebugContext *edge = allProbes.mapToValue(&edges.query());
  1849. if (!sequence || edge->queryLastSequence() > sequence)
  1850. edge->getXGMML(output);
  1851. }
  1852. HashIterator nodes(allActivities);
  1853. ForEach(nodes)
  1854. {
  1855. DebugActivityRecord *node = allActivities.mapToValue(&nodes.query());
  1856. if (!sequence || node->sequence > sequence) // MORE - if we start reporting any stats on nodes (seeks? scans?) this may need to change
  1857. {
  1858. output->outputBeginNested("node", true);
  1859. node->outputId(output, "@id");
  1860. ThorActivityKind kind = node->activity->getKind();
  1861. StringBuffer kindStr(getActivityText(kind));
  1862. output->outputString(kindStr.length(), kindStr.str(), "@shortlabel");
  1863. if (node->activity->isPassThrough())
  1864. {
  1865. output->outputBeginNested("att", false); output->outputCString("_isPassthrough", "@name"); output->outputInt(1, "@value"); output->outputEndNested("att");
  1866. }
  1867. if (node->totalCycles)
  1868. {
  1869. output->outputBeginNested("att", false);
  1870. output->outputCString("totalTime", "@name");
  1871. output->outputUInt((unsigned) (cycle_to_nanosec(node->totalCycles)/1000), "@value");
  1872. output->outputEndNested("att");
  1873. }
  1874. if (node->localCycles)
  1875. {
  1876. output->outputBeginNested("att", false);
  1877. output->outputCString("localTime", "@name");
  1878. output->outputUInt((unsigned) (cycle_to_nanosec(node->localCycles)/1000), "@value");
  1879. output->outputEndNested("att");
  1880. }
  1881. node->outputProperties(output);
  1882. }
  1883. if (!sequence || node->sequence > sequence)
  1884. output->outputEndNested("node");
  1885. ForEachItemIn(idx, node->childGraphs)
  1886. {
  1887. output->outputBeginNested("node", true);
  1888. IDebugGraphManager &childGraph = node->childGraphs.item(idx);
  1889. output->outputCString(childGraph.queryIdString(), "@id");
  1890. childGraph.outputChildGraph(output, sequence);
  1891. output->outputEndNested("node");
  1892. childGraph.outputLinksForChildGraph(output, node->queryIdString());
  1893. }
  1894. }
  1895. output->outputEndNested("graph");
  1896. }
  1897. else
  1898. {
  1899. // This is a bit of a hack. Slave-side graphs have no outer-level node, so we have to do the childgraphs instead
  1900. ForEachItemIn(idx, childGraphs)
  1901. {
  1902. childGraphs.item(idx).outputChildGraph(output, sequence);
  1903. }
  1904. }
  1905. }
  1906. void CBaseDebugGraphManager::Link() const
  1907. {
  1908. CInterface::Link();
  1909. }
  1910. bool CBaseDebugGraphManager::Release() const
  1911. {
  1912. if (!IsShared())
  1913. {
  1914. if (!id)
  1915. debugContext->releaseManager(const_cast<CBaseDebugGraphManager*> (this));
  1916. }
  1917. return CInterface::Release();
  1918. }
  1919. CBaseDebugGraphManager::CBaseDebugGraphManager(IDebuggableContext *_debugContext, unsigned _id, const char *_graphName) : debugContext(_debugContext), id(_id), graphName(_graphName)
  1920. {
  1921. if (_graphName)
  1922. debugContext->noteManager(this);
  1923. idString.append(_id);
  1924. proxyId = 0;
  1925. }
  1926. IDebugGraphManager *CBaseDebugGraphManager::queryDebugManager()
  1927. {
  1928. return this;
  1929. }
  1930. const char *CBaseDebugGraphManager::queryIdString() const
  1931. {
  1932. return idString;
  1933. }
  1934. unsigned CBaseDebugGraphManager::queryId() const
  1935. {
  1936. return id;
  1937. }
  1938. void CBaseDebugGraphManager::noteSink(IActivityBase *sink)
  1939. {
  1940. CriticalBlock b(crit);
  1941. sinks.append(*LINK(sink));
  1942. }
  1943. void CBaseDebugGraphManager::noteDependency(IActivityBase *sourceActivity, unsigned sourceIndex, unsigned controlId, const char *edgeId, IActivityBase *targetActivity)
  1944. {
  1945. CriticalBlock b(crit);
  1946. dependencies.append(*new DebugDependencyRecord(sourceActivity, sourceIndex, controlId, edgeId, targetActivity, debugContext->querySequence()));
  1947. }
  1948. void CBaseDebugGraphManager::setNodeProperty(IActivityBase *node, const char *propName, const char *propValue)
  1949. {
  1950. CriticalBlock b(crit);
  1951. Linked<DebugActivityRecord> nodeInfo = allActivities.getValue(node);
  1952. if (nodeInfo)
  1953. nodeInfo->setProperty(propName, propValue, debugContext->querySequence());
  1954. ForEachItemIn(idx, childGraphs)
  1955. {
  1956. childGraphs.item(idx).setNodeProperty(node, propName, propValue);
  1957. }
  1958. }
  1959. void CBaseDebugGraphManager::setNodePropertyInt(IActivityBase *node, const char *propName, unsigned __int64 propValue)
  1960. {
  1961. StringBuffer s;
  1962. s.append(propValue);
  1963. setNodeProperty(node, propName, s.str());
  1964. }
  1965. void CBaseDebugGraphManager::getProbeResponse(IPropertyTree *query)
  1966. {
  1967. throwUnexpected();
  1968. }
  1969. void CBaseDebugGraphManager::setHistoryCapacity(unsigned newCapacity)
  1970. {
  1971. HashIterator edges(allProbes);
  1972. ForEach(edges)
  1973. {
  1974. IActivityDebugContext *edge = allProbes.mapToValue(&edges.query());
  1975. edge->setHistoryCapacity(newCapacity);
  1976. }
  1977. }
  1978. void CBaseDebugGraphManager::clearHistories()
  1979. {
  1980. HashIterator edges(allProbes);
  1981. ForEach(edges)
  1982. {
  1983. IActivityDebugContext *edge = allProbes.mapToValue(&edges.query());
  1984. edge->clearHistory();
  1985. }
  1986. }
  1987. void CBaseDebugGraphManager::setBreakpoint(IBreakpointInfo &bp)
  1988. {
  1989. // May want a hash lookup here, but the non-uniqueness makes it slightly non-trivial so bruteforce for now
  1990. HashIterator edges(allProbes);
  1991. ForEach(edges)
  1992. {
  1993. IActivityDebugContext *edge = allProbes.mapToValue(&edges.query());
  1994. if (bp.queryMode()==BreakpointModeGlobal || bp.idMatch(BreakpointModeEdge, edge->queryEdgeId()))
  1995. edge->setBreakpoint(bp);
  1996. }
  1997. ForEachItemIn(idx, childGraphs)
  1998. {
  1999. childGraphs.item(idx).setBreakpoint(bp);
  2000. }
  2001. }
  2002. void CBaseDebugGraphManager::mergeRemoteCounts(IDebuggableContext *into) const
  2003. {
  2004. ForEachItemIn(idx, childGraphs)
  2005. {
  2006. childGraphs.item(idx).mergeRemoteCounts(into);
  2007. }
  2008. }
  2009. void CBaseDebugGraphManager::removeBreakpoint(IBreakpointInfo &bp)
  2010. {
  2011. // May want a hash lookup here, but the non-uniqueness makes it slightly non-trivial so bruteforce for now
  2012. HashIterator edges(allProbes);
  2013. ForEach(edges)
  2014. {
  2015. IActivityDebugContext *edge = allProbes.mapToValue(&edges.query());
  2016. if (bp.queryMode()==BreakpointModeGlobal || bp.idMatch(BreakpointModeEdge, edge->queryEdgeId()))
  2017. edge->removeBreakpoint(bp);
  2018. }
  2019. ForEachItemIn(idx, childGraphs)
  2020. {
  2021. childGraphs.item(idx).removeBreakpoint(bp);
  2022. }
  2023. }
  2024. IActivityDebugContext *CBaseDebugGraphManager::lookupActivityByEdgeId(const char *edgeId)
  2025. {
  2026. // MORE - if structured ID's, behave differently?
  2027. IActivityDebugContext *edge = allProbes.getValue(edgeId);
  2028. if (!edge)
  2029. {
  2030. ForEachItemIn(idx, childGraphs)
  2031. {
  2032. edge = childGraphs.item(idx).lookupActivityByEdgeId(edgeId);
  2033. if (edge)
  2034. break;
  2035. }
  2036. }
  2037. return edge;
  2038. }
  2039. const char *CBaseDebugGraphManager::queryGraphName() const
  2040. {
  2041. return graphName.get();
  2042. }
  2043. void CBaseDebugGraphManager::getXGMML(IXmlWriter *output, unsigned sequence, bool isActive)
  2044. {
  2045. // Build xgmml for this graph... as currently executing...
  2046. CriticalBlock b(crit);
  2047. output->outputBeginNested("Graph", true);
  2048. output->outputString(graphName.length(), graphName.get(), "@id"); // MORE here
  2049. output->outputBool(isActive, "@active");
  2050. output->outputBeginNested("xgmml", true);
  2051. outputChildGraph(output, sequence);
  2052. ForEachItemIn(dependencyIdx, dependencies)
  2053. {
  2054. DebugDependencyRecord &dependency = dependencies.item(dependencyIdx);
  2055. if (!sequence || dependency.sequence > sequence) // MORE - if we start reporting any stats on nodes (seeks? scans?) this may need to change
  2056. {
  2057. DebugActivityRecord *source = allActivities.getValue(dependency.sourceActivity);
  2058. DebugActivityRecord *target = allActivities.getValue(dependency.targetActivity);
  2059. if (source && target)
  2060. {
  2061. output->outputBeginNested("edge", true);
  2062. output->outputCString(dependency.edgeId, "@id");
  2063. output->outputBeginNested("att", false); output->outputCString("_dependsOn", "@name"); output->outputInt(1, "@value"); output->outputEndNested("att");
  2064. output->outputBeginNested("att", false); output->outputCString("_sourceActivity", "@name"); source->outputId(output, "@value"); output->outputEndNested("att");
  2065. output->outputBeginNested("att", false); output->outputCString("_targetActivity", "@name"); target->outputId(output, "@value"); output->outputEndNested("att");
  2066. output->outputEndNested("edge");
  2067. }
  2068. // MORE - work out why sometimes source and/or target is NULL
  2069. }
  2070. }
  2071. output->outputEndNested("xgmml");
  2072. output->outputEndNested("Graph");
  2073. }
  2074. void CBaseDebugGraphManager::searchHistories(IXmlWriter *output, IRowMatcher *matcher, bool fullRows)
  2075. {
  2076. HashIterator edges(allProbes); // MORE - could find ones that have named field
  2077. ForEach(edges)
  2078. {
  2079. IActivityDebugContext *edge = allProbes.mapToValue(&edges.query());
  2080. edge->searchHistories(output, matcher, fullRows);
  2081. }
  2082. ForEachItemIn(idx, childGraphs)
  2083. {
  2084. IDebugGraphManager &graph = childGraphs.item(idx);
  2085. graph.searchHistories(output, matcher, fullRows);
  2086. }
  2087. }
  2088. void CBaseDebugGraphManager::serializeProxyGraphs(MemoryBuffer &buff)
  2089. {
  2090. buff.append(childGraphs.length());
  2091. ForEachItemIn(idx, childGraphs)
  2092. {
  2093. IDebugGraphManager &graph = childGraphs.item(idx);
  2094. buff.append(graph.queryId());
  2095. buff.append((__uint64)graph.queryProxyId());
  2096. }
  2097. }
  2098. void CBaseDebugGraphManager::endChildGraph(IProbeManager *child, IActivityBase *parent)
  2099. {
  2100. CriticalBlock b(crit);
  2101. if (parent)
  2102. {
  2103. Linked<DebugActivityRecord> node = allActivities.getValue(parent);
  2104. if (!node)
  2105. {
  2106. node.setown(new DebugActivityRecord(parent, 0, debugContext->queryChannel(), 0));
  2107. allActivities.setValue(parent, node);
  2108. }
  2109. IDebugGraphManager *childManager = QUERYINTERFACE(child, CBaseDebugGraphManager); // yuk
  2110. node->childGraphs.append(*LINK(childManager));
  2111. }
  2112. }