GDB Basics: Stack Traces

C beginners are often very lost when their program crashes from a segmentation fault or similar issues. Most of them know about GDB but are pretty intimidated by it.

Here is a super simple example about how to find a stack trace from a program crash.

Of course, this will result in the console simply printing “Segmentation Fault” which doesn’t help very much, especially in more complex programs.

Using GDB, you just have to remember 2 commands to get a stack trace: “run” and “backtrace”.

To compile, use the “-g” flag with gcc to include debugging symbols for gdb:

To run this in gdb, run:

Now all you have to do is type “run” into the gdb console. It will show you the line where it crashed, but to show the stack trace you must run the “backtrace” command:

GDB has a lot of other great features like breakpoints and memory analysis. If you are comfortable with assembly, you can even run commands while your program is running!

How to change login shell without chsh permissions

On the University of Toronto servers and Linux lab computers, the default shell is tcsh, and we don’t have chsh access *shakes fist*.

If you are in a similar circumstance, plop this in ~/.cshrc:

If you just run exec /bin/bash (without checking for the interactive shell), it will break non-interactive shells like scp and the GNOME startup scripts. If you just use bash you can ignore the first conditional. The reason why I have bash as a backup is because the lab computers have zsh installed, but the servers only have bash.

Bai 🙂

P.S. A little known fact about the UofT computing services is that you can SSH into lab computers directly. For instance, connects to “PC #02” in DH2026.

UofT Hacks 2016

So last weekend, I spent 36 hours at UofT for their annual hackathon. If you have not been to a hackathon, I highly recommend it! There are tons of like minded individuals making awesome projects.

Check out the swag I collected:


I even got a new domain name for free: Yes, SquabbitTech is our “company” name :3

The application my team built was a super-fuzzy note searching tool called Fuzzy Wuzzy. The idea is, several years after writing down notes, it is very difficult to remember exactly what you wrote. So we built an application that guesses what you want to search for. We designed three algorithms, but only had time to implement two of them: synonym, misspelled word, and idea group matchers. For instance, searching for “small” will return notes containing the word “tiny”. We didn’t have time to create the idea group algorithm, but we had it planned out. If you search for “car”, everything that is a car will be matched (e.g. “Honda Civic”).

We built the project as an API using Flask – a micro web framework for Python. It is extremely simple in comparison to other frameworks like Symfony. It provides templating, routing, and database engines that are all super easy to get started with.

Dabbling Around with Scala

Scala is a legendary language that allows both OO and functional programming. On top of that, it is compatible with Java and compiles to JVM bytecode. Hence, in theory, it could be used anywhere Java is used *cough cough* Android *cough cough*.

For a while, I have been meaning to try out Scala – and today I bit the bullet.

Closures are awesome. Here is a simple example which multiplies a list by 2 and prints the result:

Of course, this is a very trivial example of how closures can be used, but I find the syntax pretty cool nonetheless 😀

Here are some other features that I really like about Scala:

Just for kicks, I decided to build a simple regex matching program with character matching, “multiple”, and “or” functionality.

Switching from Arch to Debian

Last night was a sad night. After 5 years of using Arch Linux, I decided to leave it for Debian. While I learnt a lot while using Arch, it was often disruptive to my work when it required maintenance. I woke up last night to work on a Physics assignment, and sure enough, my kernel panicked. I don’t blame Arch per se. If I retained a legacy kernel, it would have been fine. But that is the reason why I left – it would be nice if my computer worked out of the box with minimal maintenance.

One thing I will really miss about Arch is yaourt – a package manager which connects with the Arch User Repository. Basically any package you could think of would be contained in there. As an alternative, I installed Linuxbrew. It doesn’t seem to have as many packages, but I am really digging how the formulae are easily modifiable in ruby! (expect to see a linuxbrew-formulae repository in my Github soon…).

Segmented sleep for 5 weeks

For the next 5 weeks, I will be sleeping from 23:00-2:00 and 4:00-7:00, totaling in 6 hours of sleep per day. This is known as the segmented sleep schedule. If it goes well, I will likely continue with this schedule. For some of you 6 hours probably sounds like a lot, but I usually get 8 hours, so cutting out 2 is a significant improvement.

My motive behind doing this is that I was recently offered a research opportunity that I could not refuse (I will likely post about it later). To make time for it, I had to sacrifice something – my choice was sleep. About a year ago I was on a similar routine and it worked well, but I haven’t had the motivation to start doing it again until now.

Continue reading

Dabbling around with Haskell

So last night I went to a Haskell workshop.

One of the examples my professor gave was the factorial function:

The first line is the function signature, the second line is the base case, and the third line is the recursive step of the function. One thing I really like about Haskell so far is how clean recursive functions are.

Today I started dabbling a bit more with Haskell and created a recursive function to calculate pi using the Gregory–Leibniz formula:

I had to name it “pie” because “pi” was a reserved keyword. The “fromIntegral n” is there to allow an integer to be part of a fractional calculation. This is necessary because Haskell is über strict with types.

Here it is in action:

Gravity Simulator

A while ago I made a physics simulator for projectile motion which would gravitate towards the ground. I decided to build off this idea and make a free space gravity simulator!!!

To launch new planets, click and drag. When the planets collide, they will create a larger planet with a greater mass. The dynamics of this were done in a 3D perspective, so that is why planets grow very slowly in proportion to their mass build up (the mass is proportional to the cube root of the radius).
Check it out!

Conservation of momentum is followed, so launching a small planet with a high velocity at a large planet will produce a small velocity change due to the mass difference 🙂

I made a Chrome plugin!

So I am currently facilitating the hardware room at the UTM Hacks hackathon… Currently, everyone is settled down and hardware registration has been dead for a while, so I created a Chrome plugin!

The functionality is to play elevator music whenever you are on a login page. In my opinion, this is arguably one of the best plugins ever written 😉

Without further ado, here it is!