Koozali SME Server Debugging/fr

From SME Server
Revision as of 18:27, 16 August 2024 by Gieres (talk | contribs)
Jump to navigationJump to search


Débogage du serveur

Un problème souvent rencontré par les nouveaux développeurs est le débogage du code Perl sur votre serveur SME.

Il existe plusieurs façons de procéder, à la fois localement et à distance.

Activer le débogage du modèle

Je suis tombé sur cela dans le fichier templates.pm

config set processtemplate Debug yes

Cela ajoutera quelques lignes de débogage à /var/log/messages vous montrant la progression de l'extension du modèle.

Pour désactiver, définissez la clé sur « no » ou supprimez-la.

Routines de base de données intégrées

Vous pouvez également utiliser quelques routines de db.pm

=item B<db_print>
 db_print(\%config);
 db_print(\%config, $key);

Imprime les clés et leurs valeurs brutes dans la base de données %config. Si $key est fourni, il imprime la $key et sa valeur brute. Si aucune $key n'est fournie, il imprime toutes les clés et leurs valeurs brutes.

=item B<db_show>
 db_show(\%config);
 db_show(\%config, $key);

Imprime les clés et leurs valeurs dans un format lisible par l'homme. Si $key est fourni, il imprime la $key, le type et les propriétés de cette $key. Sinon, il imprime la clé, le type et les propriétés de toutes les clés.

=item B<db_print_type>
 db_print_type(\%config);
 db_print_type(\%config, $key);

Imprime les clés et leurs types dans la base de données %config. Si $key est fourni, il imprime uniquement cette $key et son type. Sinon, il imprime toutes les clés et leurs types.

=item B<db_print_prop>
 db_print_prop(\%config, $key);
 db_print_prop(\%config, $key, $property);

Imprime les propriétés (ou une seule $property) de la $key donnée dans le %config.

En local, en utilisant perl -d

Il existe un certain nombre de tutoriels en ligne.

Pour commencer, utilisez :

perl -d myFile.pl

Il existe ensuite un certain nombre d'options que vous pouvez utiliser pour contrôler le débogueur :

l 10 - list line 10
l get_pattern - find lines matching 'pattern'
b 22 - set breakpoint at line 22
s - step forward a line including all subroutines
n - step forward a line but jump through subroutines
c - continue to next break point
c 47 - continue to line 47
p $variable - print variable
q - quit

Débogage à distance à l'aide d'un environnement de développement (IDE)

J'utilise Komodo IDE grâce à une licence pour développeurs « Open Source » de Activestate.

Vous pouvez désormais obtenir l'application gratuitement ici, mais vous avez besoin d'un compte gratuit pour l'utiliser :

https://www.activestate.com/products/komodo-ide/download-ide/

Le mode le plus simple à utiliser avec Komodo IDE consiste à utiliser simplement l'IDE comme débogueur pour parcourir le code. Le code sur le serveur distant peut être modifié si vous disposez des autorisations suffisantes.

https://docs.activestate.com/komodo/12/manual/debugger.html

Cela nous permet de travailler avec un bureau et un serveur. Consultez la section sur le proxy de débogage pour plusieurs connexions.

Configuration du serveur

À partir de votre répertoire Komodo installé, nous devons copier un ensemble de fichiers sur le serveur.

En fait, j'ai copié l'intégralité du répertoire dbgp car il contient d'autres fichiers utiles, notamment le proxy de débogage.

scp -r ~/komdoeditDirectory/lib/support/dbgp root@some.server://opt/

Nous devrions maintenant avoir /opt/dbgp sur le serveur

Nous devons maintenant définir des chemins sur le serveur :

export PERL5LIB=/opt/dbgp/perllib:$PERL5LIB
export PERLDB_OPTS=RemotePort=your.desktop.i.p:9020 async=1

Si nécessaire, vous pouvez également ajouter un identifiant utilisateur :

export DBGP_IDEKEY=jdoe

Note: comme pour le débogage local, la fonction « Break Now » est désactivée par défaut. Le réglage de « async=1 » interrompt la lecture à la première ligne - consultez la documentation pour plus d'informations.

Single machine setup

On your desktop we need to set up the Listener for Komodo.

Edit/Preferences/Debugger/Connection
Set a specific port to 9020 (match the port above)
Set a user name as above

