<commit_before>/*
 * Copyright 2014 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <cstdio>
#include <cassert>
#include <cstring>
#include <cstdlib>

#include <dlfcn.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>

namespace {

using malloc_t = void* (*) (size_t);
using free_t = void (*) (void*);

malloc_t real_malloc = nullptr;
free_t real_free = nullptr;

void show_backtrace()
{
    const size_t BUFSIZE = 256;
    char name[BUFSIZE];
    unw_cursor_t cursor; unw_context_t uc;
    unw_word_t ip, sp, offp;

    unw_getcontext (&uc);
    unw_init_local (&cursor, &uc);

    while (unw_step(&cursor) > 0)
    {
        name[0] = '\0';
        unw_get_proc_name(&cursor, name, BUFSIZE, &offp);
        unw_get_reg(&cursor, UNW_REG_IP, &ip);
        unw_get_reg(&cursor, UNW_REG_SP, &sp);

        if (name[0] == '\0') {
            strcpy(name, "??");
        }

        printf("%s ip = %lx, sp = %lx\n", name, (long) ip, (long) sp);
        if (!strcmp(name, "main")) {
            break;
        }
    }
}

}

extern "C" {

void* malloc(size_t size)
{
    if (!real_malloc) {
        real_malloc = reinterpret_cast<malloc_t>(dlsym(RTLD_NEXT, "malloc"));
        if (!real_malloc) {
            fprintf(stderr, "could not find original malloc\n");
            exit(1);
        }
    }
    assert(real_malloc);
    void* ret = real_malloc(size);
    show_backtrace();
    return ret;
}

void free(void* ptr)
{
    if (!real_free) {
        real_free = reinterpret_cast<free_t>(dlsym(RTLD_NEXT, "free"));
        if (!real_free) {
            fprintf(stderr, "could not find original free\n");
            exit(1);
        }
    }
    assert(real_free);
    real_free(ptr);
}

}<commit_msg>Print caller of malloc/operator new/operator new[] instead of full backtrace.<commit_after>/*
 * Copyright 2014 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <cstdio>
#include <cassert>
#include <cstring>
#include <cstdlib>

#include <dlfcn.h>

#define UNW_LOCAL_ONLY
#include <libunwind.h>

namespace {

using malloc_t = void* (*) (size_t);
using free_t = void (*) (void*);

malloc_t real_malloc = nullptr;
free_t real_free = nullptr;

void print_caller(size_t size)
{
    unw_context_t uc;
    unw_getcontext (&uc);

    unw_cursor_t cursor;
    unw_init_local (&cursor, &uc);

    // skip malloc
    if (unw_step(&cursor) <= 0) {
        return;
    }

    size_t BUF_SIZE = 256;
    char name[BUF_SIZE];
    unw_word_t offset;
    unw_word_t ip;

    printf("-----\n");
    while (unw_step(&cursor) > 0)
    {
        name[0] = '\0';
        unw_get_proc_name(&cursor, name, BUF_SIZE, &offset);
        if (name[0] != '_' || name[1] != 'Z' ||
            (strcmp(name, "_Znwm") && // operator new
            strcmp(name, "_Znam")))   // operator new[]
        {
            unw_get_reg(&cursor, UNW_REG_IP, &ip);
            printf("%s+0x%lx@0x%lx %ld\n", name, offset, ip, size);
            break;
        }
    }
}

}

extern "C" {

void* malloc(size_t size)
{
    if (!real_malloc) {
        real_malloc = reinterpret_cast<malloc_t>(dlsym(RTLD_NEXT, "malloc"));
        if (!real_malloc) {
            fprintf(stderr, "could not find original malloc\n");
            exit(1);
        }
    }
    assert(real_malloc);
    void* ret = real_malloc(size);
    print_caller(size);
    return ret;
}

void free(void* ptr)
{
    if (!real_free) {
        real_free = reinterpret_cast<free_t>(dlsym(RTLD_NEXT, "free"));
        if (!real_free) {
            fprintf(stderr, "could not find original free\n");
            exit(1);
        }
    }
    assert(real_free);
    real_free(ptr);
}

}
{'fraction_non_alphanumeric': 0.12430278884462151, 'fraction_numerical': 0.010956175298804782, 'mean_word_length': 2.821156773211568, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5747627', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1657, 'n_words': 508}