1

the issue I'm experiencing , is certain applications make use of a library that has been created to include logging , we cannot recompile the code for the running applications as they are legacy applications.

so I need to add functionality in the library file and relink the source code with the library files.

a function defined in the library file called DEBUG_DATA()

-> returns function name line etc. -> this function is already used in the application , what I am required to do is on execution of this function DEBUG_DATA I need to add an event and trigger an event once the function that calls debug_data returns.

is it possible to create a trigger on an event in c where the trigger isn't called ? can this be done in runtime ? as in tweaking the stack?

EDIT: this is added to DEBUG_DATA():

void Dynatrace_Method_START(
   const char *ms_FunctionName, const char *ms_FileName, int ms_LineNO)
{
#ifdef DT_USE /*a global variable needs to be initiated for stack trace lets call it int Dyna_M_SERIAL*/
    DT_Current_Stack++;
    if (DT_Current_Stack<=DT_STACK) {
        // FILE_NAME_STRIPPER(ms_FileName);
        Dyna_M_SERIAL++;
        fprintf(pfile,"%d,mstart,%s,%s,%d,%s,%d\r\n",
            Dyna_UID, ms_FunctionName, FILE_NAME_STRIPPER(ms_FileName), ms_LineNO,
            sProgram_‌​name, Dyna_M_SERIAL);
        Line_Reference[Dyna_M_SERIAL]=ms_LineNO;
    }
#endif
}

