Sunday, December 23, 2012

Usetrace - first feasible tool to test webapps from users perspective

I have been working on my startup Usetrace since February. It's the first feasible tool to test webapps from users perspective. We participated in Startup Sauna accelerator in November 2012 and loved every bit of it!

I'm explaining more about the idea behind Usetrace at http://blog.usetrace.com/?p=1 so check it out!

Oh, sign up at http://usetrace.com, we're taking new users in as fast as we can. We're also looking for talent. If you feel you can help us, tweet me @dhpe.

Friday, August 24, 2012

Vim tips (especially for the Scandinavian keyboard)

Despite being a Vim user for 9 years I have often felt unhappy about the fact how inefficiently I'm using the editor. I decided to dedicate some time and become more proficient in Vim. Most of the new power comes with the increased use of Vim's basic stuff, e.g. the change command and simply trying to find the simplest way to define what to yank, delete, and where to jump.

Some of the new power comes with better key mappings. This is especially true for Scandinavian keyboard owners like me which is be default unsuitable for Vim. This post is about .vimrc.

The first thing is always enforce use of hjkl for simple navigation by disabling the arrow keys in normal and insert modes.

map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>
imap <up> <nop>
imap <down> <nop>
imap <left> <nop>
imap <right> <nop>

Let's note that in Vim hjkl for navigation is usually the most inefficient way.

In Finland the ~ character is behind a combination of altgr, ~, space. That is three keypresses. The ~ toggles between lowercase and uppercase in Vim. The following mapping brings it behind one keypress as it's meant to be.

map § ~ "for Finnish/Swedish keyboard

Often search is the quickest way to define a desired location in the document. Again on the Finnish keyboard the / is behind two keypresses. Search should be behind one press, so it was mapped to space.

map <space> /

Switching between windows should be quicker. I mapped Ctrl with hjkl for window navigation.

map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

For buffer navigation I mapped leader (the comma) with ö and ä. This is again useful for the unfortunate using Scandinavion keyboard, which by the way is not at all optimal for programming either.

map <leader>ö :bNext<cr>
map <leader>ä :bprev<cr>

For switching to paste mode and back I have previously used the :set paste and :set nopaste commands. Now it is mapped to leader pp which toggles between the modes quickly.

map <leader>pp :setlocal paste!<cr>

Finally, some coffeescript friendly configuration. The compilation stuff demands the coffee.vim plugin which everybody has already.

In coffeescript string interpolation is "done like #{oh boy}". The Scandinavian keyboard users' wrists are in pain. The following very warm and fluffy insert mapping injects the #{} in the context, puts the cursor after the { and stays in the insert mode.

imap <leader>§ #{}<esc>i "The § is located left to 1 on the Scandi kb.

Coffeescript programmers need to check the compiled javascript from time to time. I have mapped leader c to CoffeeCompile which opens the current buffer's compiled version in a new split window.

map <leader>c :CoffeeCompile<CR>

For checking the visually selected region's compiled code, I use

vmap <leader>c <esc>:'<,'>:CoffeeCompile<CR>

I don't want to pollute my source code directories with the compiled .js files. The following can be used to change the compile target path.

let coffee_make_options = '--bare --output ~/tmp/coffeecompiles/'.expand("%:h")

The following setting does CoffeeCompile automatically when a .coffee buffer is saved. Useful!

au BufWritePost *.coffee silent CoffeeMake! -b | cwindow | redraw!

Disclaimer! Most of the stuff here has been found from the net from various forgotten sites, so I reject all credit, except for the #{} mapping which me and my co-founder came up with - yes we write a lot of coffeescript every day!

Sunday, October 23, 2011

Potion - a language of dots

Potion is a little language and runtime by why the lucky stiff. Not much potion can be found from the net, so I wanted to conjure some. Check my Church numerals implementation from github: https://github.com/avuori/Churchpotion.

In many ways conjuring potion feels like writing Python and, not surprisingly, Ruby, but there are differences. One of the most distinctive syntactical feature is that in Potion dot is not used as a separation of an object and a message as in many object oriented languages, but rather, as in prose, as an indication of the end of something. 

Potion is a language of dots, and that becomes quickly apparent, as one ends up ending nested blocks of potion with several dots...... Though in some potion recipe it was mentioned that there is a convenience method of using a single underscore to end nested blocks of code to avoid the trail of dots. To me however, "dotting" was a delightful practise, like sprinkling salt into my sweet potion to make it perfect.

