1
0

y_debug.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*----------------------------------------------------------------------------*\
  2. ==============================
  3. Y Sever Includes - Debug Setup
  4. ==============================
  5. Description:
  6. Ensures debug levels are set and defines debug functions.
  7. General debug levels:
  8. 0 - No debug information.
  9. 1 - Callbacks and timers.
  10. 2 - Remote functions.
  11. 3 - Stock functions.
  12. 4 - Static functions.
  13. 5 - Code.
  14. 6 - Loops.
  15. 7 - Extra loop code.
  16. If you use P:0 you get an optional debug print controlled by the global
  17. state ysi_debug - which is either on or off.
  18. Legal:
  19. Version: MPL 1.1
  20. The contents of this file are subject to the Mozilla Public License Version
  21. 1.1 (the "License"); you may not use this file except in compliance with
  22. the License. You may obtain a copy of the License at
  23. http://www.mozilla.org/MPL/
  24. Software distributed under the License is distributed on an "AS IS" basis,
  25. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  26. for the specific language governing rights and limitations under the
  27. License.
  28. The Original Code is the YSI debug include.
  29. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  30. Portions created by the Initial Developer are Copyright (C) 2011
  31. the Initial Developer. All Rights Reserved.
  32. Contributors:
  33. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  34. Thanks:
  35. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  36. ZeeX - Very productive conversations.
  37. koolk - IsPlayerinAreaEx code.
  38. TheAlpha - Danish translation.
  39. breadfish - German translation.
  40. Fireburn - Dutch translation.
  41. yom - French translation.
  42. 50p - Polish translation.
  43. Zamaroht - Spanish translation.
  44. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  45. for me to strive to better.
  46. Pixels^ - Running XScripters where the idea was born.
  47. Matite - Pestering me to release it and using it.
  48. Very special thanks to:
  49. Thiadmer - PAWN, whose limits continue to amaze me!
  50. Kye/Kalcor - SA:MP.
  51. SA:MP Team past, present and future - SA:MP.
  52. Version:
  53. 1.0
  54. Changelog:
  55. 06/08/10:
  56. Added new syntax.
  57. Added level 0 debugging with state controlled functions.
  58. 15/04/07:
  59. First version.
  60. Functions:
  61. Public:
  62. -
  63. Core:
  64. -
  65. Stock:
  66. -
  67. Static:
  68. -
  69. Inline:
  70. Debug_Code - Runs defined code if a certain level is active.
  71. Debug_Print - Prints the formatted string provided at the given level.
  72. API:
  73. -
  74. Callbacks:
  75. -
  76. Definitions:
  77. P:<0-6> - Print a message.
  78. P:C - Run debug code.
  79. P:E - Print an error message.
  80. P:W - Print a warning message.
  81. Enums:
  82. -
  83. Macros:
  84. -
  85. Tags:
  86. -
  87. Variables:
  88. Global:
  89. -
  90. Static:
  91. -
  92. Commands:
  93. -
  94. Compile options:
  95. _DEBUG - Debugging level to use.
  96. Operators:
  97. -
  98. \*----------------------------------------------------------------------------*/
  99. #include "internal\y_version"
  100. #include "internal\y_funcinc"
  101. #include "y_scriptinit"
  102. #include "internal\y_natives"
  103. #if !defined _DEBUG
  104. #define _DEBUG 0
  105. #endif
  106. #define P:%1(%2); Debug_Print%1(%2);
  107. #define C:%1(%2); Debug_Code%1(%2);
  108. /*----------------------------------------------------------------------------*\
  109. Function:
  110. Debug_Code
  111. Params:
  112. level - Debug level to run the code at.
  113. code - Code to run.
  114. Return:
  115. -
  116. Notes:
  117. Code is not a variable, it's a code chunk and may be written as so:
  118. Debug_Code1(if (bla == 2) { bla++; printf("%d", bla); });
  119. The code must all be on one line to avoid errors.
  120. This isn't really a function as the first parameter is part of the name.
  121. \*----------------------------------------------------------------------------*/
  122. #define DBGC1 Debug_Code1
  123. #define DBGC2 Debug_Code2
  124. #define DBGC3 Debug_Code3
  125. #define DBGC4 Debug_Code4
  126. #define DBGC5 Debug_Code5
  127. #if _DEBUG >= 1
  128. #define Debug_Code1(%1); \
  129. %1
  130. #else
  131. #define Debug_Code1(%1);
  132. #endif
  133. #if _DEBUG >= 2
  134. #define Debug_Code2(%1); \
  135. %1
  136. #else
  137. #define Debug_Code2(%1);
  138. #endif
  139. #if _DEBUG >= 3
  140. #define Debug_Code3(%1); \
  141. %1
  142. #else
  143. #define Debug_Code3(%1);
  144. #endif
  145. #if _DEBUG >= 4
  146. #define Debug_Code4(%1); \
  147. %1
  148. #else
  149. #define Debug_Code4(%1);
  150. #endif
  151. #if _DEBUG >= 5
  152. #define Debug_Code5(%1); \
  153. %1
  154. #else
  155. #define Debug_Code5(%1);
  156. #endif
  157. #if _DEBUG >= 6
  158. #define Debug_Code6(%1); \
  159. %1
  160. #else
  161. #define Debug_Code6(%1);
  162. #endif
  163. /*----------------------------------------------------------------------------*\
  164. Function:
  165. Debug_Print
  166. Params:
  167. level - Debug level to print at.
  168. format[] - Format.
  169. ...
  170. Return:
  171. -
  172. Notes:
  173. This isn't really a function as the first parameter is part of the name:
  174. Debug_Print4("variables: %d, %d", i, j);
  175. \*----------------------------------------------------------------------------*/
  176. #define DBGP1 Debug_Print1
  177. #define DBGP2 Debug_Print2
  178. #define DBGP3 Debug_Print3
  179. #define DBGP4 Debug_Print4
  180. #define DBGP5 Debug_Print5
  181. #if _DEBUG >= 1
  182. #define Debug_Print1(%1); \
  183. printf(%1);
  184. #else
  185. #define Debug_Print1(%1);
  186. #endif
  187. #if _DEBUG >= 2
  188. #define Debug_Print2(%1); \
  189. printf(%1);
  190. #else
  191. #define Debug_Print2(%1);
  192. #endif
  193. #if _DEBUG >= 3
  194. #define Debug_Print3(%1); \
  195. printf(%1);
  196. #else
  197. #define Debug_Print3(%1);
  198. #endif
  199. #if _DEBUG >= 4
  200. #define Debug_Print4(%1); \
  201. printf(%1);
  202. #else
  203. #define Debug_Print4(%1);
  204. #endif
  205. #if _DEBUG >= 5
  206. #define Debug_Print5(%1); \
  207. printf(%1);
  208. #else
  209. #define Debug_Print5(%1);
  210. #endif
  211. #if _DEBUG >= 6
  212. #define Debug_Print6(%1); \
  213. printf(%1);
  214. #else
  215. #define Debug_Print6(%1);
  216. #endif
  217. #if _DEBUG >= 7
  218. #define Debug_Print7(%1); \
  219. printf(%1);
  220. #else
  221. #define Debug_Print7(%1);
  222. #endif
  223. #define Debug_PrintE(%1); \
  224. Debug_Print0("\7\7\7*** YSI Error: " #%1);
  225. #define Debug_PrintW(%1); \
  226. Debug_Print0("\7*** YSI Warning: " #%1);
  227. #define Debug_PrintI(%1); \
  228. Debug_Print0("*** YSI Info: " #%1);
  229. #define Debug_PrintF(%1); \
  230. Debug_Print0("\7\7\7\7\7*** YSI Fatal Error: " #%1);
  231. #define Debug_PrintC(%1); \
  232. Debug_Code1(%1);
  233. stock Debug_Print0(str[], {Float,_}:...) <ysi_debug : on>
  234. {
  235. // This uses the variable parameter passing method based on code by Zeex.
  236. // See page 15 of the code optimisations topic.
  237. new
  238. n = (numargs() - 1) * 4;
  239. if (n)
  240. {
  241. new
  242. arg_start,
  243. arg_end;
  244. //#emit BREAK
  245. // Load the real address of the last static parameter. Do this by
  246. // loading the address of the parameter and then adding the value of
  247. // [FRM] (frame pointer).
  248. #emit CONST.alt str
  249. #emit LCTRL 5
  250. #emit ADD
  251. #emit STOR.S.pri arg_start
  252. // Load the address of the last variable parameter. Do this by adding
  253. // the number of variables on the value just loaded.
  254. #emit LOAD.S.alt n
  255. #emit ADD
  256. #emit STOR.S.pri arg_end
  257. // Push the variable arguments. This is done by loading the value of
  258. // each one in reverse order and pushing them. I'd love to be able to
  259. // rewrite this to use the values of pri and alt for comparison, instead
  260. // of having or constantly reload two variables.
  261. do
  262. {
  263. #emit LOAD.I
  264. #emit PUSH.pri
  265. arg_end -= 4;
  266. #emit LOAD.S.pri arg_end
  267. }
  268. while (arg_end > arg_start);
  269. // Push the static parameter.
  270. #emit PUSH.S str
  271. // Now push the number of parameters sent and call the function.
  272. n += 4;
  273. #emit PUSH.S n
  274. #emit SYSREQ.C printf
  275. // Clear the stack, including the return.
  276. n += 4;
  277. #emit LCTRL 4
  278. #emit LOAD.S.alt n
  279. #emit ADD
  280. #emit SCTRL 4
  281. }
  282. else
  283. {
  284. print(str);
  285. }
  286. }
  287. stock Debug_Print0(str[], {Float,_}:...) <ysi_debug : off>
  288. {
  289. #pragma unused str
  290. }
  291. stock Debug_Print0(str[], {Float,_}:...) <>
  292. {
  293. #pragma unused str
  294. }
  295. stock Debug_PrintArray(arr[], size)
  296. {
  297. new
  298. str[96];
  299. switch (size)
  300. {
  301. case 0:
  302. str = "<>";
  303. case 1:
  304. format(str, sizeof (str), "<%d>", arr[0]);
  305. case 2:
  306. format(str, sizeof (str), "<%d, %d>", arr[0], arr[1]);
  307. case 3:
  308. format(str, sizeof (str), "<%d, %d, %d>", arr[0], arr[1], arr[2]);
  309. case 4:
  310. format(str, sizeof (str), "<%d, %d, %d, %d>", arr[0], arr[1], arr[2], arr[3]);
  311. case 5:
  312. format(str, sizeof (str), "<%d, %d, %d, %d, %d>", arr[0], arr[1], arr[2], arr[3], arr[4]);
  313. default:
  314. format(str, sizeof (str), "<%d, %d, %d, %d, %d, ... (+ %d)>", arr[0], arr[1], arr[2], arr[3], arr[4], size - 5);
  315. }
  316. return str;
  317. }
  318. /*#if _DEBUG > 0
  319. #define Debug_Print0(%1); \
  320. printf(%1);
  321. #endif*/
  322. public OnScriptInit()
  323. {
  324. Debug_SetState();
  325. new
  326. s;
  327. // Test the ADDRESS of the variable, not the value.
  328. #emit CONST.pri YSI_FILTERSCRIPT
  329. #emit STOR.S.pri s
  330. if (s)
  331. {
  332. //goto Debug_OnScriptInit_no_fault();
  333. return Debug_OnScriptInit();
  334. }
  335. P:F("YSI_FILTERSCRIPT == 0");
  336. while (s != 10000000) ++s;
  337. #emit CONST.pri 0
  338. #emit SCTRL 6
  339. return 1;
  340. }
  341. static stock Debug_SetState() <ysi_debug : off>
  342. {
  343. }
  344. static stock Debug_SetState() <>
  345. {
  346. state ysi_debug : on;
  347. }
  348. #undef OnScriptInit
  349. #define OnScriptInit(%0) Debug_OnScriptInit(%0) <_ALS_:hooked>
  350. forward Debug_OnScriptInit();public Debug_OnScriptInit()<_ALS_:unhooked>{return 1;}public Debug_OnScriptInit()<>{return 1;}