Skip to content

Instantly share code, notes, and snippets.

@petewarden
petewarden / arch_of.py
Created July 6, 2024 20:33
Python script to determine the architecture type of a DLL on Windows
#!/usr/bin/env python
import os
import struct
import sys
# adapted from: http://stackoverflow.com/a/495305/1338797 and
# https://github.com/tgandor/meats/blob/master/missing/arch_of.py
def arch_of(dll_file):
@petewarden
petewarden / restart_vscode_on_myth.sh
Created February 29, 2024 19:18
Restarting VSCode Server on Stanford Myth Machines
# I use VS Code for Pintos development on Stanford's myth servers, but sometimes it fails to connect
# and can be hard to reset. Normally I'd delete `~/.vscode-server` on the remote machine, but the
# networked file system used at Stanford often seems to leave files open semi-permanently even after
# an SSH session has completed, so if I run `rm -rf ~/.vscode-server` I see errors like:
$ rm -rf .vscode-server
rm: cannot remove '.vscode-server/cli/servers/Stable-903b1e9d8990623e3d7da1df3d33db3e42d80eda/server/bin': Directory not empty
rm: cannot remove '.vscode-server/extensions/ms-vscode.cpptools-1.18.5-linux-x64.-5974b6c6.vsctmp/bin/.__afsC4E7': Device or resource busy
# To solve this, I've taken to using the `lsof` command to figure out which processes have those

GDB Tips and Tricks for Debugging Pintos

As I get more familiar with the Pintos environment, I want to save notes on techniques I've found improved my quality of life when debugging through the assignments. Please don't add any comments that discuss project solutions, but feel free to contribute your own favorite ways of improving the debugging process, since I'm hoping we can create a trail of breadcrumbs for future students.

Save GDB Command History Between Sessions

One of the first things I like to do is ensure I can use the up arrow to access commands from a previous gdb session, since debugging tends to involve a lot of repetition!

@petewarden
petewarden / trace.h
Created February 15, 2022 19:12
Header file for simple variable tracing macros
#ifndef INCLUDE_TRACE_H
#define INCLUDE_TRACE_H
#include <stdio.h>
#include <stdint.h>
#define TRACE_STR(variable) do { fprintf(stderr, __FILE__":%d "#variable"=%s\n", __LINE__, variable); } while (0)
#define TRACE_INT(variable) do { fprintf(stderr, __FILE__":%d "#variable"=%d\n", __LINE__, variable); } while (0)
#define TRACE_PTR(variable) do { fprintf(stderr, __FILE__":%d "#variable"=0x%016lx\n", __LINE__, (uint64_t)(variable)); } while (0)
#define TRACE_SIZ(variable) do { fprintf(stderr, __FILE__":%d "#variable"=%zu\n", __LINE__, variable); } while (0)
@petewarden
petewarden / bashrc.sh
Created January 31, 2022 23:41
Bash history settings to tweak for infinite history, instant appending of commands, and shared history across multiple terminals.
# By Pete Warden, @petewarden
# To use this, open ~/.bashrc in your editor of choice,
# and place these settings at the end.
# Based on https://unix.stackexchange.com/a/48113 and
# https://blog.sanctum.geek.nz/better-bash-history/
# Ignore both duplicate commands, and those that start with a space.
HISTCONTROL=ignoreboth
# Append to the history file, don't overwrite it.
# ==============================================================================
"""LSTM quantization with python."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pathlib
from absl import app
@petewarden
petewarden / convert_cc_to_tflite.py
Created February 27, 2020 23:55
Example of converting a .cc TensorFlow Lite C data array file back into a binary flatbuffer on disk
import re
output_data = bytearray()
with open('tensorflow/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc', 'r') as file:
for line in file:
values_match = re.match(r"\W*(0x[0-9a-fA-F,x ]+).*", line)
if values_match:
list_text = values_match.group(1)
values_text = filter(None, list_text.split(","))
values = [int(x, base=16) for x in values_text]
output_data.extend(values)
@petewarden
petewarden / magic_wand_gesture.ino
Created February 17, 2020 22:07
TensorFlow Lite Micro sketch for Arduino that captures gesture data for training magic wand gestures
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@petewarden
petewarden / strip_names.py
Created January 29, 2020 20:35
Example of using the new Flatbuffers Python API to modify a TensorFlow Lite file
def StripTfliteFile(tflite_input, tflite_output):
"""Given 'tflite_input`, write stripped version, 'tflite_output'."""
if not os.path.exists(tflite_input):
raise RuntimeError("Invalid filename %r" % tflite_input)
with open(tflite_input, "rb") as file_handle:
file_data = bytearray(file_handle.read())
model_obj = schema_fb.Model.GetRootAsModel(file_data, 0)
model = schema_fb.ModelT.InitFromObj(model_obj)
model.description = ""
for subgraph in model.subgraphs:
# To convert from .ogg files to .wavs:
# find . -iname "*.ogg" -print0 | xargs -0 basename -s .ogg | xargs -I {} ffmpeg -i {}.ogg -ar 16000 ../converted_early_wavs/{}.wav
# Then run the extract_loudest xcode project to get one-second clips.
import glob
import os
import re
import shutil
data_index = {}