Potion is jitted to machine code before execution. Before that potion is compiled to an intermediate bytecode format (what seemed like a register-based virtual machine code). You can get the Potion implementation from github: https://github.com/fogus/potion. I think in some fork I saw a REPL as well but I did not try it.

A final tip: If you want to compile Potion runtime in Linux, you may need to create a symlink /lib/libdl.so -> /lib/libdl.so.2.

I discovered Potion from a fogus' post - be sure to check that as well!

Thursday, January 6, 2011

How to listen to a netradio channel when the firewall blocks the port

TCP port 8000 is being blocked in the customer's premises where I'm currently working. This is a Windows box. Putty resolves the problem easily for me, with some support from a remote FreeBSD setup. A GNU/Linux box would do the same.

1. Configure Putty to forward a TCP port to some local port in the FreeBSD machine.
From Putty configuration,
-> Connection -> SSH -> Tunnels -> "Add a new forwarded port"

  • In the Source port field, type some port, that is the port where you will be connecting to from your media player. Use for example, 8080.
  • In the Destination field, type for example, localhost:8000.

2. Now with Putty's SSH port forwarding configured, SSH to your FreeBSD/Linux account.

3. Now we need to forward the netradio data to the port what is being forwarded to the Windows machine. For that, I'm using wget and netcat. So in the Linux/FreeBSD account, command
 wget -qO-  http://relay5.slayradio.org:8000 | nc -l 8000
The above is redirecting wget's output to stdout and that is then piped to netcat that is listening to local port 8000. That was the port that is being forwarded. Note that in some version of nc, you may need to add the -p switch for it to work, e.g. "nc -l -p 8000".

4. Now, the netradio data is available from port 8080 from the Windows computer. You may now connect to http://localhost:8080/ using your favorite media player and start listening.

Sunday, November 7, 2010

Plus sign in email address

I ordered a micro sd memory card from a random Finnish webstore today. The website appeared to have emerged via time machine from the mid 90s. However I have to give them credit. They did not try to be too clever and validate (incorrectly) my email address in their web form. They allowed a plus sign in the email address.

The plus sign is a very handy tool in organizing/labeling email automatically. Anything after the plus sign is ignored by email servers. Thus it can be used as a label. For example, if I order something from Amazon, I use an address like me+amazon@example.com. In my email client, I can then recognize anything that has amazon as the label and do what ever organizing I want automatically.

So good job, you, the webstore from the mid 90s! Many, many others do it wrong. The programmer's lesson is this. Do not try to validate an email address yourself. If you really need to validate it, use a library that does it right. Often the best way to validate an address is to send mail to it.

Saturday, October 2, 2010

Imagine an all-display phone

There are two major forces that are pushing into opposite directions in mobile phones' physical design. One is the space we have for the apps on the phone screen where the screen size and screen resolution matter. We like to have space. Another is the physical size of the device. We like the device to be small, thin and light.

Look at the physical appearance of the current top mobile phone models. The iPhone, for example, has a screen that is almost as large as the front side of the device. However, the utilization of the available area of the whole device is under 50%. There are clearly space for significant improvement.

Imagine a device that utilizes closer to 100% of the available area. This way, we could increase the space we have for apps dramatically without increasing the size of the device.

In this kind of a device, we would have a uniform working area folded around the device. Some applications could use all of it, or you could have a music player on one side and a browser on the other. An e-book application could always have the next page waiting on the other side, and swapping the page could happen by turning the device 180 degrees around its axis. Turning in the opposite direction would naturally work as expected. Scrolling a webpage would make the page flow over the border neatly. The innovative possibilities are vast.

I did not discuss the technical issues here and on purpose. As in UI design, it is often good to forget about the possible constraints, think outside the box, and just imagine freely. Often the engineers can do it anyway sooner or later.

Wednesday, September 1, 2010

Interfacing Win32 API from Qt

If you ever need to do this sin, just include qt_windows.h and you are free to access native Win32 API.

To ensure the monster still compiles on other platforms, you may even use #ifdef Q_WS_WIN evilness as well.

Enjoy your objectionable life.

Wednesday, October 28, 2009

A good article on the problems in the Finnish startup environment

Finland is Missing the Bowling Alley or How to Find the First Pin