Now we need to start a program on the server. The debug code will then call Komodo.

The following forms should work

perl -d myFile.pl

Add -d to the header of your file e.g.

#!/usr/bin/perl -w -d

Then do

perl myFile.pl

Add -d to /sbin/e-smith/signal-event

Then do

signal-event some-event

You should get a popup from Komodo stating that a remote application has requested a debugger session. When you click yes you will get a second box stating that a mapping could not be opened. If we are just going to use Komodo as a debugger then we can answer no and you are then in debug mode allowing you to step through the code and view variables.

Mapping for editing

Should you wish to actually edit code as well you will need to set up some form of file mapping between the URI that the debugger sends and your 'Server' setup in Komodo

Note that without some of the esmith libraries installed locally you will get some warnings about being unable to locate certain files but these can safely be ignored.

In Komodo preferences you need to setup a Server/Remote Account for the remote server. e.g.

Remote Account : Test_v10
Type: SCP
Port: 2222
Username: root
Pass: somepass (or alternatively use SSH keys)

You can then set up a mapping similar to this:

URI : file://v9-test/
Maps To : scp://Test_v9/

Or

URI : file://v9-test/etc/e-smith
Maps To : scp://Test_v9/etc/e-smith

You could be more specific about the directories should you require. Remember that you inherit the server permissions - you cannot save a file that is Read Only on the server !

Local file mapping

I seemed to have managed this using sshfs:

shfs -p 2222 root@192.168.10.199:/ ~/Mounts/somedirectory

You can then access the files normally and it is easy to map the remote to the 'local' files.

Tidying up afterwards

Either reboot the server or use the following:

export PERL5LIB=/usr/local/lib64/perl5:/usr/local/share/perl5:/usr/lib64/perl5/vendor_perl:/usr/share/perl5/vendor_perl
export PERLDB_OPTS=
export DBGP_IDEKEY=jdoe

Perl CGI Debugging

Debugging CGI programs on live production servers can seriously impair performance. You have been warned !!

Please see here for further details:

http://docs.komodoide.com/Manual/debugperl#debugging-perl-komodo-ide-only_configuring-perl-for-cgi-debugging

You would need a custom httpd.conf fragment. I used 46PerlDebug with these options (configure to suit)

SetEnv PERL5LIB "/opt/dbgp/perllib:$PERL5LIB"
SetEnv PERLDB_OPTS "RemotePort=192.168.x.x async=1"
SetEnv DBGP_IDEKEY "user"

You should now be able access your cgi-script and debug accordingly.

However, having tried it you cannot use this on server-manager panels because perl is setuid.

It could be used in other scenarios, but not server-manager.

Multiple machine debug proxy

TBA

Notes are here: http://docs.komodoide.com/Manual/debugger#debugging-programs-komodo-ide-only_remote-debugging_debugger-proxy

Perl with Visual Studio

Important.png Note:
To be refined


Still working on this but we need Perl Language Server

On your server:

 yum --enablerepo=* install gcc gcc-c++ perl-App-cpanminus perl-AnyEvent-AIO perl-Coro
 cpanm Class::Refresh
 cpanm ExtUtils::CBuilder
 cpanm Compiler::Lexer
 cpanm Hash::SafeKeys
 cpanm Perl::LanguageServer

On your workstation:

  • Set up your remote servers using ssh keys in .ssh/config
  • Install Visual Studio Code Remote Explorer
  • Restart
  • Go to Remote Explorer and you should see your server list
  • Open in Current or New window
  • Select the platform and let VS Code install itself
  • Go to File explorer
  • Open Folder
  • It will suggests opening in /root/ but I find it easier to open in /
  • Click OK and your folders will appear left
  • Go to your file and open it
  • Set a break point near the top
  • Now go to Debug, Add a Configuration, and accept the defaults
  • Now run the Debug and you should see the code stop at one of your break points


You can now step through your code.

PHP remote debugging

You can debug on your local workstation but it is extremely useful to be able to debug direct on the server environment. You may have one PHP version installed on your desktop but be running a different one on the server, and you may have a different server setup.

First you need to install the xdebug packages eg:


Important.png Note:
Xdebug 3 is not available for PHP versions less than 7.2 You will need php54-php-pecl-xdebug


yum install php74-php-pecl-xdebug3
yum install php80-php-pecl-xdebug3

Or

