Emacs: How to Format an XML file

Using emacs in conjunction with libxml2 it is possible to quickly format an xml file into a more readable format. For the purposes of this example we will take the following xml file and reformat it. NB libxml2 must be installed on your machine

<?xml version="1.0"?>
<Tests xmlns="http://www.qedevelopment.co.uk">
  <Test Id="0001" Type="foo">
    <Name>Foo test</Name>
    <Input>1</Input>
    <Output>One</Output>
  </Test>
  <Test Id="0002" Type="garp"><Name>Garp test</Name><Input>abc</Input><Output>def</Output></Test>
</Tests>

Enter the following command into emacs. Where C-x h represents the pressing of the <ctrl> key and the ‘x’ key at the same time followed by the ‘h’ key in order to select all the text in the buffer. M-| represents the pressing of the <esc> key followed by the ‘|’ key (not the simultaneous pressing of the two keys). The RET represents the return key, and don’t forget the ‘-‘ between it and the format.

C-x h C-u M-| xmllint --format - RET

You contents of the buffer should now have been reformatted as show below.

<?xml version="1.0"?>
<Tests xmlns="http://www.qedevelopment.co.uk">
  <Test Id="0001" Type="foo">
    <Name>Foo test</Name>
    <Input>1</Input>
    <Output>One</Output>
  </Test>
  <Test Id="0002" Type="garp">
    <Name>Garp test</Name>
    <Input>abc</Input>
    <Output>def</Output>
  </Test>
</Tests>

How To: Connecting Valgrind to GDB

Debugging memory issues can be made much easier if you use Valgrind with gdb. To do this you need to ensure that the Valgrind gdbserver is enabled. You do this when Valgrind is started with the command below

$ valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 <prog>

where <prog> is the path to the program you want to test e.g. ./test

You then need to open another terminal window and start gdb

$ gdb <prog>

You now need to tell gdb that you want to work with the remote target

(gdb) target remote | vgdb

You can now continue, and wait for gdb to break when Valgrind determines that there is a problem.

(gdb) c