Skip to content

Instantly share code, notes, and snippets.

@Makistos
Makistos / flatten.py
Created June 27, 2016 12:43
Flattening a list in Python. #python
def flatten(coll):
for i in coll:
if isinstance(i, (list, tuple)):
for j in flatten(i):
yield j
else:
yield i
@Makistos
Makistos / median.py
Created June 16, 2016 06:42
Calculating the median from a list in Python. #python
def median(l):
sortedl = sorted(l)
mid = (len(sortedl) -1) // 2
if (len(sortedl)) % 2:
return (sortedl[mid] + sortedl[mid+1]) / 2.0
else:
return sortedl[mid]
@Makistos
Makistos / stacktrace.java
Last active March 22, 2017 08:00
Printing stack trace in Android. #java
// Get stacks for every thread
import java.util.*;
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for (Thread key : map.keySet()) {
for (StackTraceElement ste : map.get(key)) {
Log.e(TAG, "TRACE: (" + key.toString() + ":" + key.getId() + ") " + ste);
}
}
@Makistos
Makistos / debug.md
Last active June 8, 2016 08:11
Linux debugging tips.

kernel_variant.xml:

  • CONFIG_DYNAMIC_DEBUG=y
  • CONFIG_PRINTK_TIME=y
  • CONFIG_LOG_BUF_SHIFT=18
  • CONFIG_KALLSYMS=y
@Makistos
Makistos / dtparser.py
Created April 28, 2016 07:49
Print Linux Device Tree and optionally search for strings within that tree. Requires the top level dts file as input. #linux #python
#!/usr/bin/python
from __future__ import print_function
import os, sys, getopt
searchstring = ""
def usage():
print (os.path.basename(sys.argv[0]) + ' [-s search_string] -i <inputfile>')
@Makistos
Makistos / dex2oatsum.
Last active June 8, 2016 08:12
Calculating how long dex2oat takes time on Android boot. Log is created with adb logcat -v threadtime > logcat.log. #bash #awk
grep "dex2oat took" logcat.log | sed 's/\ms//g' | column -t -s ' ' | gawk '{ sum += $10 } END { print sum}'
@Makistos
Makistos / Doxyfile
Created March 9, 2016 10:18
General Doxyfile for studying C code (inlined source, statics included, etc). Requires dot and mscgen. #doxygen #c
# Doxyfile 1.8.6
# This file describes the settings to be used by the documentation system
# doxygen (www.doxygen.org) for a project.
#
# All text after a double hash (##) is considered a comment and is placed in
# front of the TAG it is preceding.
#
# All text after a single hash (#) is considered a comment and will be ignored.
# The format is:
@Makistos
Makistos / thread_trace.c
Last active April 6, 2016 05:36
These definitions can be used to trace threaded applications. Add ENTER; at the beginning and END; at the end of any functions you wish to trace. You will get prints when the function is entered and exited along with a microsecond timestamp and thread id. Note that printf() isn't probably the best function to use, for instance in Android I used …
#include <time.h>
unsigned long us_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}
#define ENTER printf("%ld enter %s (%d)\n", us_time(), __FUNCTION__, gettid())
#define EXIT printf("%ld exit %s (%d)\n", us_time(), __FUNCTION__, gettid())
@Makistos
Makistos / git-repo-statuses.sh
Last active December 18, 2022 07:01
Get a nice list of git repository statuses using the repo tool. #repo #git #git-status
# Get a list of all the repos
repo forall -c 'echo $REPO_PROJECT; git status -s'
# Just list the changed repos
repo forall -c 'if [ "$(git status --porcelain)" != "" ]; then echo $REPO_PROJECT; fi'
# Print the contents, also only include projects from given list
repo forall $(cat projects) -c 'if [ "$(git status --porcelain)" != "" ]; then echo $REPO_PROJECT; git status -s; fi'
# Check both branch and (un)staged commits
@Makistos
Makistos / rm-kernel.sh
Created February 11, 2016 13:34
Remove oldest Linux kernel and associated packages if there are more than two available. #bash #linux #bash-select #kernel
#!/bin/bash
KERNELS=`ls /boot | grep vmlinuz | cut -d'-' -f2,3`
echo Available kernels: $KERNELS
KERNCOUNT=`echo $KERNELS | wc -w`
if [ $KERNCOUNT -gt 2 ];
then
OLDEST=`echo "${KERNELS%% *}" | head -1`
PACKAGES=`dpkg -l | grep ^ii |grep $OLDEST | awk -F' ' '{ print $2 }'`
echo "Oldest package (to be removed): $OLDEST"