HHVM is currently gaining popularity at quite a pace, and it's a pretty exciting time for PHP runtimes. Have you ever wondered what is going on beneath this slick and super-speedy engine? I wondered that myself, so I dived into the internals of HHVM, discovering a treasure trove of awesome stuff. In this talk, I'll show you how HHVM itself works with a guided tour of the codebase, demonstrating how it all pieces together. I'll also show you a couple of ways to write your own incredible HHVM extension. You don't need to know C++ to understand this talk - just PHP language knowledge is enough.
49. Run with gdb
$ gdb --args
/usr/bin/hhvm
-d extension_dir=.
-d hhvm.extensions[]=calc.so
test.php
GNU gdb (Ubuntu 7.9-1ubuntu1) 7.9
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.
org/licenses/gpl.html>
--- snip ---
Reading symbols from /usr/bin/hhvm...done.
(gdb)
50. Breakpoints
(gdb) b ext_calc.cpp:6
No source file named ext_calc.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (ext_calc.cpp:6) pending.
(gdb)
51. Running
(gdb) r
Starting program: /usr/bin/hhvm -d extension_dir=. -d hhvm.extensions[]
=calc.so smoke.php
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 2, HPHP::f_calc_add (a=5, b=3) at /home/james/workspace/hhvm-
calc/ext_calc.cpp:6
6 return a + b;
(gdb) p a
$1 = 5
(gdb) p b
$2 = 3
(gdb)
52. Handy commands
c continue / step out
n step over
s step into
p x print the value of a variable (x)
set x = y set a variable (x) to value (y)
bt print backtrace
q quit :)
68. HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
69. HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
70. HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
71. HHVM Universe
<?php
$o = new Foo();
$o->bar();
PHP Land
class Foo {
public:
Foo() {}
~Foo() {}
int value = 5;
}
Planet C++
int64_t HHVM_METHOD(Foo,bar)
{
auto data =
Native::data<Foo>(this_);
return data->value;
}
Time Vortex!?!?!
C++ object !== PHP object
77. Resources
● OpenGL Tutorial
○ http://www.opengl-tutorial.org/
● HHVM Example Extension
○ https://github.com/hhvm/extension-example
● Sara Golemon - HHVM extension blog series
○ http://blog.golemon.com/2015/01/hhvm-extension-writing-part-iii.html
● Derick Rethans’ extension API cookbook
○ https://github.com/derickr/hhvm-hni-cookbook
● The official API documentation
○ https://github.com/facebook/hhvm/wiki/Extension%20API
● Journey of a Thousand Bytecodes
○ http://hhvm.com/blog/6323/the-journey-of-a-thousand-bytecodes