The linked article discusses well why it is hard for Finnish software startups to become major worldwide players.

Tuesday, October 27, 2009

A correct implementation for the 'halts' function in the Buddhist context

Yields a correct answer for all programs P.
halts P = 無

Monday, October 26, 2009

Flu^3

We all have noticed the mass media hysteria around 'swine flu'. Give it a cool name and it feels more dangerous. Make it dangerous and sell more papers. Were there a high profile war going on, the press would have other things in mind. Now it is the flu, however.

What a coincidence that I just got my third flu virus infection this month only. Normally I get flu once/twice a year. Haven't caught the SWINE (it intimidates more in caps, doesn't it) version yet, though - so they say. I'm going to write to Guinness if I get my fourth infection.

All this even though I use Linux.

Sunday, October 25, 2009

Feynman on the feeling of inadequacy

The linked quote is great, well worth linking!

You have no responsibility to live up to it!

Saturday, October 24, 2009

Book: Founders at Work

I'm only halfway reading Jessica Livingston's Founders at Work - stories of startups' early days, but I can already say that this will be one of the top books I have read this year.

Did you know that Yahoo, then already one of the biggest sites in the world, was running on diesel fuel for several days during a power outage?

Friday, October 23, 2009

New startup and a word about types

It's been a while. I joined a new tech startup in Helsinki and have had no time to write to my blog. But I'm always returning! See?

My summer adventures at TKK went smoothly and I'm happy that I had the opportunity to work in a very interesting project and enhance my C/C++ skills there. Now I'm writing mostly Python and it feels great after months of C, almost like if somebody just imported antigravity. I have to admit, I have long been a strong proponent of explicit typing as it is a form of self-documentation, but when you just need to build something quickly, as in startups, explicit types are simply slowing you down. I need to add that I'd still love static typing, something Python does not offer.

Days are already dark in Helsinki and it's getting cold. Now we have to face six months of darkness, which is always so... intriguing. I have always wondered though how quickly it passes. Now that I have Python with me, the spring will surely return in no time.

Have you ever wondered why your relatives are always asking for your help with computers? It's not that they are stupid, but the computers are still way too difficult to use. That's going to change in the next decade(s). I bet the whole platonic idea of the computer is going to change. It'll be more like that everything is a computer, and we will start calling these devices something else that more accurately describes their function.

Wednesday, September 9, 2009

Adding a license text to a set of files using sed

The problem: I needed to add (prepend) a license text to a set of source files.

I thought sed might be a good tool for this task. Having not done anything but trivial sed scripts in the past, I took a look in the web for advice.

I was faced with the following script (I already lost the original URL, I'm sorry):


1{h; r [file]
D;}
2{x; G; }



It needs some explaining. Sed works by maintaining two data buffers: the pattern space and the hold space. A sed execution cycle is performed for each input line. First the input line is placed in the pattern space. Then the commands are (conditionally) executed. Finally, the contents of the pattern space are printed to stdout (if not explicitly prevented). The hold space maintains its contents between two cycles. The pattern space on the other hand is cleared between two cycles.

The GNU sed manual explains the used commands as follows.

h - "Replace the contents of the hold space with the contents of the pattern space."

r [file] - "Queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication."

D - "Delete text in the pattern space up to the first newline. If any text is left, restart cycle with the resultant pattern space (without reading a new line of input), otherwise start a normal new cycle."

x - "Exchange the contents of the hold and pattern spaces."

G - "Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space. "


So the program is performing the job as follows. The first set of commands is executed only for the first line of the input. The first line is copied to the hold buffer. After that, the file [file] is queued to be printed to stderr in the end of the current cycle. Finally, the contents of the pattern space is deleted (so that the first line won't be printed just yet).

Now the file [file] (containing the license text) has been printed. Nothing else has been printed. Also, the first line has been saved to the hold space. Looks good. The second set of commands do the rest. These commands are executed for the second line of the input only. First the contents of the hold space and the pattern space are switched. Now the hold space contains the second line of the input and the pattern space contains the first line of the input. Now a newline and the contents of the hold space are appended to the pattern space. The pattern space now contains the first two lines of the input in correct order. Next, the cycle ends and the contents of the pattern space are printed.

No commands are executed for the remaining lines of the input. They are just printed by default. We have printed the license text, the first line of the input, the second line of the input and the rest of the input in correct order - effectively prepending the license text on the input files.

Now considering the license text is located at /tmp/license, the following code is exactly what is needed to do the job for each *.c and *.h files in the current directory.


sed -i '1{h; r /tmp/license
D;}
2{x; G; }' *.[ch]

Monday, August 24, 2009

Google's 3D view over Vallila/Helsinki

And so Google Earth got 3D view of Helsinki [digitoday.fi]. Here's a picture depicting Vallila, that is, my current home.

Friday, August 14, 2009

LLVM assembly and integer signedness

LLVM assembly does not encode integer signedness which can be a nuisance when reading the assembly code. On the other hand the policy simplifies the representation, as the sign does not make a difference in most of the operations (such as integer addition). In some operations (such as boolean comparisons on integers <, <=, >, >=) signedness is significant, however. For completeness, let's remember that the (dis)equality operator does not care about the sign.

Let's have a look on the following C program block:
if (x < UINT_MAX-1)
puts("foo");
else
puts("bar")


Piping clangs's output to llvm-dis gives us the following LLVM assembly representation:
%1 = load i32* %x, align 4
%2 = icmp ult i32 %1, -2
br i1 %2, label %bb, label %bb1

bb:
%3 = call i32 @puts(i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0)) nounwind
br label %bb2

