#1 2017-05-10 14:01:22

mduch
Member
Registered: 2017-05-10
Posts: 2

TSynLog automatic handling exceptions and SafeLoadLibrary problem

I have application build with many dynamic loaded modules (dll). (loaded on need)
I tested TSynLog and discovered that after using SafeLoadLibrary automatic handling of exceptions doesn't work.
It happens because of overwriting RtlUnwindProc by SafeLoadLibrary -> _StartLib  (System.pas).
So using TSynLog.Family.Level:= LOG_VERBOSE in begining of program setup RtlUnwindProc and SafeLoadLibrary restore orginal RtlUnwind.

Is it possible to use TSynLog with automatic handling of exception in application which is using dynamic loading libraries ?

Offline

#2 2017-05-11 07:16:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Re: TSynLog automatic handling exceptions and SafeLoadLibrary problem

With which version of the Delphi compiler?
I don't see any overwriting of RtlUnwindProc in SafeLoadLibrary itself. It may be in the library itself, which calls _startlib...

Have you some sample code ( publish e.g. it on https://pastebin.com ) to reproduce the issue?

Offline

#3 2017-05-11 11:29:04

mduch
Member
Registered: 2017-05-10
Posts: 2

Re: TSynLog automatic handling exceptions and SafeLoadLibrary problem

Thank you for quick reply.
I use Delphi 10 Seatle (Win32).
Here is example based on sample 11 - Exception logging
example program code (LibraryTest - procedure Test dynamically linked) - https://pastebin.com/g5yCSLWH
example library code (MyLibrary  - procedure Test only raise exception) - https://pastebin.com/4yzGmz7p
You can see what is happening watching address RtlUnwindProc.
After TSynLog.Family.Level := LOG_VERBOSE; there is a change -> RtlUnwindProc is set (RTLUnwindProc := @SynRtlUnwind;)
After SafeLoadLibrary RtlUnWindProc is restored to orginal address
(SaveLoadLibrary System.pas -> _StartLib ->
  version CPUX86
        MOV     EAX, offset RaiseException
        MOV     RaiseExceptionProc, EAX
        MOV     EAX, offset RTLUnwind
        MOV     RTLUnwindProc, EAX
  version CPUX64  -> not assembler so more readable (for me)
  // Setup exception handler
  RaiseExceptionProc := @RaiseException;
  RTLUnwindProc := @RTLUnwind;
)
Procedure statically linked (procedure Test; external 'MyLibrary.dll';) is working because
library is loaded first and then after TSynLog.Family.Level := LOG_VERBOSE; address is already setup correctly.

Another issue is how to use TSynLog, what is the best practise.
I'dont want add duplicatiing code (SynCommons, SynLog) to program and every dll so I built package mormot.bpl with SynCommons, SynLog and used it like runtime package in my program and dll.
It works ok. But in such case there is problem with locating map files. TSynMapFile uses fMapFile := GetModuleName(hInstance); and finding mormot.bpl instead exe or dll name.

Offline

#4 2017-06-23 09:52:38

FasBelgium
Member
Registered: 2016-07-13
Posts: 2

Re: TSynLog automatic handling exceptions and SafeLoadLibrary problem

Hi,
I think TSynlog is a wonderful tools . So we are trying to use TSynlog but it seems we have the same problem when we load dynamically a DLL.
Do you have a solution ?
Thanks

Offline

#5 2017-12-06 04:20:14

jairgza
Member
Registered: 2015-01-15
Posts: 10

Re: TSynLog automatic handling exceptions and SafeLoadLibrary problem

I run into this case too, same thing happen with version  "Class: TSynLog 1.18.4001 FTS3"

Is there any news about it?

Thanks

Offline

#6 2017-12-06 08:12:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Re: TSynLog automatic handling exceptions and SafeLoadLibrary problem

Don't use packages, if you want to reduce the .dll file size.
If you want small executables and libraries, use FPC or a Delphi version prior to 2010.

Packages made sense to add components to the Delphi IDE when there was a few MB of RAM, but now it is not the case any more.
And sharing code between main exe and libraries induces some unneeded dependency between them.

But if someone is willing to debug the package support, feel free to contribute!

Offline

Board footer

Powered by FluxBB