Slide 1

Slide 1 text

C for Dynamic Language Devs This won't hurt a bit Or How I Learned to Stop Worrying and love DTrace Thursday, May 23, 13

Slide 2

Slide 2 text

Obligatory “We’re Hiring” Slide They also paid for me to be here Thursday, May 23, 13

Slide 3

Slide 3 text

Today, We Learn C Airship Titanic, CC-BY 2.0 by Luis Daniel Carbia Cabeza at Flickr I'm Sure It'll Go Swimmingly Thursday, May 23, 13

Slide 4

Slide 4 text

Fear is the mind-killer Thursday, May 23, 13

Slide 5

Slide 5 text

Know Perl? Thursday, May 23, 13

Slide 6

Slide 6 text

Similarities ❖ C is a curly brace language with semicolons ❖ Variables are declared before use ❖ Functions serve as main organizational unit ❖ Perhaps more than Perl, C is Unix and vice versa ❖ Libraries exist for most tasks Thursday, May 23, 13

Slide 7

Slide 7 text

Data ❖ Integral types ❖ Floating point types ❖ Character types ❖ Aggregates ❖ Pointers Thursday, May 23, 13

Slide 8

Slide 8 text

Integral Types ❖ Integers, the char type, long integers, short integers ❖ Comprised of different amounts of memory ❖ No floating point operations ❖ Can and will {over,under} flow ❖ Use GMP if you need arbitrarily large (>264) values Thursday, May 23, 13

Slide 9

Slide 9 text

Pointers ❖ Not that hard ❖ A pointer contains a memory address ❖ The unary * operator gets the thing pointed to ❖ The unary & operator gets an object's memory address ❖ That's pretty much it Thursday, May 23, 13

Slide 10

Slide 10 text

Strings ❖ Least fun part of C ❖ Null-terminated ❖ Implemented as pointers ❖ Thankfully, lots of libraries to help Thursday, May 23, 13

Slide 11

Slide 11 text

Functions ❖ Like in Perl but with types ❖ Real prototypes ❖ Similar calling conventions ❖ Push button, receive bacon ❖ Check manual section 3 Thursday, May 23, 13

Slide 12

Slide 12 text

Libraries Not just book porn Thursday, May 23, 13

Slide 13

Slide 13 text

C's CPAN ❖ C has libraries ❖ Many Perl libraries build on C libraries ❖ A lanky British guy wrote something about picking libraries to use ❖ Do what he says Thursday, May 23, 13

Slide 14

Slide 14 text

The Standard Library ❖ Ships with every C implementation ❖ Covers some basics like string handing, maths, memory management, OS primitives ❖ Still very minimal: no JSON, no HTTP client Thursday, May 23, 13

Slide 15

Slide 15 text

Where to find libraries ❖ Github ❖ SourceForge ❖ Vendor's packages ❖ Search engine ❖ Your favorite app's dependencies Thursday, May 23, 13

Slide 16

Slide 16 text

Syscalls ❖ These do things that interact with hardware or the kernel ❖ Context switching ❖ Things like read(), write(), ioctl() ❖ But also time-related functions and other things ❖ Check manual section 2 Thursday, May 23, 13

Slide 17

Slide 17 text

RTFM Thursday, May 23, 13

Slide 18

Slide 18 text

C Library Manuals ❖ Most good libraries have manuals ❖ Might be in traditional roff format, might be HTML or something else ❖ Always read the manual for the version of the lib you're using Thursday, May 23, 13

Slide 19

Slide 19 text

Important Sections ❖ SYNOPSIS ❖ DESCRIPTION ❖ RETURN VALUES ❖ SEE ALSO Thursday, May 23, 13

Slide 20

Slide 20 text

Debugging Your App Why You Came Here, I Hope Thursday, May 23, 13

Slide 21

Slide 21 text

Tracing Tools ❖ Lots of different tools for different OSs ❖ All operate under the same premise ❖ Can trace already-running programs ❖ dtruss* ❖ truss ❖ ktrace ❖ strace * dtruss is part of the DTrace toolkit. DTrace is a scriptable tracing tool comprised of awesome and win. Thursday, May 23, 13

Slide 22

Slide 22 text

⸘WTF‽ ❖ These tools all output system calls ❖ Arguments ❖ Return values ❖ The best part: each and every one of these syscalls is documented (or should be) Thursday, May 23, 13

Slide 23

Slide 23 text

Some Miscellany ❖ C apps have some boilerplate before / after running user code ❖ Shared library loading, etc. ❖ This is pretty system-specific, takes some experience to see past Thursday, May 23, 13

Slide 24

Slide 24 text

How to Use the Info ❖ These tools cover syscalls, not user level functions* ❖ But they show you all the details about all the syscalls ❖ Read arguments and return values—they should make sense for what you're trying to debug ❖ These tools are useful not just for your code but also in general—find out why that module isn't loading e.g. Except DTrace, of course Thursday, May 23, 13

Slide 25

Slide 25 text

Some Examples Thursday, May 23, 13