bb1:
%4 = call i32 @puts(i8* getelementptr ([4 x i8]* @.str1, i32 0, i32 0)) nounwind
br label %bb2

bb2:


Now, in the assembly code, it can be read that the %1 register is compared with -2, and the operands are to be interpreted as unsigned 32-bit integers. On the bit level, that is of course correct. However, a human needs to make a conversion in her head to understand on what concrete values of %1 the first branch is taken.

In the two's complement, the first bit represents the sign. For negative numbers, the sign bit is set, for positive, it is unset. The number zero is represented as a value where all bits are unset. The smallest negative value of an n-bit integer is represented as one followed by n-1 zeros. Semantically it can be thought as if the rightmost n-1 bits represent a positive value that is summed with the smallest negative value available (-2^(n-1)), and the result is something between -2^(n-1)..-1.

Assuming 4-bit integers, -2 is represented as 1110 (-2^3 + 6). When 1110 is interpreted as an unsigned integer, that is 14 (2^3 + 2^2 + 2^1). However, in our example, we have 32-bit integers (the i32 type in LLVM), where the min value for int is -2147483648. In the 32-bit world, -2 is represented by a number where there are 31 ones followed by a zero. Interpreted as unsigned, that is 2^31 + 2^30 ... 2^2 + 2^1 = 4294967294.

So we came to the conclusion, for values of %1 < 4294967294, the first branch is taken. Otherwise the second branch is taken. That was not obvious directly from the assembly code. We had to consider the integer width to deduce the concrete unsigned value.

It should be noted that one can decide whether to write integer literals as the llvm tools do or not. Since the bit representation is identical for signed i32 -2 and unsigned i32 4294967294, it does not matter which way it is written. I would still stick to the signed interpretation for consistency's sake. Otherwise it becomes a hopeless mess.

Tuesday, August 4, 2009

Sex differences

I wanted to see what kind of questions we are asking about the opposite sex. Google. Let us assume that questions asked about men are usually asked by women and vice versa.

Top suggestions for 'how do women...':
1. get pregnant
2. think
3. get yeast infections
4. flirt
5. come

Top suggestions for 'how do men...':
1. think
2. fall in love
3. flirt
4. get hpv(*)
5. get yeast infections

(*) human papillomavirus

Comparing the first matches makes me sad.

Friday, July 24, 2009

Emil finds its first defect!

I earlier described the nature of my work at TKK tcslab. My job is to write a tool for automatically testing C programs using a technique called dynamic symbolic execution basing the implementation on LLVM. I call my tool Emil (that is ucfirst(reverse(lower(Lime))) after a similar tool called Lime for testing Java programs, developed here as well).

Emil is currently at 2.9 KLOC, and for the first time, I wrote a test case that should fail, and it failed on the first attempt! This is somewhat a milestone so I now celebrate it by showing the test case and the failing inputs found by Emil. This is of course all trivial and useless, but I believe Emil is going to the right direction.

==divzero.c==

int input(void);
int main() {
int i = input();
int j = input();
if (i > j) {
i = i / (0*j); // ouch
} else {
i = j;
}
return i;
}


