Skip to content

Instantly share code, notes, and snippets.

View hbobenicio's full-sized avatar

Hugo Benício hbobenicio

  • Brazil
View GitHub Profile
@hbobenicio
hbobenicio / main.c
Created July 17, 2025 19:34
Simple stacktrace printing in C using libbacktrace
/**
* A simple demonstration of libbacktrace to print a stacktrace.
*
* bear -- gcc -O0 -g -o main main.c -lbacktrace && ./main
* bear -- clang -O0 -g -isystem /usr/lib/gcc/x86_64-linux-gnu/12/include/ -o main main.c -lbacktrace && ./main
*/
#include <stdio.h>
#include <backtrace.h>
@hbobenicio
hbobenicio / gist:f2333fa6c2cc559101396cea3f5156e0
Created April 15, 2025 14:09
Java Properties for Remote Profiling (VisualVM)
-Dcom.sun.management.jmxremote.host=0.0.0.0
-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=0.0.0.0
-Djava.security.policy=jstatd.all.policy
-Dcom.sun.management.jmxremote.rmi.port=1234
@hbobenicio
hbobenicio / defer.cpp
Created January 20, 2025 02:27
Simplest Defer in C++
/**
* clang++ -std=c++2c -Wall -Wextra -Wpedantic -g -o main main.cpp && ./main
*/
#include <iostream>
template<class F>
class Defer {
private:
F func;
public:
@hbobenicio
hbobenicio / fork-and-pipe.c
Created November 25, 2024 01:12
Demonstrates how to create a subprocess with Fork and Pipe
/**
* A simple example um creating a subprocess to execute some program and capture its stdout and stderr using pipes.
*
* ## Compilation example
*
* ```
* clang -Wall -Wextra -Wpedantic -std=c23 -g -O0 -fsanitize=address,undefined -fno-omit-frame-pointer -fuse-ld=lld -o fork-test main.c
* ```
*
* ## References
@hbobenicio
hbobenicio / file-copy.c
Created November 20, 2024 13:33
File Copy in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
enum result { RESULT_OK, RESULT_ERR };
@hbobenicio
hbobenicio / main.c
Created June 23, 2023 22:09
libcurl simple example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <curl/curl.h>
struct buffer {
unsigned char* data;
size_t size;
@hbobenicio
hbobenicio / main.c
Last active June 21, 2023 20:51
Read/Map file to memory at once with mmap
/**
* This program demonstrates how to read/map a file into memory using mmap.
*
* In order to get the file size we also avoid the "problematic" fseek + SEEK_END approach.
* Instead, we use open/fstat for it (although fopen/fileno/fstat would also work just fine).
*/
// libc
#include <stdio.h>
#include <stdlib.h>
@hbobenicio
hbobenicio / main.cpp
Created September 5, 2022 20:18
Print elf 64 header info
#include <bits/stdc++.h>
#include <elf.h>
using std::cout;
using std::cerr;
using std::string_view;
using std::array;
namespace elf::header
{
@hbobenicio
hbobenicio / producer.c
Created June 17, 2022 17:25
librdkafka example: Kafka Producer in C
#include <stdio.h>
#include <librdkafka/rdkafka.h>
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
#define BOOTSTRAP_SERVERS_KEY "bootstrap.servers"
#define BOOTSTRAP_SERVER_VALUE "kafka:9092"
#define PRODUCER_TOPIC "librdkafka-examples-topic"
@hbobenicio
hbobenicio / ldap-client.cpp
Created June 17, 2022 16:52
LDAP C++ client example
/**
* https://www.forumsys.com/2014/02/22/online-ldap-test-server/
*
* clang++ -Wall -Wextra -Wpedantic -std=c++17 -g -O0 -ggdb -fsanitize=address,undefined -fno-omit-frame-pointer -o /tmp/ldap-client ldap-client.cpp -lldap
*/
#include <iostream>
#include <cassert>
#include <cstring>
#include <cerrno>
#include <sstream>