Show last authors
1 There are several possibilities for debugging XWiki:
2
3 {{toc/}}
4
5 = Debug mode =
6
7 [Since 7.1]
8
9 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.
10
11 == Elapsed time tree ==
12
13 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"]].
14
15 {{image reference="elapsedtimetree.png"/}}
16
17 = Debugging XWiki from your IDE =
18
19 == Debugging with Eclipse ==
20
21 Once you've gotten the debugger working, you'll wonder how you ever survived without it.
22
23 === Complete tutorial based on M2E and WTP ===
24
25 [[Debug XWiki with Eclipse>>DebugXEWithEclipse]]
26
27 === Debug a XWiki released version ===
28
29 * Follow the steps from [[Building In Eclipse>>BuildingInEclipse]] including the optional "Import the WAR as a web project".
30 * Select from the Eclipse menu **Run > Debug...**
31 * Create a new configuration for your server
32 * Hit the **Debug** button
33 * Set breakpoints, step through code, have fun!
34
35 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.
36
37 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.
38
39 === Using Firebug when debugging a selenium test ===
40
41 For that you will need to install Firebug [[as a global extension>>http://kb.mozillazine.org/Installing_extensions#Global_installation]]
42
43 = Remote Debugging =
44
45 To perform remote debugging, start your wiki in debug mode. To do this you can:
46 * use the ##start_xwiki_debug.sh## executable if you're using the Standalone packaging
47 * modify the way you start your container and ensure that the following JVM parameters are passed:(((
48 {{code}}
49 -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
50 {{/code}}
51
52 For example, for Jetty you could use the following start script:
53
54 {{code}}
55 #!/bin/sh
56
57 JETTY_HOME=.
58 JETTY_PORT=8080
59 JAVA_OPTS="-Xmx300m -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
60
61 java $JAVA_OPTS -Dfile.encoding=iso-8859-1 -Djetty.port=$JETTY_PORT -Djetty.home=$JETTY_HOME -jar $JETTY_HOME/start.jar
62 {{/code}}
63
64 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:
65
66 {{code}}
67 -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 5.5
68 -Dcatalina.base=C:\Program Files\Apache Software Foundation\Tomcat 5.5
69 -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\endorsed
70 -Djava.io.tmpdir=C:\Program Files\Apache Software Foundation\Tomcat 5.5\temp
71 -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
72 -Djava.util.logging.config.file=C:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\logging.properties
73 -Xdebug
74 -Xnoagent
75 -Djava.compiler=NONE
76 -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
77 {{/code}}
78 )))
79
80 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.
81
82 You can then place breakpoints in your source code.
83
84 Debugging options for ##start_xwiki_debug.sh##:
85 * Use the ##-~-suspend## parameter to tell the JVM to wait until someone connect to the debugging port before progressing.
86 * Use the ##JETTY_DEBUG_PORT## environment variable to control the JVM debug port to use (default is ##5005##).
87 * {{version since='12.8RC1'}}Use the ##-dp <number>## (or ##-~-debugPort <number>##) parameter to control the JVM debug port to use (default is ##5005##).{{/version}}
88
89 = Logging =
90
91 == Turning on logging inside XWiki ==
92
93 See the [[Logging page in the Admin guide>>platform:AdminGuide.Logging]].
94
95 == Logging shutdown operations ==
96
97 {{info}}XWiki 6.3M1+{{/info}} 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:
98
99 {{code language="xml"}}
100 <logger name="org.xwiki.shutdown" level="info"/>
101 {{/code}}
102
103 == Logging calls at the database level ==
104
105 You have several options:
106
107 * Configure Hibernate to log SQL calls. To do this edit XWiki's log configuration file (see the [[Logging page>>platform:AdminGuide.Logging]]) and change the log level from ##warn## to ##trace## for the following:(((
108 {{code language="none"}}
109 <!-- Log HQL query parser activity -->
110 <logger name="org.hibernate.hql.ast.AST" level="warn"/>
111 <!-- Log just the SQL -->
112 <logger name="org.hibernate.SQL" level="warn"/>
113 <!-- Log JDBC bind parameters -->
114 <logger name="org.hibernate.type" level="warn"/>
115 <!-- Log schema export/update -->
116 <logger name="org.hibernate.tool.hbm2ddl" level="warn"/>
117 <!-- Log HQL parse trees -->
118 <logger name="org.hibernate.hql" level="warn"/>
119 <!-- Log cache activity -->
120 <logger name="org.hibernate.cache" level="warn"/>
121 <!-- Log transaction activity -->
122 <logger name="org.hibernate.transaction" level="warn"/>
123 <!-- Log JDBC resource acquisition -->
124 <logger name="org.hibernate.jdbc" level="warn"/>
125 <!-- Enable the following line if you want to track down connection leakages when using
126 DriverManagerConnectionProvider -->
127 <logger name="org.hibernate.connection.DriverManagerConnectionProvider" level="warn"/>
128 <!-- Log prepared statement cache activity -->
129 <logger name="org.hibernate.ps.PreparedStatementCache" level="warn"/>
130 {{/code}}
131 )))
132 * Use a wrapping JDBC driver such as [[log4jdbc>>http://code.google.com/p/log4jdbc/]] or [[p6spy>>http://sourceforge.net/projects/p6spy/]].
133 * (((
134 Turn on logging in your database. The configuration is database-dependent* (((
135 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:(((
136 {{code language="none"}}
137 <property name="connection.url">jdbc:hsqldb:file:${environment.permanentDirectory}/database/xwiki_db;shutdown=true;hsqldb.sqllog=3</property>
138 {{/code}}
139 )))
140 * 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:(((
141 {{code language="none"}}
142 RULE Log SQL Statement
143 CLASS org.hsqldb.Session
144 METHOD executeCompiledStatement(org.hsqldb.Statement, Object[], int)
145 AT ENTRY
146 IF $2.length > 0
147 DO traceln("SQL Statement = [" + $1.getSQL() + "], parameter 1 = [" + $2[0] + "], schema = [" + $1.getSchemaName().name + "]")
148 ENDRULE
149 {{/code}}
150
151 Will give for example:
152
153 {{code language="none"}}
154 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]
155 {{/code}}
156 )))
157 )))
158 )))
159
160 == Logging HTTP calls ==
161
162 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:
163
164 {{code language="xml"}}
165 <logger name="org.apache.http" level="debug"/>
166 <logger name="org.apache.http.headers" level="debug"/>
167 <logger name="org.apache.http.wire" level="debug"/>
168 {{/code}}
169
170 == Using Byteman ==
171
172 [[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:
173
174 * 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:
175 {{code}}export BYTEMAN_HOME=/Users/vmassol/dev/byteman/byteman-download-2.1.0{{/code}}
176 * Start an XWiki instance somewhere on the same machine
177 * Go to ##$BYTEMAN_HOME/bin## and connect Byteman to XWiki. For example:
178 {{code}}sh bminstall.sh -b -Dorg.jboss.byteman.transform.all jetty/start.jar{{/code}}
179 * Now create a Byteman rule by creating a file, for example ##my.btm## with the following:(((
180 {{code}}
181 RULE XWiki Docs loaded
182 CLASS XWikiHibernateStore
183 METHOD loadXWikiDoc
184 AT ENTRY
185 IF TRUE
186 DO traceln("Load document = " + $1.getDocumentReference())
187 ENDRULE
188 {{/code}}
189 )))
190 * Load this rule with: {{code}}sh bmsubmit.sh -l my.btm{{/code}}
191 * Call a URL in your wiki and check that the console prints the document references that get loaded! For example:(((
192 {{code}}
193 Load document = xwiki:Main.WebPreferences
194 Load document = xwiki:ColorThemes.DefaultColorTheme
195 Load document = xwiki:XWiki.SearchSuggestConfig
196 Load document = xwiki:XWiki.SearchSuggestConfig
197 Load document = xwiki:Dashboard.WebHome
198 Load document = xwiki:XWiki.GadgetClass
199 Load document = xwiki:Dashboard.WebHome
200 Load document = xwiki:Dashboard.WebPreferences
201 Load document = xwiki:Dashboard.colibri
202 Load document = xwiki:Main.Welcome
203 ...
204 {{/code}}
205 )))
206 * Modify your rule or add new rules to the same file and apply the changes with {{code}}sh bmsubmit.sh -l my.btm{{/code}}
207 * Remove your rules with {{code}}sh bmsubmit.sh -u{{/code}}
208
209 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]].
210
211 === Examples ===
212
213 ==== Example 1 ====
214
215 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##.
216
217 {{code}}
218 RULE Create timer for containsKey
219 CLASS CommonsConfigurationSource
220 METHOD containsKey
221 AT ENTRY
222 IF TRUE
223 DO resetTimer("containsKey")
224 ENDRULE
225
226 RULE Calls to containsKey
227 CLASS CommonsConfigurationSource
228 METHOD containsKey
229 AT EXIT
230 IF TRUE
231 DO traceln("containsKey for [" + $1 + "] = [" + $! + "], time = [" + getElapsedTimeFromTimer("containsKey") + "]")
232 ENDRULE
233
234 RULE Calls to deprecated getDocument
235 CLASS com.xpn.xwiki.XWiki
236 METHOD getDocument(String, XWikiContext)
237 AT ENTRY
238 IF TRUE
239 DO traceStack("getDocument() called for [" + $1 + "] ", 3)
240 ENDRULE
241 {{/code}}
242
243 Generates:
244
245 {{code}}
246 ...
247 getDocument() called for [XWiki.Admin] com.xpn.xwiki.XWiki.getDocument(XWiki.java:-1)
248 com.xpn.xwiki.XWiki.getUserName(XWiki.java:5072)
249 com.xpn.xwiki.XWiki.getUserName(XWiki.java:5062)
250 . . .
251 containsKey for [model.reference.default.document] = [false], time = [0]
252 containsKey for [model.reference.default.document] = [false], time = [0]
253 containsKey for [model.reference.default.space] = [false], time = [0]
254 containsKey for [model.reference.default.space] = [false], time = [0]
255 containsKey for [model.reference.default.wiki] = [false], time = [0]
256 containsKey for [model.reference.default.wiki] = [false], time = [1]
257 getDocument() called for [XWiki.DefaultSkin] com.xpn.xwiki.XWiki.getDocument(XWiki.java:-1)
258 com.xpn.xwiki.XWiki.getSkinFile(XWiki.java:1955)
259 com.xpn.xwiki.XWiki.getSkinFile(XWiki.java:1918)
260 . . .
261 ...
262 {{/code}}
263
264 ==== Example 2 ====
265
266 Log calls to ##XWikiDocument#setLock()## and ##XWikiDocument#removeLock()##.
267
268 {{code}}
269 RULE XWiki Set Lock
270 CLASS XWikiDocument
271 METHOD setLock
272 AT ENTRY
273 IF TRUE
274 DO traceln("set lock for user [" + $1 + "], doc = [" + $this + "]")
275 ENDRULE
276
277 RULE XWiki Remove Lock
278 CLASS XWikiDocument
279 METHOD removeLock
280 AT ENTRY
281 IF TRUE
282 DO traceln("remove lock for doc = [" + $this + "]")
283 ENDRULE
284
285 RULE XWiki Get Lock
286 CLASS XWikiDocument
287 METHOD getLock
288 AT ENTRY
289 IF TRUE
290 DO traceln("get lock for doc = [" + $this + "]")
291 ENDRULE
292 {{/code}}
293
294 When you click edit and then cancel on a doc you get for example:
295
296 {{code}}
297 // Edit
298 get lock for doc = [Sandbox.WebHome]
299 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
300 get lock for doc = [Sandbox.WebHome]
301 get lock for doc = [Sandbox.WebHome]
302 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
303
304 // Cancel
305 get lock for doc = [Sandbox.WebHome]
306 remove lock for doc = [Sandbox.WebHome]
307 {{/code}}
308
309 When you click edit and then save on a doc you get for example:
310
311 {{code}}
312 // Edit
313 get lock for doc = [Sandbox.WebHome]
314 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
315 get lock for doc = [Sandbox.WebHome]
316 get lock for doc = [Sandbox.WebHome]
317 set lock for user [XWiki.Admin], doc = [Sandbox.WebHome]
318
319 // Save
320 get lock for doc = [Sandbox.WebHome]
321 remove lock for doc = [Sandbox.WebHome]
322 get lock for doc = [Sandbox.WebHome]
323 {{/code}}
324
325 === Troubleshooting ===
326
327 ==== NoClassDefFoundError: com/sun/tools/attach/AttachNotSupportedException ====
328
329 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.
330
331 {{code language="none"}}
332 Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/AttachNotSupportedException
333 at java.lang.Class.getDeclaredMethods0(Native Method)
334 at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
335 at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
336 at java.lang.Class.getMethod0(Class.java:3018)
337 at java.lang.Class.getMethod(Class.java:1784)
338 at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
339 at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
340 Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.AttachNotSupportedException
341 at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
342 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
343 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
344 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
345 ... 7 more
346 {{/code}}
347
348 In this case, edit ##bminstall.sh## and add the following line below the ##OS=`uname`## line in order to override it:
349
350 {{code language="none"}}
351 OS=whatever
352 {{/code}}
353
354 = Debugging JavaScript =
355
356 The 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.
357
358 {{version since='12.7-rc-1'}}
359 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).
360
361 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:
362
363 * debugging step by step may behave strangely when there's no 1 to 1 mapping between the minified code and its source
364 * 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.
365 {{/version}}
366
367 {{version since='7.1'}}
368 You can also set ##debug.minify## to ##false## in ##xwiki.properties## file instead of passing ##?minify=false## in the URL.
369 {{/version}}
370
371 {{version before='12.7-rc-1'}}
372 You had to do the following:
373 * Build XWiki with the ##debug## profile (##mvn clean install -Pdebug##)
374 * Once you're on a page make sure to pass ##?minify=false## in the URL. If you don't do this then the aggregated JS files are going to be referenced and since they don't exist, the JS code will break.
375 {{/version}}
376
377 = Debugging the WYSIWYG editor =
378
379 Example to debug JS code in ##macroSelector.js##:
380
381 * 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):(((
382 {{code language="xml"}}
383 ...
384 <plugin>
385 <groupId>org.codehaus.mojo</groupId>
386 <artifactId>exec-maven-plugin</artifactId>
387 <executions>
388 <!-- Build the CKEditor. -->
389 <execution>
390 <id>build-ckeditor</id>
391 <!-- Use a phase after compile because the Clirr Maven Plugin executes all the phases up to compile twice. -->
392 <phase>process-classes</phase>
393 <goals>
394 <goal>exec</goal>
395 </goals>
396 <configuration>
397 <executable>${ckeditor.builder.path}/build.sh</executable>
398 <arguments>
399 <!-- Exclude from release all plugins/skins that are not specified in build-config. -->
400 <argument>--skip-omitted-in-build-config</argument>
401 <argument>-s --leave-js-unminified</argument>
402 </arguments>
403 </configuration>
404 </execution>
405 </executions>
406 </plugin>
407 ...
408 {{/code}}
409 )))
410 * Rebuild the ##application-ckeditor-webjar## module (you'll also need to rebuild##application-ckeditor-plugins## if you made changes to it).
411 * Copy the generated ##application-ckeditor-webjar-<version>.jar## to an XWiki instance, in ##WEB-INF/lib## and restart XWiki
412 ** You could also remove the CKEditor extensions in the permanent's directory (in ##extensions/repository##) to be safe.
413 * 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##.
414 * Use your browser's dev tools to put breakpoints in the JS.
415
416 = Analyze Out Of Memory issues =
417
418 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.
419
420 Since 7.2RC1 the option is enabled by default in Jetty based distributions and the memory dump will ends up in ##data/## folder.

Get Connected