Valgrind Debugging common usage

Category: linux, year: 2020

Valgrind is a tool on Linux (it might work on some other UNIX platforms as well?) that allows validating and debugging memory accesses of applications (in addition to other things like profiling cache accesses and validating some thread locking correctness), which can be very useful for debugging issues in applications written in non-‘safe’ programming languages like C and C++. It can help identify issues like use-after-free, buffer overruns and other similar issues which conventional debuggers (gdb, lldb) might not always catch until is too late (i.e. you just see the segfault, not the original root cause event).

It can also track down memory leaks as well - although other more specialised applications like heaptrack and bytehound can in some cases be better tools to use for just profiling memory usage (as opposed to memory correctness as well), as these other applications just concentrate on that and are in some cases much faster when debugging your application compared to fully running through valgrind. However in some cases I have found both heaptrack and bytehound have produced non-existant (effectively not possible) stacktraces for memory allocations, whereas valgrind doesn’t seem to have this issue.

Additional args:

--track-origins=yes will track the origin of all memory allocations which can be helpful to work out what memory was causing any particular issue.

--leak-check=full will perform full memory leak checking.

--max-threads=2000 or similar is needed if the application being profiled/debugged uses more than the default of 500 threads valgrind can cope with.

--log-file=/tmp/valgrind_run1.txt will tell valgrind to write the output (which can be incredibly verbose) to a file instead of stdout which is often a good idea.



Format a number with thousand separator characters in C++

Category: cpp, year: 2019

The following code function snippet is an example of how format a number (already in std::string form) with thousand separator characters in C++:

Note: it doesn’t currently support numbers with decimal places, only integer-formatted numbers.

std::string formatNumberThousandsSeparator(const std::string& value)
{
	std::string final;
	int i = value.size() - 1;
	unsigned int count = 0;
	for (; i >= 0; i--)
	{
		final += value[i];

		if (count++ == 2 && i != 0)
		{
			final += ",";
			count = 0;
		}
	}

	std::reverse(final.begin(), final.end());

	return final;
}


Stripping whitespace characters from an std::string in C++

Category: cpp, year: 2018

C++ unfortunately does not have very many built-in convenient string-handling methods, although there are libraries like boost and pystring that can be used to obtain functionality that is found in other more modern programming languages.

However, sometimes using external dependencies is not possible or wanted for a variety of reasons.

The following code function snippet is an example of how to strip whitespace (space) characters from the start and end of an std::string in C++, in-place modifying the std::string passed in:

void stripWhitespace(std::string& str)
{
	if (str.empty())
		return;

	// use space and tab chars for whitespace...
	static const char* kWhitespaceChars = " \t";

	size_t firstNonWhitespacePos = str.find_first_not_of(kWhitespaceChars, 0);
	if (firstNonWhitespacePos == std::string::npos)
	{
		// we didn't find a non-whitespace char, so set string to empty
		str = "";
		return;
	}

	size_t lastNonWhitespacePos = str.find_last_not_of(kWhitespaceChars);
	if (lastNonWhitespacePos == std::string::npos ||
		lastNonWhitespacePos < firstNonWhitespacePos)
	{
		// this shouldn't really be possible, but something's gone wrong...
		return;
	}

	// extract the string from the non-whitespace start/end chars
	str = str.substr(firstNonWhitespacePos, lastNonWhitespacePos - firstNonWhitespacePos + 1);
}


Splitting an std::string in C++

Category: cpp, year: 2018

C++ unfortunately does not have very many built-in convenient string-handling methods, although there are libraries like boost and pystring that can be used to obtain functionality that is found in other more modern programming languages.

However, sometimes using external dependencies is not possible or wanted for a variety of reasons.

The following code function snippet is an example of how to split an std::string into an std::vector of strings based on a separator string in C++:

void splitString(const std::string& str, std::vector<std::string>& stringTokens, const std::string& sep)
{
	size_t lastPos = str.find_first_not_of(sep, 0);
	size_t pos = str.find_first_of(sep, lastPos);

	while (lastPos != std::string::npos || pos != std::string::npos)
	{
		stringTokens.push_back(str.substr(lastPos, pos - lastPos));
		lastPos = str.find_first_not_of(sep, pos);
		pos = str.find_first_of(sep, lastPos);
	}
}


Enable ptrace tracing

Category: linux, year: 2018

With most Linux distro default settings, it’s normally not possible for applications like perf, vtune or gdb to attach to already-running processes and control/examine them, so to enable that (for the current session only), the following can be run in the terminal:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope



Archive
Full Index

linux (4)
cpp (3)
rust (2)
macos (1)
matplotlib (1)
unix (1)


Tags List