API
You can use GpProfile API to start/stop profiling from the profiled program.
More:
ProfilerStart
ProfilerStartThread
ProfilerStop
ProfilerTerminate