Skip to content

Instantly share code, notes, and snippets.

@javiermon
javiermon / timedsocket.c
Last active December 21, 2015 19:59
timing of socket sys calls
/*
$ LANG=C gcc -g -Wall -Werror socket.c -o socket
$ ./socket 50.116.14.9 0
START
2013-08-27 21:04:47.762757
socket()
2013-08-27 21:04:47.762859
Error : Connect Failed Connection refused
2013-08-27 21:04:47.956695
@javiermon
javiermon / detect_endianness.c
Last active December 26, 2015 21:49
Detect endianness at runtime
/*
$ printf '\1' | od -dAn
Little Endian: 1
Big Endian: 256
*/
#include <inttypes.h>
int main(int argc, char ** argv){
volatile uint32_t i=0x01234567;
#include <stdio.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/connector.h>
#include <linux/cn_proc.h>
#include <stdlib.h>
#include <errno.h>
struct __attribute__ ((aligned(NLMSG_ALIGNTO))) nlcn_msg {
struct nlmsghdr nl_hdr;
@javiermon
javiermon / hexdump.c
Last active May 31, 2022 23:01
hexdump.c
#include <stdio.h>
void hexDump(char *desc, void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = addr;
// Output description if given.
if (desc != NULL)

High-level

I want to write about why GDB is a great tool for learning C. At least part of the difficulty in learning C is that the language isn’t as interactive as using Python or Ruby.

TL;DR You can kinda use gdb as a repl for c

What’s the smallest possible program we could debug to learn about pointers?

@javiermon
javiermon / kernel_page_size.txt
Created January 22, 2014 08:08
Get the kernel page size
One approximate method is to read /proc/meminfo and check Mapped size (on mine its 52544 kB as of now)
and then check nr_mapped in /proc/vmstat (on mine its 131136 as of now). Finally PAGE_SIZE = Mapped/nr_mapped.
Sometimes this gives you an accurate value (as in the current example I've quoted)
and sometimes its approximate but very close.
@javiermon
javiermon / toolchain.txt
Last active January 4, 2016 22:09
embedded toolchain
- Download the toochain from http://buildroot.uclibc.org/downloads/
Once downloaded & unpacked:
make menuconfig
Then configure the toolchain, check kernel, uclibc, etc versions:
- kernel version (uname -a)
linux 2.6 (manually specified?)
linux version: 2.6.22
- Kernel & arch:
@javiermon
javiermon / mac2bin.py
Last active August 29, 2015 14:01
mac2bin.py
#!/usr/bin/python
# netaddr does this and more:
# >>> import netaddr
# >>> netaddr.EUI('d4:be:d9:23:d5:bf').words
import sys
if len(sys.argv) != 2:
print "usage %s mac-addr" % sys.argv[0]
sys.exit(-1)
#!/usr/bin/env python
"""Simple HTTP Server With Upload.
This module builds on BaseHTTPServer by implementing the standard GET
and HEAD requests in a fairly straightforward manner.
"""
@javiermon
javiermon / inotify_directory.c
Last active June 7, 2017 06:00
inotify_directory.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>
#include <signal.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )