Dev:Logos

Logos is a component of the Theos development suite that allows method hooking code to be written easily and clearly, using a set of special preprocessor directives.

= Overview =

The syntax provided by Logos greatly simplifies the development of MobileSubstrate extensions ("tweaks") which can hook other methods throughout the OS. In this context, "method hooking" refers to a technique used to replace or modify methods of classes found in other applications on the OS.

= Getting Logos =

Logos is distributed with Theos, and you can use Logos' syntax in any Theos-built project without any extra setup. For more information about Theos, visit its page.

= Command Line Interface =

Logos can be invoked on the command line via. Usage is as follows:

Below are two examples, one using the default generator (Substrate) and one using the internal generator:

= List of Logos Directives =

Block level
The directives in this category open a block of code which must be closed by an %end directive (shown below). These should not exist within functions or methods.

%group
Groups are for conditional initialization or code organization. Grouping can be useful for managing backwards compatibility with older code.

Begin a hook group with the name Groupname. Groups cannot be inside another  block. All ungrouped hooks are in the implicit "_ungrouped" group. The _ungrouped group is initialized for you if there are no other groups. You can use the  directive to initialize it manually. Other groups must be initialized with the  directive.

%hook
Open a hook block for the class named Classname.

Can be inside a %group block.

Here's a trivial example:

%new
Add a new method to a hooked class or subclass by adding this directive above the method definition. signature is the Objective-C type encoding for the new method; if it is omitted, one will be generated.

Must be inside a %hook block.

You should also declare the new method within an interface as Logos does not do that automatically. For example:

%subclass
Subclass block - the class is created at runtime and populated with methods. ivars are not yet supported (use associated objects). The %new specifier is needed for a method that doesn't exist in the superclass. To instantiate an object of the new class, you can use the %c operator.

Can be inside a %group block.

Here's an example:

%property
Add a property to a %subclass just like you would with @property to a normal Objective-C subclass as well as adding new properties to existing classes within %hook</tt>.

Must be inside a %subclass</tt> or %hook</tt> block.

%end
Close a group/hook/subclass block.

Top level
The directives in this category should not exist within a group/hook/subclass block.

%config
Set a logos configuration flag.

%hookf
Generate a function hook for the function named symbolName. If the name is passed as a literal string then the function will be dynamically looked up.

It is common for people to hook the function that its address is resolved at run-time like, MGGetBoolAnswer</tt>, for example:

You can also do:

And replace  with any expression for a function pointer.

%ctor
Generate an anonymous constructor (of default priority).

%dtor
Generate an anonymous deconstructor (of default priority).

Function level
The directives in this category should only exist within a function block.

%init
Initialize a group (or the default group). Passing no group name will initialize "_ungrouped", and passing class=expr arguments will substitute the given expressions for those classes at initialization time. The + sigil (as in class methods in Objective-C) can be prepended to the classname to substitute an expression for the metaclass. If not specified, the sigil defaults to -, to substitute the class itself. If not specified, the metaclass is derived from the class. The class name replacement is specially useful for classes that contain characters that can't be used as the class name token for the %hook</tt> directive, such as spaces and dots.

Usage:

%class
Forward-declare a class. Outmoded by %c</tt>, but still exists. Creates a $Class variable, and initializes it with the "_ungrouped" group.

%c
Evaluates to Class</tt> at runtime. If the + sigil is specified, it evaluates to MetaClass instead of Class. If not specified, the sigil defaults to -, evaluating to Class.

%orig
Call the original hooked method. It doesn't function in a %new</tt>'d method. It works in subclasses, strangely enough, because MobileSubstrate will generate a supercall closure at hook time. (If the hooked method doesn't exist in the class we're hooking, it creates a stub that just calls the superclass implementation.) args is passed to the original function - don't include self</tt> and _cmd</tt>, Logos does this for you.

%log
Dump the method arguments to syslog. Typed arguments included in %log</tt> will be logged as well.

= File Extensions for Logos =

xi or xmi files can use Logos directives in #define macros.

= Splitting Logos Hooking Code Across Multiple Files =

By default, the Logos pre-processor will only process one .xm file at build time. However, it is possible to split the Logos hooking code into multiple files. First, the main file has to be renamed to an .xmi file. Then, other .xm files can be included in it using the #include directive. The Logos pre-processor will add those files to the main file before processing it.

Groups
Normally, it isn't possible to initialize hooking groups across multiple Logos files, but there is a workaround that can be used to unlock this functionality. This is done by wrapping group initializations inside of static methods that can then be called from other files.

Take a look at the following code. All it does is log a message when the SpringBoard application has finished launching. It is inside of a group called TweakGroup, which is initialized in a static function called InitGroup.

As you may have noticed, there is an import for Shared.h at the top of Group.xm. That is simply a header file that will be imported into our main Logos file so that we may call the function there:

Finally, Shared.h can be imported into the Logos file that contains your constructor. Calling the static function will initialize the group from Group.xm and run its hooks:

If done correctly and compiled without errors, this could should log two messages: one from the constructor and the other one from the method inside the group. Keep in mind that this doesn't apply to hooks that aren't inside of a group.

A few things to note:
 * This doesn't work in normal C, so you must use .xm files for your groups and constructor.
 * You have to pay attention to how many times you call the initialization as Logos will no longer tell you if it is called more than once.

= logify.pl =

You can use logify.pl to create a Logos source file from a header file that will log all of the functions of that header file. Here is an example of a very simple Logos tweak generated by logify.pl

Given a header file:

You can find logify.pl at $THEOS/bin/logify.pl and you would use it as so:

The resulting output should be:

= External links =


 * Main logos.pl file
 * logify.pl file
 * %hookf syntax change