Introduction to strace

There will come a time when you will find yourself asking “What the heck is that process doing?”. To uncover the mysteries behind the behaviour of a process, we have a tool called strace.

The program strace is very handy when you want to debug the execution of a program. It catches and states all the system calls performed called by a process. It will also catch and state any inter-process signals received by this process.

Let’s dive into some examples.

Trace the execution

..is the simple straight-forward way to use it. The output might look something like:

The output looks a bit messy, but it can provide very useful information like which files does this program use, what is this program doing right now or why does this program not read the config file – does it even look for it?

Attach to an existing process

You can run strace on a process that’s already running. Use -p, and provide the Process ID (PID):

The output presented to you is similar to the example above. Notice that you can only trace a process you have access rights to. Multiple -p options will also trace these processes with a limit of 32 processes that strace can attach to.

Trace child processes

Use -f to trace child processes as they are created by currently traced processes. This is useful for debugging a program that spawns children. strace will prepend the pid of the traced process output:

Trace specific system calls

The e-flag, along with the call open, displays only open system calls of the ls command, which outputs something along these lines:

With the above example, close to the end, you can see two things – ghost and ghost.zip, a folder and file, which is the actual output of the command ls.

A few of the options available after -e trace= are:

  • open
  • close
  • read
  • write

Trace multiple system calls

What if you want to trace multiple system calls in one command though? The option -e trace= can take a comma separated set of systemcalls as argument:

Which outputs:

Write output to file

If you’d like to write the output of strace to a file, add a -o:

When you print the contents of ls.txt, you’ll see that it contains only the output from strace, and not from the command ls:

Add a timestamp

If you add a -t to the command, you’ll be able to add a timestamp to each printed line. You can add up to 3 -t‘s. The more you add, the more verbose and detailed the timestamp will be.

One -t shows you seconds:

Adding a second -t will display microseconds:

A third -t will include the microseconds and the leading portion will be printed as the number of seconds since the epoch.

Print relative timestamp

If you’d like to find out the execution time of each call, -r will certainly do the trick:

A summary of system calls

Would you like to have a summary of calls, time, and errors for each system call? -c does this:

This option is very useful when trying to find out why a program is running slow.

Introduction to lsof

Today I’m going to introduce you to the exciting world of lsof.

As you will see, it’s a nifty little tool that has various uses, and it has so many switches that you need both a and a + to use all the options available! As the description of the command says in its man-page:

lsof – list open files

So, let’s get started.

Network

List network connections

Continue reading

urxvt and urlLauncher not working

I recently found myself having problems with urxvt. In my configuration-file, .Xdefaults, I had the following lines set up:

This would enable urxvt to highlight a link in white, and when clicking it would launch the URL in to a tab of my browser of choice. But one day it suddenly stopped working, for no apparent reason. After a little digging, I found the following in urxvt’s changelog, http://cvs.schmorp.de/rxvt-unicode/Changes:

– INCOMPATIBLE CHANGE: renamed urlLauncher resource to url-launcher.

Changing URxvt.urlLauncher to URxvt.url-launcher in .Xdefaults seemed to fix the problem!

Reducing PDF file-size in Linux

The other day I downloaded a PDF that ended up being a whole lot bigger than I thought. A “whopping” 230MB, which is another deal compared to the 30MB PDF’s that I’m accustomed to. So how to reduce the file-size? Ghostscript to the rescue!

If you have Ghostscript installed, run the following command to reduce the file-size of your PDF:

Now you have a few options here under -dPDFSettings:

  • /screen selects low-resolution output, and the lowest file-size.
  • /ebook selects medium-resolution output, with a medium file-size.
  • /printer and /prepress are both the high-resolution options, which is mainly used for printing PDFs. As you might have guessed, this option gives you the biggest file-size (yes, even bigger than your mother).

The results?

What can I say. I’m a cheap bastard when it comes to storage space, no matter how low the price might be. 😉

Changing My Tune on Exercising

I used to play soccer, and it was great. Through soccer I made great friends, and my shape was great. This however was close to 14 years ago. Between then and now, well, I’ve become quite the big guy. It’s hard to admit, seeing as I spend a lot of moments looking back at what once was. It’s a horrible place to be stuck in, and I wouldn’t wish it on my worst enemy. But the fact is, and as cliché as it might sound, the only person able to make a change, is yourself.

I’ve recently begun finding joy in exercising again, and though I used to hate the thought of jogging, things have changed. Getting back to the shape I once had is going to be difficult, but not impossible. But if anyone out there thinks along the lines of what I do, a distant goal can be quite the challenge. As I often (and unfortunately) tend to repeat to myself, what good is tomorrow, if I can’t survive today? It’s obvious that I need to change the way that I think, and trust me, I’m working on it. I’ve never been able to make myself set smaller goals, which makes the process of losing weight and getting in shape even more difficult. There is however one thing that has kept me motivated, and eager, to work out. It’s simply the feeling of afterwards. As in you’ve lifted something heavy, or have run a distance. You’re freshly showered, and your tired muscles are giving you a “Well done”. Even if you for some reason can’t complete the entire set, or the workout you planned, at least you tried. And that’s a lot better than just sitting on the couch. The next time, try a little bit harder, run a little bit faster, push yourself just the tiniest bit. Improvement isn’t just in the bigger steps, it’s in the small ones as well. That’s what I try to tell myself at least.

Other things that help me with motivation is reading about working out, before actually doing it. Currently I’m working on the program called Stronglifts. You can read more about it at http://www.stronglifts.com. Reddit as well is a big help, either looking at before/after pictures, or /r/GetMotivated.

I’ll be posting more about Stronglifts in the weeks to come, mainly for myself to keep track of things.

The way is tough, but well worth it. Or so I’ve been told. 😉