Date post: | 18-May-2015 |
Category: |
Technology |
Upload: | pavel-novitsky |
View: | 1,403 times |
Download: | 5 times |
Pavel Novitsky
Meet Magento Belarus 2012
Magento Debug Process
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
Brian Kernighan
The main thing about Magento is PHP
Popular practices of PHP applications debugging.
1. Error output
2. Variable values
3. Structured data
4. Code tracing
5. Objects analysis
6. Database query
Error output
In php.ini:
display_errors = Onerror_reporting = E_ALL | E_STRICT
Directly in the application:
ini_set('display_errors', 'On');error_reporting(E_ALL | E_STRICT);
Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query
Popular practices of PHP applications debugging.
Variable value output
echo $myVar;
Virtually useless.
In most cases — a senseless waste of time.
Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query
Popular practices of PHP applications debugging.
Structured data output
var_dump($myArray);echo '<pre>'.print_r($myArray, true).
'</pre>';
Array( [key1] => value 1 [key2] => value 2)
Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query
Popular practices of PHP applications debugging.
array(2) { ["key1"]=> string(7) "value 1" ["key2"]=> string(7) "value 2" }
Code tracing
debug_backtrace() and print_debug_backtrace()
Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query
Popular practices of PHP applications debugging.
What object do we have?
get_class()
get_class_vars()
get_declared_classes()
method_exists()
Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query
Popular practices of PHP applications debugging.
Database query
a) echo $sql = "SELECT `some_field1` FROM `table` WHERE `some_field2` = '$var'";
b) $res = mysql_query($sql) or die(mysql_error());
Error outputVariable valuesStructured dataCode tracingObjects analysisDatabase query
Popular practices of PHP applications debugging.
It's good enough for a majority of applications.
But what about Magento?
error_reporting(E_ALL | E_STRICT);ini_set('display_errors', 1);
if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) { Mage::setIsDeveloperMode(true);}
addSetEnv MAGE_IS_DEVELOPER_MODE “true”to .htaccess
Popular practices of PHP applications debugging.
Error output
For intermediate variable verification only.
Still useless.
Standard practices used in Magento
Variable value output
$customer = Mage::getModel('customer/customer')->load(1);
print_r($customer);
Standard practices used in Magento
Structured data output
Mage_Customer_Model_Customer Object ( [_eventPrefix:protected] => customer [_eventObject:protected] => customer [_errors:protected] =>
Array ( ) [_attributes:protected] => [_addresses:protected] => [_addressesCollection:protected] => [_isDeleteable:protected] => 1 [
_isReadonly:protected] => [_resourceName:protected] => customer/customer [_resource:protected] => [_resourceCollectionName:protected] =>
customer/customer_collection [_cacheTag:protected] => [_dataSaveAllowed:protected] => 1 [_isObjectNew:protected] => [_data:protected] =>
Array ( [entity_id] => 1 [entity_type_id] => 1 [attribute_set_id] => 0 [website_id] => 1 [email] => [email protected] [group_id] => 1 [
increment_id] => 000000001 [store_id] => 1 [created_at] => 2007-08-30 23:23:13 [updated_at] => 2008-08-08 12:28:24 [is_active] => 1 [
firstname] => John [lastname] => Doe [password_hash] => 2049484a4020ed15d0e4238db22977d5:eg [prefix] => [middlename] => [suffix] =>
[taxvat] => [default_billing] => 274 [default_shipping] => 274 ) [_hasDataChanges:protected] => [_origData:protected] => Array ( [entity_id] =>
1 [entity_type_id] => 1 [attribute_set_id] => 0 [website_id] => 1 [email] => [email protected] [group_id] => 1 [increment_id] =>
000000001 [store_id] => 1 [created_at] => 2007-08-30 23:23:13 [updated_at] => 2008-08-08 12:28:24 [is_active] => 1 [firstname] => John
[lastname] => Doe [password_hash] => 2049484a4020ed15d0e4238db22977d5:eg [prefix] => [middlename] => [suffix] => [taxvat] => [
default_billing] => 274 [default_shipping] => 274 ) [_idFieldName:protected] => entity_id [_isDeleted:protected] => [_oldFieldsMap:protected] =>
Array ( ) [_syncFieldsMap:protected] => Array ( ) )
Standard practices used in Magento
Structured data output
Standard practices used in Magento
Useless again?
Standard practices used in Magento
Varien_Object::getData()
Varien_Object::debug()
$customer = Mage::getModel('customer/customer')->load(1);
echo '<pre>'.print_r($customer->debug(), true);
Standard practices used in Magento
Structured data output
[entity_id] => 1
[entity_type_id] => 1
[attribute_set_id] => 0
[website_id] => 1
[email] => [email protected]
[group_id] => 1
[increment_id] => 000000001
[store_id] => 1
[created_at] => 2007-08-30 23:23:13
[updated_at] => 2008-08-08 12:28:24
[is_active] => 1
[firstname] => John
[lastname] => Doe
[password_hash] => 2049484a4020ed15d0e4238db22977d5:eg
[prefix] =>
[middlename] =>
[suffix] =>
[taxvat] =>
[default_billing] => 274
[default_shipping] => 274
Standard practices used in Magento
Structured data output
Code tracing
debug_backtrace()
print_debug_backtrace()
Standard practices used in Magento
Varien_Debug::backtrace()
Standard practices used in Magento
Object analysis
get_class()
get_class_vars()
get_declared_classes()
method_exists()
Used everywhere in Magento
Database queries
echo $sql = "SELECT `some_field1` FROM `table`”;
$res = mysql_query($sql) or die(mysql_error());
Standard practices used in Magento
Database queries
Display a query:
$myCollection->load(true);
Write a query to the system log:
$myCollection->load(false, true);
Standard practices used in Magento
Database queries
$model = Mage::getModel('catalog/product')->getCollection();$sql = $model->getSelect()->__toString();echo $sql;
Standard practices used in Magento
Mage::getModel('catalog/product')->getCollection()->load(true);
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Database queries
Write all the queries to var/debug/pdo_mysql.log
Standard practices used in Magento
lib/Varien/Db/Adapter/Pdo/Mysql.php:
protected $_debug = true; protected $_logAllQueries = true;
What else?
Logging
Let’s experiment
<?php
error_reporting(E_ALL | E_STRICT);ini_set('display_errors', 1);
$mageFilename = 'app/Mage.php';require_once $mageFilename;
Mage::setIsDeveloperMode(true);umask(0);
Mage::app();
// … our code …
— developer’s Swiss knife
DEBUGGING and PROFILING
Installation
xdebug.profiler_enable_trigger=onxdebug.remote_autostart=offxdebug.remote_enable=1xdebug.remote_host="127.0.0.1"xdebug.remote_port=9000xdebug.remote_handler="dbgp"xdebug.idekey="netbeans"xdebug.collect_vars=onxdebug.collect_params=4xdebug.show_local_vars=onxdebug.var_display_max_depth=5xdebug.show_exception_trace=on
zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so
; zend_extension_ts="c:\php\ext\php_xdebug-2.0.1-5.2.1.dll"
xDebug
http://xdebug.org/download.php
Setting up
xDebug
Setting up NetBeans IDE
xDebug
Well, what good will that do?http://example.com/index.php?XDEBUG_SESSION_START=netbeans
xDebug
http://example.com/index.php?XDEBUG_SESSION_START=netbeans
easy Xdebug http://bit.ly/LKpvjC
Xdebug helper http://bit.ly/KuCo2c
Profiling
xdebug.profiler_enable=1
xDebug
Logs visualization
xDebug — profiling
Webgrind http://bit.ly/LXMGFJ
Kcachegrind http://bit.ly/KGzyAw
WinCacheGrind http://bit.ly/Nh4iPY
Xdebugtoolkit http://bit.ly/LmB4t9
MacCallGrind http://bit.ly/LlerGS
CachegrindVisualizer http://bit.ly/OD6dLy
The client visited…
<disable_local_modules>true</disable_local_modules>
<config><modules>
<Some_Module><active>false</active>
<Some_Module></modules>
</config>
Developer Toolbar for Magento http://bit.ly/LnSW8s
Magento Connect — developer tools
Advanced Developer Tools http://bit.ly/Lo1Vqa
Magento Connect — developer tools
Only for versions 1.6.1 and earlier
Magento FirePHP http://bit.ly/LnYGyX
Magento Connect — developer tools
Mage::helper('firephp')->send('Lorem ipsum sit amet ..');Mage::helper('firephp')->debug(Mage::getModel('catalog/product')->load(54));