==Values for i, j by Emil that will lead to division by zero==
{i: 892648572, j: -1285173836}

I hope this is just the beginning for Emil. The future of Emil will be out of my hands, though, because I will (in my current knowledge) leave Emil in the hands of others before next year and be searching for new challenges in the software industry.

Sunday, July 19, 2009

Back to work

Ah, my week-long summer vacation is now over. I mostly spent the time in a friend's summer cottage in Sysmä and in the beautiful archipelago of Sipoo. Last two days I was in Porvoo. Vacation reading included works from Hesse and Solzhenitsyn, but I especially want to raise Erlend Loe's Naive, Super that I fell in love with when sitting in a bus.

As for work, I'm now familiar with LLVM optimization pass architecture and implementation. It all feels intuitive. On the negative side, C++ frequently demonstrates its clumsiness in basic tasks. Also, the LLVM API is poorly documented, so I constantly find myself grepping (actually, ack'ing) around the LLVM tree and reading code rather than the doxygen (huh) generated API. However, all in all I'm definitely thankful for LLVM.org/docs.

Wednesday, June 17, 2009

C++ name mangling

Ever wondered what the 'extern "C" { ... }' block stands for in C++? In C and C++ the 'extern' keyword is used to declare external variables that are instantiated in other translation units. Using 'extern' tells the compiler not to allocate space for the variable as space is being allocated elsewhere. The keyword is also used within variable instantiation. In that case its semantics are to denote that the variable may be referenced from other translation units.

The 'extern "C" { ... }' block, however, has additional semantics. Let's first explain what name mangling is and why it is required.

In C functions cannot be overloaded by type. Thus we cannot declare, for example, functions with prototypes 'int foo(void)' and 'int foo(int)' in the same program. In C++, however, such overloading is possible. Due to the tight coupling of C and C++, particularly in the past (C++ was first implemented by compiling C++ code to C code and then using a regular C compiler to produce machine code), C++ compilers must use distinct symbols for overloaded functions to be compatible with the linker. Name mangling is the act of adding type information in the function names to separate overloaded functions from each other.

For example, the GNU C compiler mangles the first 'foo' function [int foo(void)] to '_Z3foov' and the second 'foo' function [int foo(int)] to '_Z3fooi'. The '_Z' is just a prefix that is a unique identifier in C/C++ that prevents conflicts with user defined identifiers. The number is the length of the original name, three in case of 'foo', and the following letters encode argument types (here 'i' for int and 'v' for void, respectively).

Now, suppose there is a C++ function 'bar' that we would want to use in our C module. We would declare a prototype for 'bar' in the C module that references the function and link the C module with the C++ module, that provides the definition for 'bar'. Let's try just that:


-------module.c--------
int bar(void);
int hooray() {
return bar() + 42;
}
-----------------------


-------bar.cpp--------
int bar(void) {
return 7;
}
-----------------------


Let's first compile our modules:

$ g++ -c -O0 -o bar.o bar.cpp
$ gcc -c -O0 -o module.o module.c


Then link them together to produce an executable

$ gcc -O0 bar.o module.o


module.o: In function `hooray':
module.c:(.text+0x7): undefined reference to `bar'
collect2: ld returned 1 exit status


The linker could not find 'bar' from the symbol table. And that is precisely due to C++ name mangling. Let's have a look inside bar.o:

$ objdump -D bar.o

-- [clip] --
00000000 <_Z3barv>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: b8 07 00 00 00 mov $0x7,%eax
8: 5d pop %ebp
9: c3 ret
-- [/clip] --


There it is again, the function is called '_Z3barv' instead of 'bar'. This is the point where the extern "C" block comes to the rescue. It declares that the symbols declared inside the block are to be referenced from C context, and thus the compiler cannot mangle the names.

If the code in bar.cpp is wrapped inside the extern "C" block, everything will work:

-------bar.cpp--------
extern "C" {

int bar(void) {
return 7;
}

}
-----------------------


Recompiling bar.cpp:

$ g++ -c -O0 -o bar.o bar.cpp

.. and relinking the modules:

$ gcc -O0 bar.o module.o


No errors. If you now disassemble bar.o you will see that the name of the bar function is now 'bar' instead of '_Z3barv', and thus the C module is able to reference the C++ function.