OS is Guardian H Series TNS\E, compiler is Compaq ETK -NSE.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
CK_ONE
  • 11
  • 2
  • Modifying the stack can be detected by the operating system (or an antivirus program) as a malicious program is running, and your program may be marked as a virus or other hacking malware. – Some programmer dude Sep 04 '13 at 12:15
  • I'm afraid I don't really get your question? Could you show s.th. you've tried or interface declarations? – πάντα ῥεῖ Sep 04 '13 at 12:16
  • You can hook the function. – Michael M. Sep 04 '13 at 12:16
  • Is this a library (.lib) or dynamic link library (.dll)? – Dialecticus Sep 04 '13 at 12:24
  • Is DEBUG_DATA can be call several time in the function ? – Jarod42 Sep 04 '13 at 12:34
  • Can you disassemble the code of DEBUG_DATA ? Is patching the DLL can be an acceptable solution ? – Michael M. Sep 04 '13 at 12:39
  • What can you change ? You can do what you want if instead of calling your `debug_data()` function, you just instantiate an object whose constructor calls your existing debug_data(), and destructor calls your new event function. (This is feasible for C++ code, not for C) – nos Sep 04 '13 at 12:39
  • @Joachim: there won't be an issue regarding antivirus as the applications run on main frame. – CK_ONE Sep 04 '13 at 13:09
  • @ Michael: how do you hook a function ? – CK_ONE Sep 04 '13 at 13:10
  • @Dialecticus: its .tlo its a library function for Non Stop KErnel Tandem , gaurdian OS – CK_ONE Sep 04 '13 at 13:10
  • @ Nos: unfortunately its only c – CK_ONE Sep 04 '13 at 13:11
  • I can basically change the code of the .tlo library and recompile the library , thereafter relink it – CK_ONE Sep 04 '13 at 13:12
  • this I added to debug data : – CK_ONE Sep 04 '13 at 13:14
  • void Dynatrace_Method_START(const char *ms_FunctionName, \ const char *ms_FileName, \ int ms_LineNO) { #ifdef DT_USE /*a global variable needs to be initiated for stack trace lets call it int Dyna_M_SERIAL*/ DT_Current_Stack++; if (DT_Current_Stack<=DT_STACK) { // FILE_NAME_STRIPPER(ms_FileName); Dyna_M_SERIAL++; fprintf(pfile,"%d,mstart,%s,%s,%d,%s,%d\r\n" ,Dyna_UID,ms_FunctionName,FILE_NAME_STRIPPER(ms_FileName),ms_LineNO,sProgram_name,Dyna_M_SERIAL); Line_Reference[Dyna_M_SERIAL]=ms_LineNO; } #endif } – CK_ONE Sep 04 '13 at 13:14
  • I need to call the following at the end of of the function that called debug data: – CK_ONE Sep 04 '13 at 13:15
  • void Dynatrace_Method_END(const char *me_FunctionName, \ const char *me_FileName \ ) { int me_LineNO; #ifdef DT_USE if (Dyna_M_SERIAL>0) { me_LineNO= Line_Reference[Dyna_M_SERIAL]; //FILE_NAME_STRIPPER(me_FileName); fprintf(pfile,"%d,mend,%s,%s,%d,%s,%d\r\n" ,Dyna_UID,me_FunctionName,FILE_NAME_STRIPPER(me_FileName),me_LineNO,sProgram_name,Dyna_M_SERIAL); Dyna_M_SERIAL--; } #endif } – CK_ONE Sep 04 '13 at 13:15
  • it basically sends triggers to a monitoring system , and measures time on the other end, somehow I need to trigger the above when the function that calls debug data ends – CK_ONE Sep 04 '13 at 13:17
  • @ Jarod42: debug_data can get called multiple times, however alot of the time its only called once per function – CK_ONE Sep 04 '13 at 13:19
  • 1
    I have a feeling the answer will require more than just C/C++. What is this OS, what is the compiler? I think this requires the knowledge of assembler for this particular mainframe. – Dialecticus Sep 04 '13 at 13:21
  • OS is Guardian H Series TNS\E, compiler is Compaq ETK -NSE – CK_ONE Sep 04 '13 at 13:24
  • Regarding ["hooking" a function...](http://stackoverflow.com/questions/7743771/function-hooking-in-c). But you need to wait for one to many calls to DEBUG_DATA(), and since you need to wait until the _calling_ function exits this technique may not be the solution, . – ryyker Sep 04 '13 at 15:37
  • @Dialecticus - I agree with your statement _the answer will require more than just C/C++_. But even if access to the disassembled code was available, how would that help since he is constrained by legacy (and unchangeable) executable code? – ryyker Sep 04 '13 at 15:55
  • @ryyker, In Windows it is possible to reroute dll calls, so a proxy function could run this prolog and epilog code. I don't know how this is done, but am pretty sure it's doable. Maybe something similar is doable in this Guardian H OS, who knows... – Dialecticus Sep 04 '13 at 16:14
  • Are you sure that `DEBUG_DATA()` is actually a function and not a preprocessor macro? The only way I am familiar to pull the source file name, function name, and line number is to use macros such as `__FUNCTION__`, `__FILE__`, `__LINE__`, etc. Since these return the values of the current function, they are usually wrapped in macros to which then call the actual debugging function. For instance `#define DEBUG_DATA() debug_data_func(__FILE__, __FUNCTION__, __LINE__)` or just do the string manipulation `#define DEBUG_DATA() "File: " __FILE__ " Function: " __FUNCTION__ " Line: " __LINE__` – David M. Syzdek Sep 04 '13 at 16:53
  • hi David well its a function that does make use of macros: – CK_ONE Sep 05 '13 at 07:58
  • #define debug_data(message) \ clib_debug_data(message,__FUNCTION__,__FILE__,__LINE__) – CK_ONE Sep 05 '13 at 07:59
  • applogies you are correct its a preproccessor macro – CK_ONE Sep 05 '13 at 08:00
  • In "legacy" code, you could replace the calls to the underlying library function by using BINDER to link in your own function of the same name. This could call the original function (you could rename it using BINDER). Similar functionality is available with eld in the TNS/E environment as several commercial products use it for application-transparent data replication and security. – Andy Simpson Sep 05 '13 at 11:06
  • @Andy: if the function names are unknown ? so for a generic sloution where it applies to all functions that contain a sub call to a known function is this still possible ? – CK_ONE Sep 05 '13 at 16:01
  • @CK_ONE If you want to intercept calls to known function X from generic program(s) which call X without knowing/caring where they call X, then this is possible. I suspect that I may be missing out a level of indirection in my understanding of your question. Apologies for delay, been off SO for a few weeks. – Andy Simpson Sep 19 '13 at 07:06

0 Answers0