Wiki source code of Debugging

Last modified by Thomas Mortagne on 2025/01/16 16:28

Show last authors
1 There are several possibilities for debugging XWiki:
2
3 {{toc/}}
4
5 = Debug mode =
6
7 It's possible to enable debug log by adding ##debug=true## (as in [[http://127.0.0.1:8080/xwiki/bin/Main/WebHome?debug=true]]) to the URL. This provide various details on what exactly happens during the request.
8
9 == Elapsed time tree ==
10
11 A detailed tree of how long has been spent in each step. You can add you own step to the tree by using [[progress API>>extensions:Extension.Job Module||anchor="HNotifyaboutprogress"]].
12
13 {{image reference="elapsedtimetree.png"/}}
14
15 = Debugging XWiki from your IDE =
16
17 == Debugging with Eclipse ==
18
19 Once you've gotten the debugger working, you'll wonder how you ever survived without it.
20
21 === Complete tutorial based on M2E and WTP ===
22
23 [[Debug XWiki with Eclipse>>DebugXEWithEclipse]]
24
25 === Debug a XWiki released version ===
26
27 * Follow the steps from [[Building In Eclipse>>BuildingInEclipse]] including the optional "Import the WAR as a web project".
28 * Select from the Eclipse menu **Run > Debug...**
29 * Create a new configuration for your server
30 * Hit the **Debug** button
31 * Set breakpoints, step through code, have fun!
32
33 WTP will deploy XWiki into ##{workspace_location}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps/xwiki##. If you suspect a change has not been deployed correctly (e.g. a change to a config file), check there to confirm.
34
35 If you are working on an XWiki plugin in a separate project in your workspace, you can ensure it is automatically deployed to XWiki's ##WEB-INF/lib## directory when debugging. Open the project properties of the XWiki project resulting from the WAR import above, go to the ##J2EE Module Dependencies## section, and add your plugin project.
36
37 === Using Firebug when debugging a selenium test ===
38
39 For that you will need to install Firebug [[as a global extension>>http://kb.mozillazine.org/Installing_extensions#Global_installation]]
40
41 = Remote Debugging =
42
43 To perform remote debugging, start your wiki in debug mode. To do this you can:
44
45 * use the ##start_xwiki_debug.sh## executable if you're using the Standalone packaging
46 * modify the way you start your container and ensure that the following JVM parameters are passed:(((
47 {{code}}
48 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
49 {{/code}}
50
51 For example, for Jetty you could use the following start script:
52
53 {{code}}
54 #!/bin/sh
55
56 JETTY_HOME=.
57 JETTY_PORT=8080
58 JAVA_OPTS="-Xmx300m -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
59
60 java $JAVA_OPTS -Dfile.encoding=iso-8859-1 -Djetty.port=$JETTY_PORT -Djetty.home=$JETTY_HOME -jar $JETTY_HOME/start.jar
61 {{/code}}
62
63 If you are using the Tomcat service on Windows, you should modify the JVM args with the Tomcat Service Configuration Panel. Select the Java tab and add each of the options **on a separate line**. For example:
64
65 {{code}}
66 -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 5.5
67 -Dcatalina.base=C:\Program Files\Apache Software Foundation\Tomcat 5.5
68 -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\endorsed
69 -Djava.io.tmpdir=C:\Program Files\Apache Software Foundation\Tomcat 5.5\temp
70 -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
71 -Djava.util.logging.config.file=C:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\logging.properties
72 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
73 {{/code}}
74 )))
75
76 Then, in your favorite IDE, open the XWiki project and run a remote debugging session, attached to the socket on port 5005. For example, if you're using IntelliJ IDEA, go to ##Run|Edit Configurations...## and create a new ##Remote## configuration (default parameters should be fine). Then execute it and IDEA will connect to the executing JVM.
77
78 You can then place breakpoints in your source code.
79
80 Debugging options for ##start_xwiki_debug.sh##:
81
82 * Use the ##~-~-suspend## parameter to tell the JVM to wait until someone connect to the debugging port before progressing.
83 * Use the ##JETTY_DEBUG_PORT## environment variable to control the JVM debug port to use (default is ##5005##).
84 * {{version since="12.8RC1"}}Use the ##-dp <number>## (or ##-~-debugPort <number>##) parameter to control the JVM debug port to use (default is ##5005##).{{/version}}
85
86 = Logging =
87
88 == Turning on logging inside XWiki ==
89
90 See the [[Logging page in the Admin guide>>xwiki:Documentation.AdminGuide.Logging]].
91
92 == Logging shutdown operations ==
93
94 To log debug information of what happens when there are shutdown operations, simply set the log level to ##DEBUG## for the ##org.xwiki.shutdown## logger:
95
96 {{code language="xml"}}
97 <logger name="org.xwiki.shutdown" level="info"/>
98 {{/code}}
99
100 == Logging calls at the database level ==
101
102 You have several options:
103
104 * Configure Hibernate to log SQL calls. To do this edit XWiki's log configuration file (see the [[Logging page>>xwiki:Documentation.AdminGuide.Logging]]) and change the log level from ##warn## to ##trace## for the following:(((
105 {{code language="none"}}
106 <!-- Log HQL query parser activity -->
107 <logger name="org.hibernate.hql.ast.AST" level="warn"/>
108 <!-- Log just the SQL -->
109 <logger name="org.hibernate.SQL" level="warn"/>
110 <!-- Log JDBC bind parameters -->
111 <logger name="org.hibernate.type" level="warn"/>
112 <!-- Log schema export/update -->
113 <logger name="org.hibernate.tool.hbm2ddl" level="warn"/>
114 <!-- Log HQL parse trees -->
115 <logger name="org.hibernate.hql" level="warn"/>
116 <!-- Log cache activity -->
117 <logger name="org.hibernate.cache" level="warn"/>
118 <!-- Log transaction activity -->
119 <logger name="org.hibernate.transaction" level="warn"/>
120 <!-- Log JDBC resource acquisition -->
121 <logger name="org.hibernate.jdbc" level="warn"/>
122 <!-- Enable the following line if you want to track down connection leakages when using
123 DriverManagerConnectionProvider -->
124 <logger name="org.hibernate.connection.DriverManagerConnectionProvider" level="warn"/>
125 <!-- Log prepared statement cache activity -->
126 <logger name="org.hibernate.ps.PreparedStatementCache" level="warn"/>
127 {{/code}}
128 )))
129 * Use a wrapping JDBC driver such as [[log4jdbc>>http://code.google.com/p/log4jdbc/]] or [[p6spy>>http://sourceforge.net/projects/p6spy/]].
130 * (((
131 Turn on logging in your database. The configuration is database-dependent*
132
133 (((
134 For **HSQLDB**. Two solutions:* Solution 1: Edit ##WEB-INF/hibernate.cfg.xml## and modify the URL connection property to add ##hsqldb.sqllog=3##, as in:
135
136 (((
137 {{code language="none"}}
138 <property name="connection.url">jdbc:hsqldb:file:${environment.permanentDirectory}/database/xwiki_db;shutdown=true;hsqldb.sqllog=3</property>
139 {{/code}}
140 )))
141
142 * Solution 2: Using Byteman (See the section below for more information). For example the following rule (tested on HSQLDB 2.3.1) will log SQL statements with parameters and schema when there's more than 1 parameter:(((
143 {{code language="none"}}
144 RULE Log SQL Statement
145 CLASS org.hsqldb.Session
146 METHOD executeCompiledStatement(org.hsqldb.Statement, Object[], int)
147 AT ENTRY
148 IF $2.length > 0
149 DO traceln("SQL Statement = [" + $1.getSQL() + "], parameter 1 = [" + $2[0] + "], schema = [" + $1.getSchemaName().name + "]")
150 ENDRULE
151 {{/code}}
152
153 Will give for example:
154
155 {{code language="none"}}
156 SQL Statement = [select xwikidocum0_.XWD_ID as XWD1_0_0_, xwikidocum0_.XWD_FULLNAME as XWD2_0_0_, xwikidocum0_.XWD_NAME as XWD3_0_0_, xwikidocum0_.XWD_TITLE as XWD4_0_0_, xwikidocum0_.XWD_LANGUAGE as XWD5_0_0_, xwikidocum0_.XWD_DEFAULT_LANGUAGE as XWD6_0_0_, xwikidocum0_.XWD_TRANSLATION as XWD7_0_0_, xwikidocum0_.XWD_DATE as XWD8_0_0_, xwikidocum0_.XWD_CONTENT_UPDATE_DATE as XWD9_0_0_, xwikidocum0_.XWD_CREATION_DATE as XWD10_0_0_, xwikidocum0_.XWD_AUTHOR as XWD11_0_0_, xwikidocum0_.XWD_CONTENT_AUTHOR as XWD12_0_0_, xwikidocum0_.XWD_CREATOR as XWD13_0_0_, xwikidocum0_.XWD_WEB as XWD14_0_0_, xwikidocum0_.XWD_CONTENT as XWD15_0_0_, xwikidocum0_.XWD_VERSION as XWD16_0_0_, xwikidocum0_.XWD_CUSTOM_CLASS as XWD17_0_0_, xwikidocum0_.XWD_PARENT as XWD18_0_0_, xwikidocum0_.XWD_CLASS_XML as XWD19_0_0_, xwikidocum0_.XWD_ELEMENTS as XWD20_0_0_, xwikidocum0_.XWD_DEFAULT_TEMPLATE as XWD21_0_0_, xwikidocum0_.XWD_VALIDATION_SCRIPT as XWD22_0_0_, xwikidocum0_.XWD_COMMENT as XWD23_0_0_, xwikidocum0_.XWD_MINOREDIT as XWD24_0_0_, xwikidocum0_.XWD_SYNTAX_ID as XWD25_0_0_, xwikidocum0_.XWD_HIDDEN as XWD26_0_0_ from xwikidoc xwikidocum0_ where xwikidocum0_.XWD_ID=?], parameter 1 = [-4526159677276379501], schema = [TEST91]
157 {{/code}}
158 )))
159 )))
160 )))
161
162 == Logging HTTP calls ==
163
164 XWiki uses [[HTTP Client>>https://hc.apache.org/httpcomponents-client-ga/]] in most places and logging what it does is easy. You'll need to add the following to XWiki's ##logback.xml## file:
165
166 {{code language="xml"}}
167 <logger name="org.apache.http" level="debug"/>
168 <logger name="org.apache.http.headers" level="debug"/>
169 <logger name="org.apache.http.wire" level="debug"/>
170 {{/code}}
171
172 == Using Byteman ==
173
174 [[Byteman>>https://www.jboss.org/byteman]] is a great framework that allows to modify bytecode in a running JVM. It can easily be used to add logging (for ex) to find out what's happening in a running XWiki instance. Here's a quick tutorial:
175
176 * Download Byteman and unzip it in a directory. Set the ##$BYTEMAN_HOME## environment property to point to the directory where you've unzipped it. For example:
177 {{code}}export BYTEMAN_HOME=/Users/vmassol/dev/byteman/byteman-download-2.1.0{{/code}}
178 * Start an XWiki instance somewhere on the same machine
179 * Go to ##$BYTEMAN_HOME/bin## and connect Byteman to XWiki. For example:
180 {{code}}sh bminstall.sh -b -Dorg.jboss.byteman.transform.all jetty/start.jar{{/code}}
181 * Now create a Byteman rule by creating a file, for example ##my.btm## with the following:(((
182 {{code}}
183 RULE XWiki Docs loaded
184 CLASS XWikiHibernateStore
185 METHOD loadXWikiDoc
186 AT ENTRY
187 IF TRUE
188 DO traceln("Load document = " + $1.getDocumentReference())
189 ENDRULE
190 {{/code}}
191 )))
192 * Load this rule with: {{code}}sh bmsubmit.sh -l my.btm{{/code}}
193 * Call a URL in your wiki and check that the console prints the document references that get loaded! For example:(((
194 {{code}}
195 Load document = xwiki:Main.WebPreferences
196 Load document = xwiki:ColorThemes.DefaultColorTheme
197 Load document = xwiki:XWiki.SearchSuggestConfig
198 Load document = xwiki:XWiki.SearchSuggestConfig
199 Load document = xwiki:Dashboard.WebHome
200 Load document = xwiki:XWiki.GadgetClass
201 Load document = xwiki:Dashboard.WebHome
202 Load document = xwiki:Dashboard.WebPreferences
203 Load document = xwiki:Dashboard.colibri
204 Load document = xwiki:Main.Welcome
205 ...
206 {{/code}}
207 )))
208 * Modify your rule or add new rules to the same file and apply the changes with {{code}}sh bmsubmit.sh -l my.btm{{/code}}
209 * Remove your rules with {{code}}sh bmsubmit.sh -u{{/code}}
210
211 For more read [[A Byteman tutorial>>https://community.jboss.org/wiki/ABytemanTutorial]] or the [[Reference documentation>>http://downloads.jboss.org/byteman/2.0.1/ProgrammersGuideSinglePage.html]].
212
213 === Examples ===
214
215 ==== Example 1 ====
216
217 In this example we'll print calls to ##CommonsConfigurationSource.containsKey(...)## with the parameters printed and the time it takes to execute. In addition we'll also print calls to ##XWiki.getDocument(String, XWikiContext)## which is a deprecated method which, for the purpose of this example, we think generate the calls to ##containsKey##.
218
219 {{code}}
220 RULE Create timer for containsKey
221 CLASS CommonsConfigurationSource
222 METHOD containsKey
223 AT ENTRY
224 IF TRUE
225 DO resetTimer("containsKey")
226 ENDRULE
227
228 RULE Calls to containsKey
229 CLASS CommonsConfigurationSource
230 METHOD containsKey
231 AT EXIT
232 IF TRUE
233 DO traceln("containsKey for [" + $1 + "] = [" + $! + "], time = [" + getElapsedTimeFromTimer("containsKey") + "]")
234 ENDRULE
235
236 RULE Calls to deprecated getDocument
237 CLASS com.xpn.xwiki.XWiki
238 METHOD getDocument(String, XWikiContext)
239 AT ENTRY
240 IF TRUE
241 DO traceStack("getDocument() called for [" + $1 + "] ", 3)
242 ENDRULE
243 {{/code}}
244
245 Generates:
246
247 {{code}}
248 ...
249 getDocument() called for [XWiki.Admin] com.xpn.xwiki.XWiki.getDocument(XWiki.java:-1)
250 com.xpn.xwiki.XWiki.getUserName(XWiki.java:5072)
251 com.xpn.xwiki.XWiki.getUserName(XWiki.java:5062)
252 . . .
253 containsKey for [model.reference.default.document] = [false], time = [0]
254 containsKey for [model.reference.default.document] = [false], time = [0]
255 containsKey for [model.reference.default.space] = [false], time = [0]
256 containsKey for [model.reference.default.space] = [false], time = [0]
257 containsKey for [model.reference.default.wiki] = [false], time = [0]
258 containsKey for [model.reference.default.wiki] = [false], time = [1]
259 getDocument() called for [XWiki.DefaultSkin] com.xpn.xwiki.XWiki.getDocument(XWiki.java:-1)
260 com.xpn.xwiki.XWiki.getSkinFile(XWiki.java:1955)
261 com.xpn.xwiki.XWiki.getSkinFile(XWiki.java:1918)
262 . . .
263 ...
264 {{/code}}
265
266 ==== Example 2 ====
267
268 Log calls to ##XWikiDocument#setLock()## and ##XWikiDocument#removeLock()##.
269
270 {{code}}
271 RULE XWiki Set Lock
272 CLASS XWikiDocument
273 METHOD setLock
274 AT ENTRY
275 IF TRUE
276 DO traceln("set lock for user [" + $1 + "], doc = [" + $this + "]")
277 ENDRULE
278
279 RULE XWiki Remove Lock
280 CLASS XWikiDocument
281 METHOD removeLock
282 AT ENTRY
283 IF TRUE
284 DO traceln("remove lock for doc = [" + $this + "]")
285 ENDRULE
286
287 RULE XWiki Get Lock
288 CLASS XWikiDocument
289 METHOD getLock
290 AT ENTRY
291 IF TRUE
292 DO traceln("get lock for doc = [" + $this + "]")
293 ENDRULE
294 {{/code}}
295
296 When you click edit and then cancel on a doc you get for example:
297
298 {{code}}
299 // Edit
300 get lock for doc = [Sandbox.WebHome]
301 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
302 get lock for doc = [Sandbox.WebHome]
303 get lock for doc = [Sandbox.WebHome]
304 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
305
306 // Cancel
307 get lock for doc = [Sandbox.WebHome]
308 remove lock for doc = [Sandbox.WebHome]
309 {{/code}}
310
311 When you click edit and then save on a doc you get for example:
312
313 {{code}}
314 // Edit
315 get lock for doc = [Sandbox.WebHome]
316 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
317 get lock for doc = [Sandbox.WebHome]
318 get lock for doc = [Sandbox.WebHome]
319 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
320
321 // Save
322 get lock for doc = [Sandbox.WebHome]
323 remove lock for doc = [Sandbox.WebHome]
324 get lock for doc = [Sandbox.WebHome]
325 {{/code}}
326
327 === Troubleshooting ===
328
329 ==== NoClassDefFoundError: com/sun/tools/attach/AttachNotSupportedException ====
330
331 If you get the following exception, it can mean that you're on Mac and that your ##tools.jar## is not added to the classpath. Apparently ##bminstall.sh## thinks that if you're on Mac then you don't need ##tools.jar## which is apparently wrong nowadays.
332
333 {{code language="none"}}
334 Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/AttachNotSupportedException
335 at java.lang.Class.getDeclaredMethods0(Native Method)
336 at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
337 at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
338 at java.lang.Class.getMethod0(Class.java:3018)
339 at java.lang.Class.getMethod(Class.java:1784)
340 at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
341 at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
342 Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.AttachNotSupportedException
343 at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
344 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
345 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
346 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
347 ... 7 more
348 {{/code}}
349
350 In this case, edit ##bminstall.sh## and add the following line below the ##OS=`uname`## line in order to override it:
351
352 {{code language="none"}}
353 OS=whatever
354 {{/code}}
355
356 = Debugging JavaScript =
357
358 One solution is to use the developer tools provided by the Browser for inspecting JavaScript and putting breakpoints in it. By default the XWiki build will minify and aggregate JavaScript files for performance reasons.
359
360 Open the developer tools provided by the browser and put the breakpoints using the Debugger / Sources tab. Look for the //non-minified// (source) JavaScript code (Ctrl+P). You should be able to find it most of the time because the minified version (which is used by default) indicates its source map which in turn indicates the source code. So the browser ends up loading all 3 when the developer tools are open (shouldn't be the case for end users), which means you can run the minified version while viewing the progress step by step over the source (non-minified) version. If the source map is missing or the source URL is wrong then you can still debug step by step using the source code by adding ##?minify=false## to the URL of the current page. This will force the browser to load the non-minified version (and only that).
361
362 Note that JavaScript minification is not only about removing whitespace and renaming local variables. Depending on the minifier used it may also mean code optimizations, syntax suggar replacement, polyfill injection, etc. So the differences between the minified version and the source code could be significant. As a consequence:
363
364 * debugging step by step may behave strangely when there's no 1 to 1 mapping between the minified code and its source
365 * the non-minified (source) version might not work on all browsers; that's why we recommend using ##?minify=false## only with the latest versions of Firefox and Chrome.
366
367 You can also set ##debug.minify## to ##false## in ##xwiki.properties## file instead of passing ##?minify=false## in the URL.
368
369 == Debugging Vue.js ==
370
371 The first step is to load a non-minified version of ##vue.js##. To do so, edit ##webapps/xwiki/resources/uicomponents/widgets/liveData.min.js## and replace {{code}}'vue': $services.webjars.url('vue', 'vue.min'){{/code}} by {{code}}'vue': $services.webjars.url('vue', 'vue'){{/code}}.
372 Then, install [[Vue Devtools>>https://devtools.vuejs.org/]] in Firefox or Chrome.
373 You should now have access to the Vue tab in you browser debugger, with the ability to inspect the Vue applications deployed in the page.
374
375 = Debugging the WYSIWYG editor =
376
377 Example to debug JS code in ##macroSelector.js##:
378
379 * Modify the top level CKEditor ##pom.xml## to remove minification (the important part is to add {{code}}--leave-js-unminified{{/code}} as on the example below):(((
380 {{code language="xml"}}
381 ...
382 <plugin>
383 <groupId>org.codehaus.mojo</groupId>
384 <artifactId>exec-maven-plugin</artifactId>
385 <executions>
386 <!-- Build the CKEditor. -->
387 <execution>
388 <id>build-ckeditor</id>
389 <!-- Use a phase after compile because the Clirr Maven Plugin executes all the phases up to compile twice. -->
390 <phase>process-classes</phase>
391 <goals>
392 <goal>exec</goal>
393 </goals>
394 <configuration>
395 <executable>${ckeditor.builder.path}/build.sh</executable>
396 <arguments>
397 <!-- Exclude from release all plugins/skins that are not specified in build-config. -->
398 <argument>--skip-omitted-in-build-config</argument>
399 <argument>-s --leave-js-unminified</argument>
400 </arguments>
401 </configuration>
402 </execution>
403 </executions>
404 </plugin>
405 ...
406 {{/code}}
407 )))
408 * Rebuild the ##application-ckeditor-webjar## module (you'll also need to rebuild##application-ckeditor-plugins## if you made changes to it).
409 * Copy the generated ##application-ckeditor-webjar-<version>.jar## to an XWiki instance, in ##WEB-INF/lib## and restart XWiki
410 ** You could also remove the CKEditor extensions in the permanent's directory (in ##extensions/repository##) to be safe.
411 * In the wiki, edit ##CKEditor.EditSheet## in object mode and look for ###set ($macroWizardBundlePath = "${ckeditorBasePath}plugins/xwiki-macro/macroWizard.bundle.min")## and remove the ##.min## part. Note that in this case you have to know that the macro wizard is loaded separately in ##macroWizard.bundle.min.js##. Otherwise the JS is in ##ckeditor.js##.
412 * Use your browser's dev tools to put breakpoints in the JS.
413
414 = Analyze Out Of Memory issues =
415
416 You can enable automatic memory dump when using ##-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/where/to/save/the/memory/dumps ## which will generate a memory dump as soon as Java detect an Out Of memory error. See https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/clopts001.html#CHDFDIJI for more details.
417
418 This option is enabled by default in Jetty based distributions of XWiki, and the memory dump will ends up in ##data/## folder.
419
420 You can then navigate and analyze this memory dump with various tools, like [[Eclipse Memory Analyzer>>https://eclipse.dev/mat/]].

Get Connected