Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
HAVE_LIBUNWIND=1
WITH_ORIGINS_TRACE=1

ifeq ($(HAVE_LIBUNWIND), 1)
optional_libs=libunwind
Expand All @@ -7,19 +8,29 @@ else
optional_libs=
endif

CC ?= cc
FLAGS=`pkg-config --cflags gobject-2.0`
LIBS=`pkg-config --libs gobject-2.0 $(optional_libs)`

OBJS = gobject-list.o

ifeq ($(WITH_ORIGINS_TRACE), 1)
BUILD_OPTIONS+=-DWITH_ORIGINS_TRACE
OBJS += bt-tree.o
endif

all: libgobject-list.so
.PHONY: all clean
clean:
rm -f libgobject-list.so gobject-list.o
rm -f libgobject-list.so $(OBJS)

%.o: %.c
$(CC) -fPIC -rdynamic -g -c -Wall -Wextra ${FLAGS} ${BUILD_OPTIONS} $<

libgobject-list.so: gobject-list.c
libgobject-list.so: $(OBJS)
ifeq ($(HAVE_LIBUNWIND), 1)
@echo "Building with backtrace support (libunwind)"
else
@echo "Building without backtrace support (libunwind disabled)"
endif
$(CC) -fPIC -rdynamic -g -c -Wall ${FLAGS} ${BUILD_OPTIONS} $<
$(CC) -shared -Wl,-soname,$@ -o $@ gobject-list.o -lc -ldl ${LIBS}
$(CC) -shared -Wl,-soname,$@ -o $@ $^ -lc -ldl ${LIBS}
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ GOBJECT_LIST_DISPLAY:
• ‘refs’: Print information about every reference increment and
decrement on objects.
• ‘backtrace’: Include backtraces with every printed message.
• ‘tracerefs’: At exit, for each object still alive, print a call tree.
• ‘all’: All of the above.

GOBJECT_LIST_FILTER:
Expand Down
141 changes: 141 additions & 0 deletions bt-tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* Stores a call trace ("backtrace") for later inspection.
*
* Copyright (C) 2014 Peter Wu <peter@lekensteyn.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

/* TODO
* - faster lookups. The path lookup can surely be optimized (cached?)
* - This code allocates memory which may or may not be a problem depending on
* context.
*/

#include <glib.h>
#include "bt-tree.h"

enum {
COUNT_REF = 0,
COUNT_UNREF,

COUNT_LAST
};

typedef struct BtTrie {
GHashTable *children;
char *label;
unsigned count[COUNT_LAST];
} BtTrie;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BtTree ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A trie is a data structure also known as prefix tree. "BtTrie" = "prefix tree for backtraces storage".


BtTrie *
bt_create (char *label)
{
BtTrie *bt_trie = g_malloc0 (sizeof(BtTrie));
bt_trie->label = label;
bt_trie->children = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify) bt_free);
return bt_trie;
}

void
bt_free (BtTrie *bt_trie)
{
g_free (bt_trie->label);
g_hash_table_unref (bt_trie->children);
g_free (bt_trie);
}

/* returns the child of bt_trie with the item at position i inserted. The memory
* is freed if such a child already exists. */
static inline BtTrie *
find_child (BtTrie *bt_trie, const GPtrArray *items, guint i)
{
BtTrie *child = NULL;
char *label = g_ptr_array_index (items, i);
g_hash_table_lookup_extended (bt_trie->children, label,
NULL, (gpointer *)&child);
if (child == NULL) {
child = bt_create (label);
g_hash_table_insert (bt_trie->children, child->label, child);
} else {
/* unused label */
g_free (label);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're releasing the memory but the label is still in the GPtrArray. Create your GPtrArray with a destroy func and then use g_ptr_array_remove_index() that will cause it to release the memory while removing it.

}
return child;
}

/**
* Inserts a trace described by items into a trie. Memory can be allocated if a
* node is missing.
* @bt_trie: root of the tree.
* @items: the items to insert (in reverse order: the first element is the leaf,
* the last element is the root). Must not be empty. The control of the contents
* is transferred from the caller.
*/
void
bt_insert (BtTrie *bt_trie, const GPtrArray *items, gboolean is_ref)
{
guint i = items->len;
++bt_trie->count[is_ref ? COUNT_REF : COUNT_UNREF]; /* mark root */
while (i-- > 0) {
bt_trie = find_child (bt_trie, items, i);
++bt_trie->count[is_ref ? COUNT_REF : COUNT_UNREF];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use this ternary twice, assign it somewhere and use the variable name.

}
}

static void
_bt_print_tree (gpointer key, gpointer value, gpointer user_data)
{
const char *label = key;
BtTrie *tree = value;
guint indent = GPOINTER_TO_INT (user_data), i;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One declaration per line please.

gint diff = tree->count[COUNT_REF] - tree->count[COUNT_UNREF];
const char
*color_default = "\e[1;34m", /* blue */
*color_unref = "\e[0;31m", /* red */
*color_ref = "\e[0;33m", /* yellow */
*color_diff;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make these #defines.


if (diff == 0) /* not important */
color_default = color_unref = color_ref = color_diff =
"\e[1;30m"; /* gray */
else if (diff < 0) /* more unrefs than refs */
color_diff = "\e[1;31m"; /* red */
else /* diff > 0, more refs than unrefs */
color_diff = "\e[1;33m"; /* yellow */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copies of what's above.


for (i = 0; i < indent; i++)
g_print("| ");
g_print ("%s# %s ", color_default, label); /* name */
g_print ("("
"%s+%u%s" /* refs */
"/"
"%s-%u%s" /* unrefs */
" = %s%d%s", /* diff */
color_ref, tree->count[COUNT_REF], color_default,
color_unref, tree->count[COUNT_UNREF], color_default,
color_diff, diff, color_default);
g_print (")\e[m\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this in a #define also.

g_hash_table_foreach (tree->children, _bt_print_tree,
GINT_TO_POINTER (indent + 1));
}

void
bt_print_tree (BtTrie *root, guint indent)
{
_bt_print_tree (root->label, root, GINT_TO_POINTER (indent));
}
7 changes: 7 additions & 0 deletions bt-tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

typedef struct BtTrie BtTrie;

BtTrie *bt_create (char *label);
void bt_free (BtTrie *bt_trie);
void bt_insert (BtTrie *root, const GPtrArray *items, gboolean is_ref);
void bt_print_tree (BtTrie *root, guint indent);
Loading