yum install php*php-pecl-xdebug3

Next you need a small template fragment to enable remote debugging.

Xdebug 2
mkdir -p /etc/e-smith/templates-custom/etc/php.ini
nano /etc/e-smith/templates-custom/etc/php.ini/90XdebugSettings 

Add this:

[Debugger]
; /etc/e-smith/templates-custom/etc/php.ini
xdebug.remote_enable                   = true
xdebug.remote_host                     = 127.0.0.1
xdebug.remote_port                     = 9001
xdebug.remote_handler                  = dbgp
xdebug.remote_log                      = /var/log/xdebug.log
xdebug.remote_mode                     = req
xdebug.max_nesting_level               = 5000


Xdebug 3

New syntax.

https://xdebug.org/docs/upgrade_guide https://xdebug.org/docs/all_settings

nano /etc/e-smith/templates-custom/etc/php.ini/90XdebugSettings

[Debugger]
xdebug.start_with_request               = yes
xdebug.discover_client_host             = true
xdebug.client_host                      = localhost
xdebug.client_port                      = 9003
xdebug.idekey                           = users
xdebug.mode                             = debug
xdebug.log                              = /var/log/xdebug.log


Expand templates and restart services:

signal-event webapps-update

We should see Xdebug here

php74 -v
PHP 7.4.28 (cli) (built: Feb 15 2022 13:23:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
   with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies
   with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans

Or like this:

php80 -v

PHP 8.0.19 (cli) (built: May 10 2022 08:07:35) ( NTS gcc x86_64 ) Copyright (c) The PHP Group Zend Engine v4.0.19, Copyright (c) Zend Technologies

   with Zend OPcache v8.0.19, Copyright (c), by Zend Technologies
   with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans

Komodo IDE

We can obtain the debug proxy from the installation, and move it to the server and then execute from there:

python /root/dbgp/bin/pydbgpproxy -d 127.0.0.1:9001 -i 192.168.10.1:9003

Or alternatively grab the lastest xdebug client here:

mkdir /root/xdbg-proxy
cd /root/xdbg-proxy
curl https://xdebug.org/files/binaries/dbgpProxy -o dbgpProxy
chmod 0700 dbgpProxy
./dbgpProxy -s 127.0.0.1:9001 -i 192.168.10.1:9003

In Komodo Go to Preferences, Debugger, Connections

Komodo should listen on: System provide port

Check 'I am running a debugger proxy' Listener address: IP.of.your.server:9003 Proxy key: individual key name for this desktop

Save and check that it connected to the debugger.

Now to debug a file:

https://your.server.ip/index.php?XDEBUG_SESSION_START=mydesktopkey

Or

https://your.server.ip/index.php?somevalue=3&XDEBUG_SESSION_START=mydesktopkey

Visual Studio/VS Codium

Sample launch.json

Note for newer versions of Xdebug the default port is now 9003 Pay careful attention to the path mapping,. This has to be absolutely correct or it will not work.

{
   // Use IntelliSense to learn about possible attributes.
   // Hover to view descriptions of existing attributes.
   // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
   "version": "0.2.0",
   "configurations": [
       {
           "name"        : "vscphpdebug",
           "type"        : "php",
           "request"     : "launch",
            "stopOnEntry" : false,
           "proxy": {
           "enable" : true,
           "host" : "my.server.ip",
           "key" : "users", // As set in the server above
           "port": 9003,
           "allowMultipleSessions" : true
           },
           "pathMappings": {
           "/home/e-smith/files/ibays/testbay/html/phptestcode": "${workspaceFolder}"
           },
       },
   ]
}

Now you can run start the PHP debugger in Codium so it connects the to the debug server, and then trigger it with a URL like this:

https://my.SME.Server/testbay/phptestcode/myTestPhpFile.php?XDEBUG_SESSION_START=mycodiumkey

Editors and IDEs

Some Open Source Editors/IDEs

These all allow remote debugging.

Komodo-IDE https://www.activestate.com/products/komodo-ide

Komodo Debug tools: https://code.activestate.com/komodo/remotedebugging/

Eclipse https://www.eclipse.org/

Netbeans https://netbeans.org/

Codium https://itsfoss.com/vscodium/ - Use PHP Debug - felixfbecker.php-debug
For Codium I can't see a remote Perl debugger as yet.