How to speed up / slow down application time

I think I'm not saying it right in the title. What I intend to do is connect to some api system, since time interruption occurs every time interval (since every application in the operating system interprets the time) and calls the application for this api, returning some larger / smaller result. Thus, from the point of view of the application, time speeds up or slows down. I found some windows application that can do this, can someone give me some directions on how to implement this on Mac OS X?

+4
source share
4 answers

Your question should be divided into two parts:

  • What source of time values โ€‹โ€‹is used by many applications?
  • How to intercept this source?

Answer to 1) (approximately in order of importance):

  • System clock read by gettimeofday (which, in turn, is used by NSDate, CFAbsoluteTime, ...)
  • Clock sound card. Many multimedia applications synchronize visual effects with audio and use audio as a moving source of time.
  • Video clock (vsyncs counting)
  • CPU tick counters, interrupts, etc.
  • Just advancing the program counter (execution speed).

Edit:

  • Clock synchronization features. Thanks @tc for pointing this out.

gettimeofday interception gettimeofday relatively simple: just create a dynamic library containing a function with the same name and signature as the original gettimeofday (see #include <sys/time.h> ). This function should return any value you want over time.

To use this special gettimeofday function in an application, run it in the terminal, as in:

 DYLD_INSERT_LIBRARIES=/path/to/myGetTimeOfDay.dylib /Applications/TextEdit.app/Contents/MacOS/TextEdit 

Of course, your dylib should be consistent with the application architecture.

+3
source

I am not an OS X developer, so I cannot give you details. But I know Windows and I can guess how this can be implemented - you can convert some of these methods.

To illustrate, suppose most programs that want to follow the abstract flow of time on Windows use GetTickCount . This returns the number of milliseconds since the system started. (There are other APIs that can use the application, but the technique remains the same.)

To change the time that appears for an application, you must change the values โ€‹โ€‹returned from this time function. (for example, multiplied by a coefficient), so we create a wrapper function to apply the transformation:

  DWORD GetTimeWarpTickCount() { static double factor = 2.0; return (DWORD)(GetTickCount()*factor); // simple implementation - you can be more sophisticated than this // to preserve accuracy if necessary } 

After you have encoded the function, use hooking to replace the original Win32 function with your own. A connection can be made based on each application, so you can localize time changes for specific applications.

So, in short, if there is a way to override the basic time functions provided by OS X applications, then you can apply the same procedure.

+2
source

In addition to fixing gettimeofday () (or whatever it calls under the hood), you will also need to fix mach_timebase_info (): it returns the frequency mach_absolute_time (), which is used almost everywhere where the system API has a timestamp, which is "time with loading time. " Of course, you can also fix mach_absolute_time (), but that would probably be more overhead. See QA1398 for some details (note that the line elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom subject to overflow).

(The corresponding POSIX function is clock_gettime(CLOCK_MONOTINIC, &result) . It is not clear why OSX does not implement this.)

+2
source

If this is your application, just dial all your time requests through the function you are writing that has a scalar. To do something like an arbitrary application, enter the code and pay the trap, so to speak. I think mach-inject still exists, and there are other similar tools like ape that put a nice face on it. Even if this is your application, you can use a similar approach in mach-inject to control the result of time system calls.

All cocoa clocks and basic time clocks wall out until gettimeofday. There are other ways to track time, but this is a good start.

+1
source

Source: https://habr.com/ru/post/1305505/


All Articles