y_debug.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. </remarks>
  99. \**--------------------------------------------------------------------------**/
  100. #include "internal\y_version"
  101. #include "internal\y_funcinc"
  102. #include "y_scriptinit"
  103. #include "internal\y_natives"
  104. #if !defined _DEBUG
  105. #define _DEBUG 0
  106. #endif
  107. #define P:%1(%2); Debug_Print%1(%2);
  108. #define C:%1(%2); Debug_Code%1(%2);
  109. /**--------------------------------------------------------------------------**\
  110. <summary>Debug_Code</summary>
  111. <param name="level">Debug level to run the code at.</param>
  112. <param name="code">Code to run.</param>
  113. <returns>
  114. -
  115. </returns>
  116. <remarks>
  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. </remarks>
  122. \**--------------------------------------------------------------------------**/
  123. #define DBGC1 Debug_Code1
  124. #define DBGC2 Debug_Code2
  125. #define DBGC3 Debug_Code3
  126. #define DBGC4 Debug_Code4
  127. #define DBGC5 Debug_Code5
  128. #if _DEBUG >= 1
  129. #define Debug_Code1(%1); \
  130. %1
  131. #else
  132. #define Debug_Code1(%1);
  133. #endif
  134. #if _DEBUG >= 2
  135. #define Debug_Code2(%1); \
  136. %1
  137. #else
  138. #define Debug_Code2(%1);
  139. #endif
  140. #if _DEBUG >= 3
  141. #define Debug_Code3(%1); \
  142. %1
  143. #else
  144. #define Debug_Code3(%1);
  145. #endif
  146. #if _DEBUG >= 4
  147. #define Debug_Code4(%1); \
  148. %1
  149. #else
  150. #define Debug_Code4(%1);
  151. #endif
  152. #if _DEBUG >= 5
  153. #define Debug_Code5(%1); \
  154. %1
  155. #else
  156. #define Debug_Code5(%1);
  157. #endif
  158. #if _DEBUG >= 6
  159. #define Debug_Code6(%1); \
  160. %1
  161. #else
  162. #define Debug_Code6(%1);
  163. #endif
  164. /**--------------------------------------------------------------------------**\
  165. <summary>Debug_Print</summary>
  166. <param name="level">Debug level to print at.</param>
  167. <param name="format[]">Format.</param>
  168. <param name="..."></param>
  169. <returns>
  170. -
  171. </returns>
  172. <remarks>
  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. </remarks>
  176. \**--------------------------------------------------------------------------**/
  177. #define DBGP1 Debug_Print1
  178. #define DBGP2 Debug_Print2
  179. #define DBGP3 Debug_Print3
  180. #define DBGP4 Debug_Print4
  181. #define DBGP5 Debug_Print5
  182. #if _DEBUG >= 1
  183. #define Debug_Print1(%1); \
  184. printf(%1);
  185. #else
  186. #define Debug_Print1(%1);
  187. #endif
  188. #if _DEBUG >= 2
  189. #define Debug_Print2(%1); \
  190. printf(%1);
  191. #else
  192. #define Debug_Print2(%1);
  193. #endif
  194. #if _DEBUG >= 3
  195. #define Debug_Print3(%1); \
  196. printf(%1);
  197. #else
  198. #define Debug_Print3(%1);
  199. #endif
  200. #if _DEBUG >= 4
  201. #define Debug_Print4(%1); \
  202. printf(%1);
  203. #else
  204. #define Debug_Print4(%1);
  205. #endif
  206. #if _DEBUG >= 5
  207. #define Debug_Print5(%1); \
  208. printf(%1);
  209. #else
  210. #define Debug_Print5(%1);
  211. #endif
  212. #if _DEBUG >= 6
  213. #define Debug_Print6(%1); \
  214. printf(%1);
  215. #else
  216. #define Debug_Print6(%1);
  217. #endif
  218. #if _DEBUG >= 7
  219. #define Debug_Print7(%1); \
  220. printf(%1);
  221. #else
  222. #define Debug_Print7(%1);
  223. #endif
  224. #define Debug_PrintE(%1); \
  225. Debug_Print0("\7\7\7*** YSI Error: " #%1);
  226. #define Debug_PrintW(%1); \
  227. Debug_Print0("\7*** YSI Warning: " #%1);
  228. #define Debug_PrintI(%1); \
  229. Debug_Print0("*** YSI Info: " #%1);
  230. #define Debug_PrintF(%1); \
  231. Debug_Print0("\7\7\7\7\7*** YSI Fatal Error: " #%1);
  232. #define Debug_PrintC(%1); \
  233. Debug_Code1(%1);
  234. stock Debug_Print0(str[], {Float,_}:...) <ysi_debug : on>
  235. {
  236. // This uses the variable parameter passing method based on code by Zeex.
  237. // See page 15 of the code optimisations topic.
  238. new
  239. n = (numargs() - 1) * 4;
  240. if (n)
  241. {
  242. new
  243. arg_start,
  244. arg_end;
  245. //#emit BREAK
  246. // Load the real address of the last static parameter. Do this by
  247. // loading the address of the parameter and then adding the value of
  248. // [FRM] (frame pointer).
  249. #emit CONST.alt str
  250. #emit LCTRL 5
  251. #emit ADD
  252. #emit STOR.S.pri arg_start
  253. // Load the address of the last variable parameter. Do this by adding
  254. // the number of variables on the value just loaded.
  255. #emit LOAD.S.alt n
  256. #emit ADD
  257. #emit STOR.S.pri arg_end
  258. // Push the variable arguments. This is done by loading the value of
  259. // each one in reverse order and pushing them. I'd love to be able to
  260. // rewrite this to use the values of pri and alt for comparison, instead
  261. // of having or constantly reload two variables.
  262. do
  263. {
  264. #emit LOAD.I
  265. #emit PUSH.pri
  266. arg_end -= 4;
  267. #emit LOAD.S.pri arg_end
  268. }
  269. while (arg_end > arg_start);
  270. // Push the static parameter.
  271. #emit PUSH.S str
  272. // Now push the number of parameters sent and call the function.
  273. n += 4;
  274. #emit PUSH.S n
  275. #emit SYSREQ.C printf
  276. // Clear the stack, including the return.
  277. n += 4;
  278. #emit LCTRL 4
  279. #emit LOAD.S.alt n
  280. #emit ADD
  281. #emit SCTRL 4
  282. }
  283. else
  284. {
  285. print(str);
  286. }
  287. }
  288. stock Debug_Print0(str[], {Float,_}:...) <ysi_debug : off>
  289. {
  290. #pragma unused str
  291. }
  292. stock Debug_Print0(str[], {Float,_}:...) <>
  293. {
  294. #pragma unused str
  295. }
  296. stock Debug_PrintArray(arr[], size)
  297. {
  298. new
  299. str[96];
  300. switch (size)
  301. {
  302. case 0:
  303. str = "<>";
  304. case 1:
  305. format(str, sizeof (str), "<%d>", arr[0]);
  306. case 2:
  307. format(str, sizeof (str), "<%d, %d>", arr[0], arr[1]);
  308. case 3:
  309. format(str, sizeof (str), "<%d, %d, %d>", arr[0], arr[1], arr[2]);
  310. case 4:
  311. format(str, sizeof (str), "<%d, %d, %d, %d>", arr[0], arr[1], arr[2], arr[3]);
  312. case 5:
  313. format(str, sizeof (str), "<%d, %d, %d, %d, %d>", arr[0], arr[1], arr[2], arr[3], arr[4]);
  314. default:
  315. format(str, sizeof (str), "<%d, %d, %d, %d, %d, ... (+ %d)>", arr[0], arr[1], arr[2], arr[3], arr[4], size - 5);
  316. }
  317. return str;
  318. }
  319. /*#if _DEBUG > 0
  320. #define Debug_Print0(%1); \
  321. printf(%1);
  322. #endif*/
  323. public OnScriptInit()
  324. {
  325. Debug_SetState();
  326. new
  327. s;
  328. // Test the ADDRESS of the variable, not the value.
  329. #emit CONST.pri YSI_FILTERSCRIPT
  330. #emit STOR.S.pri s
  331. if (s)
  332. {
  333. //goto Debug_OnScriptInit_no_fault();
  334. #if defined Debug_OnScriptInit
  335. return Debug_OnScriptInit();
  336. #else
  337. return 1;
  338. #endif
  339. }
  340. P:F("YSI_FILTERSCRIPT == 0");
  341. while (s != 10000000) ++s;
  342. #emit CONST.pri 0
  343. #emit SCTRL 6
  344. return 1;
  345. }
  346. static stock Debug_SetState() <ysi_debug : off>
  347. {
  348. }
  349. static stock Debug_SetState() <>
  350. {
  351. state ysi_debug : on;
  352. }
  353. #undef OnScriptInit
  354. #define OnScriptInit Debug_OnScriptInit
  355. #if defined Debug_OnScriptInit
  356. forward Debug_OnScriptInit();
  357. #endif