Development/Navigating the LibreOffice codebase
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
LibreOffice's large codebase can feel overwhelming to new contributors. This article provides ways to speed up getting from an issue to a solution.
Read the code
Many of the directories have readme files, so remember to check for them when studying modules or test suites. If you have ideas for more and expanded readmes or code comments, feel free to submit patches.
Terminal utilities
You will get a lot of mileage out of git grep, git log, git diff and git blame. Do try out ways of enhancing their output. Studying the man pages for the commands from time to time is a good idea.
For git grep, keep in mind at least -n (--line-number), -i (--ignore-case), -p (--show-function) and -W (--function-context) options as well as -E (--extended-regexp).
For git log, the --follow option helps with renamed files and -S"some string" shows you when a string was added. The option --all combined with --grep="some string" is useful for finding clues within commit messages.
Try out Blame Browse from Neil Roberts for conveniently studying git blame results.
CGrep makes it possible to limit your searches to a specific context like code, comments or string literals.
Find what changed something
A change in behaviour or appearance can be traced back to a commit using git bisect. To make this easier, we provide a method called binary bisecting. These investigations are commonly performed by contributors without programming experience.
A straightforward way to find code is when you can begin with a string seen in the UI. You need to take into account the mnemonic/accelerator characters, which are indicated as underlined when you press the Alt key. These may or may not be automatically generated. If you see a label "Width:" in a Writer dialog with the "i" character underlined, you can use a search like so to make sure any mnemonics are included: git grep -E 'W.?idth:' sw*.ui
A possible way to get from a dialog to its .ui file is to click the Help button in the dialog. As an example, when doing this in an Area tab or the Area dialog, in the address bar of the browser you will see a parameter like this in the URL: HID=cui/ui/areatabpage/box1. This means the source code file is cui/uiconfig/ui/areatabpage.ui. Grep with just the file name to find the associated C++ file.
OpenGrok can be used for matching .ui files with .cxx files. Take a piece of text from a dialog (anything that is long enough, like "Width and spacing") and put it into "Full Search" including the quotes (so that it does not find thousands of results). In this example, you get columnpage.ui. If you search for "columnpage.ui" you will get to sw/source/ui/frmdlg/column.cxx where the dialog is anchored.
When you use the environment variable HELP_DEBUG with value 1, then the tooltip of a toolbar or menu item has an additional dashed line with the .uno command below it. For that, "Extended tips" must be enabled via ▸ ▸ ▸ . You will also see UI element paths when hovering in dialogs or the Sidebar.
Searching for the .uno command gives you an .sdi file. It gives you the SID_FOO string associated to the command. Searching for this SID_FOO command gives you .cxx files that use it, mostly in a switch statement. For example, git grep RemoveHyperlink *.sdi gives a hit in svx/sdi/svx.sdi
When selecting commands in the Available Commands listbox in ▸ , you will see the .uno command for each one in the Description field.
For a list of all available commands, see Development/DispatchCommands.
Debugging
See the main article on debugging.
When running LibreOffice with the GDB debugger, you can say info types ^TypeName$ to get the file name and line number for the definition of a type.
Sometimes it might be handy to print out values in the console. The SAL_DEBUG macro is for temporary debug statements. For example:
SomeClass pSomeClass = this->getSomeClass();
SAL_DEBUG(pSomeClass.Name << ":" << pSomeClass.Value);
SAL_DEBUG() statements need to be removed before submitting code to Gerrit. Learn more at sal/log.hxx.
OpenGrok
TDF hosts an instance of the OpenGrok code search and cross reference engine. In the "Project(s)" selector, core means LibreOffice's source code.
Integrated development environment
See the main article on IDEs. Some IDEs are able to display declarations and definitions from anywhere in the codebase.
Helpful VS Code extensions include C/C++ Documentation Viewer.
Dumping XML models
You can launch a debug build with the environment variables SW_DEBUG=1 and SD_DEBUG=1 to enable keybindings for dumping XML model files into the running directory. F12 in Writer creates a layout.xml for the layout model and ⇧ Shift + F12 a nodes.xml for the document model. F12 in Impress and Draw creates a model.xml for the document model. See sw/qa/extras/README for discussion of the Writer model dumps in the context of writing tests.
Development Tools
In Writer, Calc, Impress and Draw you can access Tools - Development Tools to inspect objects and to explore the document structure. Read more in the help page for the feature. If you would like to improve the feature, check out the open requests and issues for it.
Studying XML-based documents
In the case of ODF documents, you can save into a flat format that allows you to examine the entire document as a single file. Otherwise you may unzip the ODF or OOXML document and look inside the separate .xml files. A document saved with pretty printing is the easiest to study. Go into ▸ ▸ ▸ and click . There you should search for "prettyprinting" and double-click the PrettyPrinting option to change the value from false to true.
You can search through the unzipped .xml files in the terminal with a command like:
find . -name '*.xml' -exec grep -i --color=always "some string" {} +
Flat ODF files can easily be diffed with any tool. Special tools are covered in Analyzing Documents while the article ODF Markup is useful as an overview.
Detective work with profilers
Especially if you have no idea what to search for, recording and examining a performance profile can be a nice trick. For this purpose, using valgrind's callgrind is probably better than perf as the output is more detailed. KCachegrind can be used to study the results and search within them. With this method it's best to use a build with --enable-symbols instead of debug options. Likely you will want to launch without instrumentation and then only record the action that you are interested in.
SAL_LOG output
The main debugging article explains macros controlling debug code. When you are dealing with a code mystery, you might benefit from activating all logging while you do some action in the software. You can do this by launching a debug build like so, directing the output to a file:
SAL_LOG=1 instdir/program/soffice 2>&1 | tee sal_log.log
Example investigations
Origins of CSS properties in HTML files saved from Writer
Let's say you are interested in why LibreOffice adds a CSS style block with margin-bottom: 0.25cm; for p elements when saving a Writer document as HTML. Here the sw/README.md file is helpful and points to the general area of sw/source/filter/html. Still, there are many files in that directory and what if the module did not have such a rich readme? Callgrind to the rescue:
- Launch your symbols build with:
VALGRIND="callgrind --instr-atstart=no" ./instdir/program/soffice - ▸ , pick "HTML Document (Writer) (.html)", click Save, but hold off on confirming the non-ODF format
- In another terminal, give the command
callgrind_control -i on - Now confirm the save
- Wait until the .html file is saved and in the other terminal, give the command
callgrind_control -d - Stop the tracing and kill LibreOffice with
callgrind_control -k
Launch KCachegrind and open the biggest callgrind.out file residing in the directory where you ran LibreOffice. Now you can search the trace with keywords like "html" and "css". SwHTMLWriter::OutCSS1_Property seems very interesting.
Assuming you are on Linux, you can launch your debug build of LibreOffice and attach GDB to it with gdb --pid=$(pidof soffice.bin). Before you do, make it so that the string comparison convenience function from the GDB article gets loaded by GDB. You can then set a breakpoint within the SwHTMLWriter::OutCSS1_Property function by saying b sw/source/filter/html/css1atr.cxx:365 if $cppstrcmp(pProp, "margin-bottom"), pointing to the number of the line that has sOut.append(pProp + OString::Concat(": "));.
Now say continue or just c and save a blank Writer document as HTML. GDB will report that a breakpoint was hit and stop the execution of the program. You can find what called the function by saying backtrace or bt.
Looking at the topmost lines in the backtrace, you notice that 0.25cm is originating from a value of 140. This value is passed by the OutCSS1_SvxULSpace function.
The frames in the backtrace are numbered and you can switch to them by pointing to the number with frame n or f n. You may use the print or p command to print out interesting things in the frame context.
The trace points to a location in the SwHTMLWriter::OutStyleSheet function where a comment says "The text body style has to be exported always" and the paragraph style with the ID COLL_TEXT is handled. So now the mystery is revealed: the value is taken from the paragraph style that is displayed as Body Text in the LibreOffice UI.
In an IDE you might jump to the definition of GetLower and find it in include/editeng/ulspitem.hxx. If you set a breakpoint to the SvxULSpaceItem::SetLower function there with b include/editeng/ulspitem.hxx:88 if nL == 140 and open a Writer document where a paragraph has the Body Text style, you will see in a backtrace that the value 140 was converted from 247. The conversion is done in editeng/source/items/frmitems.cxx where the call is
SetLower(bConvert ? o3tl::toTwips(nVal, o3tl::Length::mm100) : nVal);
A further mystery is solved when you study the Wikipedia article for Twip. 140 times 17.64 μm equals 2469.6, which matches the 1/100th mm rounded value of 247.
If you dump nodes.xml in a blank Writer document and search for 140, you will see this entry:
<SwTextFormatColl next="Body Text">
<SwFormat ptr="0x55a8d29afc70" symbol="25SwConditionTextFormatColl" name="Body Text" derived-from="Default Paragraph Style">
<SfxItemSet>
<SfxPoolItem ptr="94183576239920" whichId="68" typeName="18SvxLineSpacingItem" presentation="1.15 Lines"/>
<SvxULSpaceItem whichId="104" nUpper="0" nLower="140" bContext="false" nPropUpper="100" nPropLower="100"/>
</SfxItemSet>
</SwFormat>
</SwTextFormatColl>
On the other hand, if you search for 247 in the styles.xml of a saved Writer document, you will see this:
<style:style style:name="Text_20_body" style:display-name="Text body" style:family="paragraph" style:parent-style-name="Standard" style:class="text">
<style:paragraph-properties fo:margin-top="0cm" fo:margin-bottom="0.247cm" style:contextual-spacing="false" fo:line-height="115%"/>