Thursday, October 6, 2011

Debugging your OSGi Application using OSGi console commands

This blog post is mainly targeted at the WSO2 Carbon developers who make use of OSGi console to debug their bundles/components during the runtime. I'm here assuming that you have the basic shell which you would find in equinox environment.
You can start any of the WSO2 server products (that is based on Carbon) with OSGi console enabled, by passing the system property 'osgiConsole' during the startup. To do so, start the carbon server with,
./wso2server.sh -DosgiConsole
After full server startup pressing enter in the command shell will give you the OSGi shell command prompt, which looks like;
osgi>

After that you can valid OSGi command using this shell. Let us explore some of those,
The below commands will help you while debugging bundles in 'Module Layer':


Command    Use-Case
install file:/<filePath of bundle jar>  Install bundle jar in to runtime
ss To simply list down the bundles along with the life-cycle state we can use this command.
start <bundleId found using ‘ss’ command>  starting the bundle given by bundle ID.
b <bundleId> Prints all the details attached with bundle including  importing packages, exporting packages, etc.
diag <bundleId> If the bundle is still in installed state we can use this command to find the missing constraints which are stopping bundle going in to next lifecycle stage
p <packageName> List all the bundles that import/export the given package name within the runtime.
stop <bundleId> stopping the bundle given by bundle ID
uninstall <bundleId> uninstalling the bundle



To debug OSGi services(Service Components) layer you can use the following commands.

Command    Use-Case
ls list down all the service components found in this runtime along with their component ID and life-cycle status
enable <componentID> enabling service components
comp <componentID> Use this command to find unsatisfied services of the given component, which prevent it from going it to satisfied state.
ls  -c <bundleID> same as the above command, but you can retrieve the information on your service components using the bundle ID instead of component ID
services list down all the services in the runtime including services provided by the system bundle. You can use simple filters to narrow down your list.

eg: To find out the bundles/serviceComponents that expose the OSGi service ‘ org.foo.bar’

you can use the command:
>services (objectclass=org.foo.bar)    or
>services (objectclass=*foo.bar)
disable <componentID>  disabling component  given by the component ID

You don’t have to memorize these commands; all the commands along with their documentation can be found using the OSGi console command ‘help’. This is just a quick reference guide. You can extend the available set of commands by writing your own command provider. Hope this helps.