Sockets… why they are not included in the C++ standard? We had a lot of trouble wrapping BSD sockets C API into our C++ library. Recently, analyzing our application with valgrind I discovered memory leaks in one of the socket wrappers.
The raw pointer to C struct addrinfo was wrapped into unique pointer:
std::unique_ptr<struct addrinfo> addresssInfo_;
The memory was allocated as
int result = getaddrinfo(host, post, &hints, &aInfo);
addressInfo_ = std::unique_ptr<struct addrinfo>(aInfo);
In our case, the IpSocket destructor will try to free memory by invoking the default deleter (delete operator) over object stored in addressInfo_. That is the problem, the getaddrinfo() allocates the memory using C function malloc and hence we need to invoke free() function to release this memory.
The manpage for getaddrinfo() says that the freeaddrinfo() function should be used to free memory. The quick look into freeaddrinfo() source confirms that the function does what we expected
void freeaddrinfo (struct addrinfo *ai)
struct addrinfo *p;
while (ai != NULL)
p = ai;
ai = ai->ai_next;
In order to force our unique_ptr use freeaddrinfo we need to define custom deleter and put it as a second template argument.
I’ve been a long time an AVR user, but the technology is going ahead and it is time to use something more modern. Recently I started to look closer on the ARM microcontrollers and finally, I’ve selected STM32 Cortex-M3 as my base unit. For me, the perfect development environment would be QtCreator + GNU toolchain (since I use this environment on my work and I do not like Java-based IDEs). This post will be devoted to the hardware enabling on-chip debugging the STM32 applications using gdb.
In order to be able to use gdb, one need some adapter providing the interface between the debugger and ARM chip. I have considered several popular alternatives ST-Link from ST-Microelectronics, J-Link from Segger and Black Magic Probe from Black Sphere Technologies.
J-Link is a cool tool but it is really expensive. ST-Link is a nice and cheap tool and is supported by BareMetal plugin in QtCreator. In fact, gdb can’t work directly with ST-Link and you will need intermediate software like openocd to communicate with this dongle.
Black Magic Probe (BMP) implements gdb protocol natively and you do not need any additional software. BMP supports JTAG as well as SWD protocols, it has bootloader for updating firmware over USB. This dongle is also quite expensive for the hobbyist. Hopefully, the developer provides the schematics and firmware under the GNU Public License 3, and hence it is possible to create your own BMP use it for developing the commercial products and even sell the BMP clones. I’ve decided to build my own BMP with some minor modifications. First of all, I’ve selected the original version since it contains full-sized JTAG header. I’ve made the following modifications with respect to the original schematics: the mini-USB was replaced by USB-B one, the SWD header was added to simplify the flashing BMP chip.
Now it is time to build and flash the firmware:
git clone https://github.com/blacksphere/blackmagic.git cd blakmagic git submodule init git submodule update make -j4 PROBE_HOST=native
This will create a bootloader file and BMP firmware file. In order to make debugging dongle one need to have debugging dongle 🙂 Connect your favorite programmer to the SWD head on the BMP and flash it. Hier the ST-Link is used:
The second command flashes the bootloader and the third one downloads the BMP firmware. Now it is time to connect BMP to your PC via USB an check if it is recognized. Use lsub command to see USB devices attached. If everything is fine you will see the following line:
ID 1d50:6018 OpenMoko, Inc.
then the device is recognized. To test gdb protocol just start gdb and issue the following commands:
(gdb) target extended-remote /dev/ttyACM0 (gdb) monitor version
Black Magic Probe (Firmware v1.6.1-269-g7cb1858) (Hardware Version 0) Copyright (C) 2015 Black Sphere Technologies Ltd.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
In the next article, I will show how to configure BMP with QtCreator.